我想执行一些语句,并在最后提交整个批次。我尝试了以下方法:
connection.setAutoCommit(false); // Don't commit for each statement
int[] returnCodes = pstmt.executeBatch(); // Execute all statements
connection.setAutoCommit(true); // Back to normal state - future statements
// will be committed instantly
connection.commit(); // Commit our batch
失败得很惨:
java.sql.SQLException: Can't call commit when autocommit=true
--reference to the line with connection.commit()--
正确的方法是什么?是否connection.setAutoCommit(true)
提交所有已执行的批处理语句?
从
Connection.setAutoCommit
文档:但我认为它在您的代码中不是非常可读/明显。您可能应该在切换回自动提交模式之前简单地提交。使意图明确。