java的preparedstatment中的addbatch的使用

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.