Java JDBC clearBatch() 和堆内存
我注意到以下行为。
我有一个大约 3MB 的文件,包含数千行。 在行中,我拆分并创建准备好的语句(大约 250 000 个语句)。
我所做的是:
preparedStatement
addBatch
do for every 200 rows {
executeBatch
clearBatch().
}
最后
commit()
内存使用量将增加到 70mb 左右,而不会出现内存不足错误。是否可以降低内存使用量?并具有事务行为(如果一个失败则全部失败。)。 我能够通过使用 executeBatch
和 clearBatch
进行提交来降低内存...但这将导致整个集合的部分插入。
I've noticed the following behavior.
I have a file that is about 3MB containing several thousand rows.
In the rows I split and create prepared statement (about 250 000 statements).
What I do is:
preparedStatement
addBatch
do for every 200 rows {
executeBatch
clearBatch().
}
at the end
commit()
The memory usage will increase to around 70mb without out of memory error. Is it possible get the memory usage down? and have the transactional behavior (if one fails all fails.).
I was able to lower the memory by doing commit with the executeBatch
and clearBatch
... but this will cause a partial insert of the total set.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果一切正常,您可以将所有行插入具有相同结构的临时表中。让数据库使用以下命令将它们插入到目标表中:
insert into target (select * from temp)
。如果导入临时表失败,您还没有更改目标表中的任何内容。
编辑:固定语法
You could insert all rows into a temp table with same structure and if everything is fine. let the database insert them into to target table using:
insert into target (select * from temp)
.In case the import into the temp table fails you haven't changed anything in you target table.
EDIT: fixed syntax
您还可以使用 JDBC 2.0“批处理”功能。
connection.setAutoCommit(false)
设置 dbconnectionstatement.addBatch(sql_text_here)
将批次添加到语句中statements.executeBatch()
connection.commit()
提交它connection.rollback()
捕获异常并根据需要回滚有关回滚的异常处理的更多信息。 ..这是一个典型的回滚异常处理程序:
在这里阅读: http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html#JDBC2015
You could also use the JDBC 2.0 "batch processing" feature.
connection.setAutoCommit(false)
statement.addBatch(sql_text_here)
statement.executeBatch()
connection.commit()
connection.rollback()
More on exception handling for rollback... here is a typical rollback exception handler:
Read up here: http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html#JDBC2015