当前位置:网站首页>MySQL关闭连接事务自动提交的问题
MySQL关闭连接事务自动提交的问题
2022-07-26 22:43:00 【零舍】
今天自主实验时,发现数据库关闭连接时自动提交事务的说法在实验过程中并未实现,因此查阅大量资料有以下结果。
MySQL事务默认情况下是自动提交的,当我们需要回滚事务时就需要将事务自动提交关闭,可以通过如下命令设置:
set AutoCommit = 0;当事务完成后可以通过如下命令进行提交:
commit当关闭数据库连接时,事务也会自动提交,但是此时必须将自动提交恢复为true,证明如下:
- 当数据库的自动提交为false时,更改表中数据,此时直接关闭数据库,查询表中数据不变。
表中初始数据:

更改数据库自动提交为false:
![]()
修改表中数据:
![]()
直接关闭连接:

重新连接并查询该数据:

此时,我们发现表中数据并没有改变,也就是说关闭连接时事务并没有自动提交。
- 当数据库的自动提交为false时,更改表中数据,此时将数据库的自动提交改为true,再关闭数据库连接,查询表中数据变了。
表中初始数据:

更改数据库自动提交为false:
![]()
修改表中数据:
![]()
再更改数据库自动提交为true:
![]()
关闭连接:

查询表中数据:

此时我们发现表中数据已经变更,即数据库自动提交了该事务。
通过查阅资料,查找到如下代码:
if (!skipLocalTeardown) {
if (!this.getAutoCommit() && issueRollback) {
try {
this.rollback();
} catch (SQLException var21) {
sqlEx = var21;
}
}
skipLocalTeardown和issueRollback在close调用的时候都默认是false,我们可以看到当关闭了自动提交后,关闭连接时会执行rollback而并非自动提交。于是猜想关闭连接时自动提交事务是否需要在autoCommit为true的情况下。
总结:
MySQL的数据库连接在关闭时想要自动提交事务,一定要在连接关闭前设置自动提交为true
边栏推荐
- 无重复字符的最长子串
- Simple explanation of database table connection
- ADB shell screen capture command
- Jenkins--基础--5.1--系统配置--插件管理
- Jenkins -- Basic -- 5.2 -- system configuration -- system configuration
- 短视频App开发有哪些必备的功能?
- Li Hongyi machine learning (2017 Edition)_ P3-4: Regression
- Analysis of contentvalues
- DataNode Decommision
- 深度学习报告(3)
猜你喜欢

小程序直播、连线直播、直播打赏:腾讯云移动直播组件MLVB多场景直播拓展

Understanding of Flink checkpoint source code

Simple explanation of database table connection

SQL学习(2)——表的基础查询与排序

李宏毅机器学习(2021版)_P7-9:训练技巧

Flink sliding window understanding & introduction to specific business scenarios

adb. Exe stopped working popup problem

The difference between forward and redirect

Jenkins--基础--5.3--系统配置--全局安全配置

Jenkins--基础--5.2--系统配置--系统配置
随机推荐
李宏毅机器学习(2017版)_P14:反向传播
5. 合法的括号串
Play guest cloud brush machine 5.9
SQL learning (2) -- basic query and sorting of tables
FaceNet
The basic concept of how Tencent cloud mlvb technology can highlight the siege in mobile live broadcasting services
堆排序相关知识总结
物联网改善我们生活的 6 种方式
Uni app applet app's advertising realization path: banner information flow advertising
VIS工作流 - PlantCV
adb shell截屏录屏命令
Game project export AAB package upload Google tips more than 150m solution
Flinksql window triggered in advance
什么是数字经济,它是如何改变商业模式的?
游戏项目导出AAB包上传谷歌提示超过150M的解决方案
onSaveInstanceState和onRestoreInstanceState方法的调用
Li Hongyi machine learning (2017 Edition)_ P5: error
How can Tencent cloud live plug-in mlvb make use of these advantages to become the divine assistant of anchor Live Streaming?
Scala pattern matching
短视频App开发有哪些必备的功能?