mysql 设置事物自动提交_mysql事务自动提交的问题

1:mysql的aut0commit配置默认是开启的,也就是没执行一条sql都会提交一次,就算显示的开启事务也会导致多条SQL不在一个事务中,

如果需要相关的SQL在同一个事务中执行,那么必须将autocommit设置为OFF,再显式开启事务。

2:如果使用spring的事务,那么不存在这个问题,spring的事务默认是关闭自动提交的,做法是判断连接池是否开启事务自动提交,如果连接池开启自动提交则设置自动提交为关闭,否则不做操作,因为某些jdbc驱动做设置自动提交关闭代价昂贵。

/**

* This implementation sets the isolation level but ignores the timeout.

*/

@Override

protected void doBegin(Object transaction, TransactionDefinition definition) {

.........省略某些代码

// Switch to manual commit if necessary. This is very expensive in some JDBC drivers,

// so we don't want to do it unnecessarily (for example if we've explicitly

// configured the connection pool to set it already).

if (con.getAutoCommit()) {

txObject.setMustRestoreAutoCommit(true);

if (logger.isDebugEnabled()) {

logger.debug("Switching JDBC Connection [" + con + "] to manual commit");

}

con.setAutoCommit(false);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值