JDBC批处理插入OutOfMemoryError

问题描述 投票:21回答:2

[我编写了一种方法insert(),其中我试图使用JDBC Batch将50万条记录插入到MySQL数据库中:

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);
    }
}

但是每当我尝试运行此方法时,都会出现以下错误:

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)

如果我将ps.addBatch()替换为ps.executeUpdate()并删除ps.executeBatch(),则它会正常工作,尽管需要一些时间。如果您知道在这种情况下使用Batch是否合适,请告诉我,如果可以,为什么它会给出OurOfMemoryError

谢谢

jdbc batch-file large-data-volumes out-of-memory
2个回答
44
投票

[addBatchexecuteBatch为您提供了执行批处理插入的机制,但是您仍然需要自己执行批处理算法。

如果您只是将每条语句堆放在同一个批处理中,则会耗尽内存。您需要每n个记录执行/清除批处理。 n的值取决于您,JDBC无法为您做出决定。批处理大小越大,运行速度就越快,但是过大会导致内存不足,并且运行速度会变慢或失败。这取决于您有多少内存。

例如,从批量大小为1000开始,然后从那里尝试不同的值。

final int batchSize = 1000;
int count = 0;
for(String s : names ) {
   ps.setInt(1, nameListId); 
   ps.setString(2, s);
   ps.addBatch();

   if (++count % batchSize == 0) {
      ps.executeBatch();
      ps.clearBatch(); //not sure if this is necessary
   }
}
ps.executeBatch();   // flush the last few records.

5
投票

内存不足,因为它将所有事务保存在内存中,并且仅在您调用executeBatch时才将其发送到数据库。

如果您不需要它是原子的并且希望获得更好的性能,则可以保留一个计数器,每n]个记录数就调用executeBatch

© www.soinside.com 2019 - 2024. All rights reserved.