用于从文件中批量插入项目的 Spring jdbc 编程事务
我正在改进一个数据加载器,它从平面文件中读取数据,并使用 jdbctemplate 批量插入每 500 个项目。我正在使用 java 执行器固定线程池来提交任务,它会读取每个文件并进行批量更新。例如,当读取第一个文件时,如果在第 3 个批量插入期间失败,则需要回滚该文件的所有先前的批量插入。该任务应继续处理下一个文件并创建一个新的事务用于插入。我需要一个可以做到这一点的代码。目前,我正在使用 transactiontemplate 并将批插入代码包装在 doInTransactionwithoutcallback
中,并在 catch 块中调用 transaction status.setrollbackonly 发生异常期间。但我需要一个可以为下一个文件创建新事务的代码,无论最后一个文件是失败还是成功。设置传播到需要新解决它吗?
I am revamping a dataloader that reads from flat file and batchinsert using jdbctemplate for every 500 items. I am using java executor fixed thread pool that submits tasks, which does reading each file and batchupdate. For example when reading first file, if it fails during 3rd batchinsert ,all the previous batchinsert for this file needs to be rollbacked. The task should continue with next file and create a new transaction for insert. I needed a code that can do this . Currently I am using transactiontemplate and wrapping the batchinsert code inside doInTransactionwithoutcallback
and during exception in catch block calling transaction status.setrollbackonly. But I need a code which can create new transaction for next file irrespective of whether last file failed or succeded.setting propagation to requires new solves it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如肖恩评论的那样,你不应该重新发明整个事情,并使用 Spring Batch< /a> 相反。
Spring Batch 将允许您:
还有它已经在那里了=>已编码、已测试且很棒。
As Sean commented, you should not reinvent the whole thing, and use Spring Batch instead.
Spring Batch will allow you to:
And it is already there => coded, tested and awesome.