在executeBatch()之后需要connection.commit()吗? [英] do I need a connection.commit() after executeBatch()?
问题描述
我必须检查一位同事的代码,我偶然发现了这段代码:
I have to check the code of a fellow coworker and I stumble on this piece of code:
private void pdate(JdbcTemplate jdbcTemplate, List<Long> saisineIdsToUpdate,Connection connection) throws SQLException {
String sqlUpdate = "UPDATE SAISINES SAI WHERE SAI.IDSAISINE = ?"; //request simplified
PreparedStatement psUpdate = connection.prepareStatement(sqlUpdate);
for (Long saisineId : saisineIdsToUpdate) {
psUpdate.setLong(1, saisineId );
psUpdate.addBatch();
}
psUpdate.executeBatch();
psUpdate.close();
代码有效,更新正确完成,但我找不到<$ c的跟踪$ c> connection.commit();
我想知道它如何在没有提交的情况下工作 - 有人可以解释原因吗?
The code works, the updates are done correctly, but I cannot find the trace of a connection.commit();
I wonder how it can work without the commit - could someone explain why ?
推荐答案
如此处所述,JDBC驱动程序通常使用autocommit,您可以通过DBMS驱动程序特定设置启用数据库跟踪,例如JPA中的 showSQL
或 generateDDL
。 / p>
As explained here, JDBC-drivers commonly use autocommit, you can enable database-traces via DBMS-driver specific settings like showSQL
or generateDDL
in JPA.
要启用手动事务支持而不是JDBC驱动程序默认使用的自动提交模式
,请使用Connection对象
setAutoCommit()方法。如果将boolean false传递给setAutoCommit(
),则关闭自动提交。你可以传递一个布尔值true来重新打开
。
To enable manual- transaction support instead of the auto-commit mode that the JDBC driver uses by default, use the Connection object's setAutoCommit() method. If you pass a boolean false to setAutoCommit( ), you turn off auto-commit. You can pass a boolean true to turn it back on again.
这篇关于在executeBatch()之后需要connection.commit()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!