详解MySQL与Spring的自动提交(autocommit)

作者:瞩目学码 时间:2024-01-26 15:19:02 

1 MySQL的autocommit设置

MySQL默认是开启自动提交的,即每一条DML(增删改)语句都会被作为一个单独的事务进行隐式提交。如果修改为关闭状态,则执行DML语句之后要手动提交 才能生效。
查询当前会话的自动提交是否开启:


mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit  | ON  |
+---------------+-------+

查询全局的自动提交是否开启:


mysql> show global variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit  | ON  |
+---------------+-------+

通过修改autocommit变量可以关闭和开启操作


关闭当前会话的自动提交模式
mysql> set autocommit=0;

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit  | OFF  |
+---------------+-------+

全局的autocommit还是开启状态
mysql> show global variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit  | ON  |
+---------------+-------+

关闭全局的autocommit
mysql> set global autocommit=0;

mysql> show global variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit  | OFF  |
+---------------+-------+

如果想要MySQL服务重启之后仍能生效,需要设置系统环境变量。MySQL5.7 在cnf配置文件中[mysqld]下面设置autocommit的值。


[mysqld]
...
autocommit=0

Spring中对自动提交的控制

MySQL的JDBC驱动包 mysql-connector-java 会给会话的connection默认开启自动提交,譬如 mysql-connector-java-8.0.22版本的代码:


//com.mysql.cj.protocol.a.NativeServerSession.java
 private boolean autoCommit = true;

常用的数据库连接池 如HikariCP,druid等,默认也是开启自动提交,会将connection的自动提交设置都改为true。
druid在初始化DataSource的时候设置connection的autocommit为true。代码如下:


com.alibaba.druid.pool.DruidAbstractDataSource.java
 protected volatile boolean             defaultAutoCommit             = true;
 ...
 public void initPhysicalConnection(Connection conn, Map<String, Object> variables, Map<String, Object> globalVariables) throws SQLException {
   if (conn.getAutoCommit() != defaultAutoCommit) {
     //将connection的autocommit设置为true
     conn.setAutoCommit(defaultAutoCommit);
   }
   ...

}

HikariCP 初始化DataSource的默认配置 中autocommit也是true:


com.zaxxer.hikari.HikariConfig.java
 public HikariConfig()
 {
  ...
  isAutoCommit = true;
 }

对于事务管理器PlatformTransactionManager管理的显式事务(譬如@Transactional注解声明)在 开启事务时会关闭自动提交模式。 代码如下:


@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
Connection con = null;

try {
     ........

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

.......
}

catch (Throwable ex) {
    .......
}
}

总结

MySQL的autocommit模式默认是打开状态,为了防止手动的DML操作导致失误,生产环境可以设置为默认关闭的状态。一般的jdbc 连接池默认都是开启状态,而且是可配置的。显式事务下会设置成关闭状态,单纯的修改数据库环境的autocommit不会对代码的行为产生影响。

来源:https://juejin.cn/post/6916415803852062734

标签:MySQL,自动提交,autocommit,spring
0
投稿

猜你喜欢

  • Python脚本破解压缩文件口令实例教程(zipfile)

    2023-03-14 08:31:08
  • 用python画圣诞树三种代码示例介绍

    2023-03-24 08:15:01
  • 如何解决ORA-01843与NLS_DATE_FORMAT问题

    2023-06-30 20:57:14
  • SQL Server 分页查询通用存储过程(只做分页查询用)

    2024-01-12 20:10:11
  • 聊聊Javascript中try catch的2个作用

    2024-04-22 13:25:57
  • pandas学习之txt与sql文件的基本操作指南

    2022-08-03 18:57:50
  • JS中FormData类实现文件上传

    2024-04-10 10:50:10
  • vue+element实现图片上传及裁剪功能

    2024-05-29 22:22:12
  • 运行asp.net程序 报错:磁盘空间不足

    2024-01-16 23:48:21
  • Python selenium把歌词评论做成词云图

    2022-12-29 10:17:35
  • 帮助你分析MySQL的数据类型以及建库策略

    2009-02-23 17:39:00
  • 以独占方式打开Access数据库

    2007-10-22 12:24:00
  • Python检测网络延迟的代码

    2023-03-26 08:36:31
  • 解决Django加载静态资源失败的问题

    2023-09-11 21:10:51
  • sql语句中单引号嵌套问题(一定要避免直接嵌套)

    2024-01-16 14:48:52
  • MySQL查询缓存优化示例详析

    2024-01-27 12:21:32
  • mysql中#{}和${}的区别详解

    2024-01-12 21:37:17
  • 详解MybatisPlus集成nacos导致druid连接不上数据库

    2024-01-18 02:57:09
  • 实现像php一样方便的go ORM数据库操作示例详解

    2024-05-05 09:27:25
  • python numpy.linalg.norm函数的使用及说明

    2022-05-27 10:20:32
  • asp之家 网络编程 m.aspxhome.com