insert()
MySQLデータベースに50万レコードを挿入するためにJDBCバッチを使用しようとしているメソッドを作成しました。
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()
、少し時間がかかりますが、問題なく動作します。この状況でバッチを使用することが適切かどうかを知っているかどうかを教えてください。適切である場合、なぜそれが得られるのOurOfMemoryError
ですか?
ありがとう