JDBC Batch Insert OutOfMemoryError

本文介绍了一种使用JDBC批处理向MySQL数据库中批量插入大量记录的方法,并解决了在此过程中遇到的内存溢出问题。通过调整批处理大小并定期执行批处理任务,有效地避免了内存占用过高的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have written a method insert() in which I am trying to use JDBC Batch for inserting half a million records into a MySQL database:

public void insert(int nameListId, String[] names) {
String sql = "INSERT INTO name_list_subscribers (name_list_id, name, date_added)"+" VALUES (?, ?, NOW())";
Connection conn = null;
PreparedStatement ps = null;

try{
conn = getConnection();
ps = conn.prepareStatement(sql);

for(String s : names ){
ps.setInt(1, nameListId);
ps.setString(2, s);
ps.addBatch();
}

ps.executeBatch();

}catch(SQLException e){
throw new RuntimeException(e);
}finally{
closeDbResources(ps, null, conn);
}
}


But whenever I try to run this method, I get the following error:

java.lang.OutOfMemoryError: Java heap space
com.mysql.jdbc.ServerPreparedStatement$BatchedBindValues.<init>(ServerPreparedStatement.java:72)
com.mysql.jdbc.ServerPreparedStatement.addBatch(ServerPreparedStatement.java:330)
org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:171)

If I replace ps.addBatch() with ps.executeUpdate() and remove ps.executeBatch(), it works fine, though it takes some time. Please let me know if you know if using Batch is appropriate in this situation, and if it is, then why does it give OurOfMemoryError?

[color=green]It is out of memory because it hold all the transaction in memory and only send it over to the database when you call executeBatch.

If you don't need it to be atomic and would like the get better performance, you can keep a counter and call executeBatch every n number of records.[/color]

public void insert(int nameListId, String[] names) {
String sql = "INSERT INTO name_list_subscribers (name_list_id, name, date_added)"+" VALUES (?, ?, NOW())";
Connection conn = null;
PreparedStatement ps = null;

try{
conn = getConnection();
ps = conn.prepareStatement(sql);
int icount = 0;
for(String s : names ){
ps.setInt(1, nameListId);
ps.setString(2, s);
ps.addBatch();
if(icount%1000 == 0){
ps.executeBatch();
ps.clearBatch();
}

icount++;
}

ps.executeBatch();

}catch(SQLException e){
throw new RuntimeException(e);
}finally{
closeDbResources(ps, null, conn);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值