Spring @Transactional和JDBC autoCommit

Seb*_*ber 12 spring jdbc spring-batch

在我的实际应用程序中,我有一个DBCP连接池,它没有设置JDBC autoCommit = false.它似乎有默认的autoCommit = true.这可能是一个错误,但我想了解更改此参数的影响.

我正在使用: - 带有@Transactional注释的Spring - 带有JDBC阅读器和编写器的Spring Batch,最终使用JdbcTemplate自定义tasklet

我想知道Spring是否在当前连接上设置autoCommit = false,如果它在TransactionManager处理的事务的上下文中.它是否覆盖默认设置?因为在我看来这样做是有意义的.

fpm*_*les 13

PlatformTransactionManager是一个接口,所以我不会说所有实现都设置AutoCommit = false,但是最常见的实现(DataSourceTransactionManager)确实设置了AutoCommit = false.请参阅下面的doBegin方法的代码片段:

if (con.getAutoCommit()) {
            txObject.setMustRestoreAutoCommit(true);
            if (logger.isDebugEnabled()) {
                logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
            }
            con.setAutoCommit(false);
        }
        txObject.getConnectionHolder().setTransactionActive(true);
Run Code Online (Sandbox Code Playgroud)

现在正如你所说,这样做是完全合理的,否则你就不会有回滚段来激活回滚.