Mysql中autocommit的用法
定义#
Mysql文档原文:SET autocommit disables or enables the default autocommit mode for the current session. Autocommit is a session variable and must be set for each session.
By default, MySQL runs with autocommit mode enabled.
该变量为全局与会话变量,默认值为1,表示自动提交事务。autocommit控制当前会话是否自动提交事务。
If set to 1, all changes to a table take effect immediately. If set to 0, you must use COMMIT to accept a transaction or ROLLBACK to cancel it. If autocommit is 0 and you change it to 1, MySQL performs an automatic COMMIT of any open transaction. Another way to begin a transaction is to use a START TRANSACTION or BEGIN statement.
如果设置为1,对一个表的所有改变立即生效。
如果设置为0,你必须使用COMMIT去提交事务或者用ROLLBACK来回滚事务。
如果autocommit为0,你修改其为1,Mysql对任何开放的事务进行自动提交。
另一个开启一个事务的方式是:使用START TRANSACTION或者BEGIN。
查看方法#
SHOW VARIABLES LIKE 'autocommit';
会话内修改方法#
SET autocommit = {0 | 1}
关闭自动提交:SET autocommit=0;
打开自动提交:SET autocommit=1;
全局修改方法#
By default, client connections begin with autocommit set to 1. To cause clients to begin with a default of 0, set the global autocommit value by starting the server with the --autocommit=0 option. To set the variable using an option file, include these lines:
默认情况下下,客户端连接默认autocommit为1. 如果希望客户端一连接即默认autocommit为0,设置全局的autocommit通过以下方式:
*启动服务器时携带--autocommit=0选项
*修改option文件如下:
用法展示#
2.开启会话1修改
START TRANSACTION;
UPDATE
employees.
salaries SET salary = 200053 WHERE emp_no = 10001;
SELECT * FROM
employees.
salaries WHERE emp_no = 10001;
复杂用法链接#
(https://dev.mysql.com/doc/refman/5.7/en/commit.html)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端