2012-12-22 113 views
2

SimpleJdbcTemplate中是否有batchUpdate()函數的最大記錄限制。鏈接heremysql批量更新限制

我需要在以下行

MapSqlParameterSource[] batchArgs = batchArguements.toArray(new MapSqlParameterSource[0]); 
simpleJdbcTemplate.batchUpdate(SQL_INSERT, batchArgs); 

任何建議在time.Presently我收到超時約10萬的記錄插入?

+0

您是否需要將它們插入到一個事務中,或者是否可以將插入事件拆分爲多個事務? – Ralph

+0

目前我試圖將它插入單個事務中,但是它獲取timeout.Is他們是無論如何插入單事務? – Sajith

+0

可能是一個「max_allowed_pa​​cket」問題,我想知道。 – Joddy

回答

0

這是我們使用的類。請參閱Javadoc以獲得解釋:

import java.util.ArrayList; 
import java.util.List; 
import javax.persistence.EntityManager; 
import javax.persistence.Query; 
import org.apache.commons.collections.CollectionUtils; 
import org.apache.commons.lang.StringUtils; 
import com.google.common.base.Function; 

/** 
* Performs a MySQL bulk insert. 
* <p> 
* A MySQL bulk insert statement allows for performing multiple insertions with 
* one insert statement: 
* 
* <pre> 
*  INSERT INTO tbl (col1, col2, ..., colN) VALUES 
*  (val1.1, val1.2, ..., val1.N), 
*  (val2.1, val2.2, ..., val2.N), 
*   ..., 
*  (valM.1, valM.2, ..., valM.N); 
* </pre> 
* </p> 
* 
* @param <T> the entity type to work on. 
*/ 
public class MySqlBulkInsert<T extends Object> { 
    private final EntityManager entityManager; 
    private final String tableName; 

    /** 
    * Constructor. 
    * 
    * @param entityManager the entity manager 
    * @param tableName the name of the table 
    */ 
    public MySqlBulkInsert(final EntityManager entityManager, final String tableName) { 
    this.entityManager = entityManager; 
    this.tableName = tableName; 
    } 

    /** 
    * Executes the bulk insert. 
    * 
    * @param entities the entities to insert 
    * @param csvColumns the columns to insert as a comma separated values string 
    * @param entityToValuesFunction the entity to values function. 
    * Please note: Values have to be correct formatted. For example String values 
    * must contain apostrophes. 
    * @return the number of records inserted 
    */ 
    public int execute(List<T> entities, 
     String csvColumns, 
     Function<T, Object[]> entityToValuesFunction) { 
    return execute(entities, csvColumns.split("\\s*,\\s*"), entityToValuesFunction); 
    } 

    /** 
    * Executes the bulk insert. 
    * 
    * @param entities the entities to insert 
    * @param columns the columns to insert 
    * @param entityToValuesFunction the entity to values function 
    * Please note: Values have to be correct formatted. For example String values 
    * must contain apostrophes. 
    * @return the number of records inserted 
    */ 
    public int execute(List<T> entities, 
     String[] columns, 
     Function<T, Object[]> entityToValuesFunction) { 
    if (CollectionUtils.isEmpty(entities)) { 
     return 0; 
    } 

    final StringBuilder sql = new StringBuilder(); 
    sql.append("insert into "); 
    sql.append(tableName); 
    sql.append(" ("); 
    sql.append(StringUtils.join(columns, ',')); 
    sql.append(") values "); 
    List<String> records = new ArrayList<String>(); 
    for (T entity : entities) { 
     Object[] values = entityToValuesFunction.apply(entity); 
     String csvValues = StringUtils.join(values, ','); 
     if (columns.length != values.length) { 
     throw new IllegalArgumentException("Number of values doesn't match number of columns (" 
      + columns.length + "): " + csvValues); 
     } 
     records.add("(" + csvValues + ")"); 
    } 
    sql.append(StringUtils.join(records, ',')); 
    Query query = entityManager.createNativeQuery(sql.toString()); 
    return query.executeUpdate(); 
    } 
} 
+1

你的答案不是JDBC,不能解決問題,即使對於JPA也有點古怪。 –

+0

@AdamGent,照顧詳細? OP要求爲MySQL提供解決方案 - 我提供了一個。出於某種原因,該類名爲_MySql_BulkInsert。如果它是JPA,它怎麼能不是JDBC?沒有什麼古怪的,它使用MySQL支持的SQL特性。 –

+0

謝謝answer.is在單個事務中插入?在這段代碼中,這個entityManager類在做什麼? Query query = entityManager.createNativeQuery(sql.toString()); – Sajith