Spring @Transactional和JDBC autoCommit [英] Spring @Transactional and JDBC autoCommit

查看:636
本文介绍了Spring @Transactional和JDBC autoCommit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On my actual application, I have a DBCP connection pool which doesn't have JDBC autoCommit=false set. It seems to have the default autoCommit=true. This is probably a mistake but I'd like to understand the impact of changing this parameter.

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

I am using: - Spring with @Transactional annotation - Spring Batch with JDBC readers and writers, eventually custom tasklets using JdbcTemplate

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

I would like to know if Spring does set autoCommit=false on the current connection if it is in the context of a transaction handled by the TransactionManager. Does it override the default setting? Because it seems to me it makes sense to do so.

推荐答案

PlatformTransactionManager是一个接口,因此我不会一味地说所有实现都将AutoCommit = false设置为有效,但是最常见的实现(DataSourceTransactionManager)确实将AutoCommit = false设置为.请从doBegin方法中查看下面的代码段:

PlatformTransactionManager is an interface, so I would not blanket say that all implementations set AutoCommit = false, however the most common implementation (DataSourceTransactionManager) does set AutoCommit = false. see code snippet below from the doBegin method:

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);

现在,正如您所说,这样做非常有道理,否则您将没有回滚段来激活回滚.

Now as you stated, it makes perfect sense to do so or you would not have a rollback segment to activate a rollback on.

这篇关于Spring @Transactional和JDBC autoCommit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆