我正在尝试使用spring jdbc batch delete删除在3个表中从文件读取的每100条记录。如果我将逻辑包装在transactionTemplate中,它将按预期工作,例如,假设我正在创建3个批处理在300条记录中,并将逻辑包装在事务中,然后,如果第3批处理出现问题,则该事务将回滚第1批和第2批处理。我的代码包含在参考中。我正在编写下面的代码来实现上面所解释的内容,我的代码正确吗?
TransactionTemplate txnTemplate = new TransactionTemplate(txnManager);
txnTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
try {
deleteFromABCTable(jdbcTemplate, successList);
deleteFromDEFTable(jdbcTemplate, successList);
deleteFromXYZTable(jdbcTemplate, successList);
} catch (Exception e) {
status.setRollbackOnly();
throw e;
}
}
});
我的删除方法:-
private void deleteFromABCTable(JdbcTemplate jdbcTemplate, List
successList) {
try {
jdbcTemplate.batchUpdate(
"delete from ABC where document_id in (select document_id
from ABC where item in(?)))",
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setString(0, successList.get(i));
} });
} catch (Exception e) { }