java的preparedstatment中的addbatch的使用
jvm:-Xmx1024m
size = 1000000,可以插入成功。
如果再為size增大一個數量級,還是OutOfMemoryError,這時記憶體已經不好再增加分配了
批量處理本來是為了提高效能,但是太大的資料提交,因為堆的膨脹,堆的定址會造成效能反而下降。
如果是為了交易處理,不妨用個變通的辦法,建立暫存資料表,匯入資料,還是可以用到addBatch,比如每100條一個批次。
然後在另外一張表有個記錄,記錄是否全部匯入成功的標誌。
基本思路是利用兩段提交理論
------------------------------
public class App {
public static void main(String[] args) throws Exception {
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "marshal", "password");
PreparedStatement preparedStatement = connection
.prepareStatement("insert into addbatch_test(id,name) values(?,?)");
int size = 1000000;
for (int i = 0; i < size; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setString(2, "n" + i);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
preparedStatement.close();
connection.close();
}
}
轉自:http://hi.baidu.com/dyancheng/blog/item/2691422a3cad3a3f5243c1df.html