在Server层实现Kill Idle Transaction
浏览:1594次 出处信息
在上一篇文章里我们写了如何针对InnoDB清理空闲事务《如何杀掉空闲事务》,在@sleebin9 的提示下,这个功能不仅可以针对InnoDB,也可以用于所有MySQL的事务引擎。
如何在Server层实现呢,sql/sql_parse.cc的do_command()函数是个好函数,连接线程会循环调用do_command()来读取并执行命令,在do_command()函数中,会调用my_net_set_read_timeout(net, thd->variables.net_wait_timeout)来设置线程socket连接超时时间,于是在这里可以下手。
主要代码:
830 /* 831 This thread will do a blocking read from the client which 832 will be interrupted when the next command is received from 833 the client, the connection is closed or "net_wait_timeout" 834 number of seconds has passed 835 */ 836 /* Add For Kill Idle Transaction By P.Linux */ 837 if (thd->active_transaction())
838 {
839 if (thd->variables.trx_idle_timeout > 0)
840 {
841 my_net_set_read_timeout(net, thd->variables.trx_idle_timeout);
842 } else if (thd->variables.trx_readonly_idle_timeout > 0 && thd->is_readonly_trx)
843 {
844 my_net_set_read_timeout(net, thd->variables.trx_readonly_idle_timeout);
845 } else if (thd->variables.trx_changes_idle_timeout > 0 && !thd->is_readonly_trx)
846 {
847 my_net_set_read_timeout(net, thd->variables.trx_changes_idle_timeout);
848 } else {
849 my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
850 }
851 } else {
852 my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
853 }
854 /* End */
大家看明白了吗?其实这是偷梁换柱,本来在这里是要设置wait_timeout的,先判断线程是不是在事务里,就可以转而实现空闲事务的超时。
trx_idle_timeout 控制所有事务的超时,优先级最高
trx_changes_idle_timeout 控制非只读事务的超时
trx_readonly_idle_timeout 控制只读事务的超时
效果:
root@localhost : (none) 08:39:49> set autocommit = 0 ;
Query OK, 0 rows affected (0.00 sec)
root@localhost : (none) 08:39:56> set trx_idle_timeout = 5;
Query OK, 0 rows affected (0.00 sec)
root@localhost : (none) 08:40:17> use perf
Database changed
root@localhost : perf 08:40:19> insert into perf (info ) values('11');
Query OK, 1 row affected (0.00 sec)
root@localhost : perf 08:40:26> select * from perf;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 6
Current database: perf
+----+------+
| id | info |
+----+------+
| 7 | aaaa |
| 9 | aaaa |
| 11 | aaaa |
+----+------+
3 rows in set (0.00 sec)
完整的patch这里下载:
server_kill_idle_trx.patch
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:一线DBA总结:MySQL搭配XFS文件系统优势最大
后一篇:MySQL 数据库性能优化之SQL优化 >>
文章信息
- 作者:P.Linux 来源: MySQLOPS 数据库与运维自动化技术分享
- 标签: Idle 空闲事务
- 发布时间:2012-01-27 18:08:39
近3天十大热文
-
[927] WordPress插件开发 -- 在插件使用 -
[126] 解决 nginx 反向代理网页首尾出现神秘字 -
[51] 如何保证一个程序在单台服务器上只有唯一实例( -
[50] 整理了一份招PHP高级工程师的面试题 -
[48] CloudSMS:免费匿名的云短信 -
[48] Innodb分表太多或者表分区太多,会导致内 -
[48] 用 Jquery 模拟 select -
[48] 全站换域名时利用nginx和javascri -
[48] 海量小文件存储 -
[46] ps 命令常见用法