MySQL Transaction--事务无法正常回滚导致的异常
问题表现:系统增删改操作明显变慢(由原来的几十毫秒变为几十秒)
查看未提交事务
## 查看未提交的事务 ##
SELECT
p.ID,
P.USER,
P.HOST,
p.DB,
P.TIME,
T.trx_started,
T.trx_isolation_level,
T.trx_tables_locked,
T.trx_rows_locked,
t.trx_state,
p.COMMAND AS process_state
FROM `information_schema`.`INNODB_TRX` t
INNER JOIN `information_schema`.`PROCESSLIST` p
ON t.trx_mysql_thread_id=p.id
WHERE t.trx_state='RUNNING'
AND p.COMMAND='Sleep'
ORDER BY T.trx_started ASC \G
发现无未提交事务。
查看执行时间较长的进程
SELECT *
FROM information_schema.processlist
WHERE TIME>10 and
COMMAND NOT IN('Binlog Dump')
ORDER BY TIME DESC LIMIT 10;
未发现异常
PS1: processlist表中的TIME字段是指进程处理目前状态的时间,而不是进程开始执行到现在的时间。
查看事务版本Purge情况
mysql_exe="/export/servers/mysql/bin/mysql"
mysql_host="127.0.0.1"
mysql_port=3358
user_name="root"
user_psw="root.psw" for i in $(seq 1 10)
do echo `date +"%Y-%m-%d %T"` ( ${mysql_exe} \
--host="${mysql_host}" \
--port=$mysql_port \
--user="${user_name}" \
--password="${user_psw}" \
--execute="show engine innodb status \G" \
| egrep 'History list length|Purge done for trx' ) 2>/dev/null sleep 3 done
执行结果为:
2019-04-08 21:36:41
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2494727
2019-04-08 21:36:44
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2494863
2019-04-08 21:36:47
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495019
2019-04-08 21:36:50
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495149
2019-04-08 21:36:53
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495292
2019-04-08 21:36:56
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495394
2019-04-08 21:36:59
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495464
2019-04-08 21:37:02
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495624
2019-04-08 21:37:05
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495709
2019-04-08 21:37:08
Purge done for trx's n:o < 2291233765 undo n:o < 3126066 state: running
History list length 2495905
从上面可看出,“Purge done for trx's n:o”一直未变,而“History list length”在缓慢增长。
查看当前事务状态
SELECT
trx_id,trx_state,trx_operation_state,
trx_isolation_level,trx_started
FROM information_schema.INNODB_TRX
ORDER BY trx_started;
查询结果:
+-----------------+--------------+---------------------+---------------------+---------------------+
| trx_id | trx_state | trx_operation_state | trx_isolation_level | trx_started |
+-----------------+--------------+---------------------+---------------------+---------------------+
| 2291587663 | ROLLING BACK | rollback | REPEATABLE READ | 2019-04-08 12:31:59 |
| 421571830792992 | RUNNING | fetching rows | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830788432 | RUNNING | fetching rows | READ COMMITTED | 2019-04-08 21:42:02 |
| 421571830855920 | RUNNING | NULL | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830721856 | RUNNING | fetching rows | READ COMMITTED | 2019-04-08 21:42:02 |
| 421571830734624 | RUNNING | fetching rows | READ COMMITTED | 2019-04-08 21:42:00 |
| 421571830658016 | RUNNING | starting index read | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830851360 | RUNNING | starting index read | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571831050176 | RUNNING | starting index read | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830975392 | RUNNING | starting index read | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830783872 | RUNNING | NULL | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830789344 | RUNNING | NULL | READ COMMITTED | 2019-04-08 21:42:06 |
| 421571830741920 | RUNNING | fetching rows | READ COMMITTED | 2019-04-08 21:42:05 |
| 421571830800288 | RUNNING | fetching rows | READ COMMITTED | 2019-04-08 21:42:04 |
+-----------------+--------------+---------------------+---------------------+---------------------+
其中有一条事务处开始于8个小时前并长期处于于"rollback"状态。
通过" SHOW ENGINE INNODB STATUS \G " 命令也能看到该事务信息:
---TRANSACTION 2291587663, ACTIVE 28172 sec rollback
ROLLING BACK 10670 lock struct(s), heap size 1089744, 312287 row lock(s), undo log entries 8953223
MySQL thread id 27564734, OS thread handle 139960483022592, query id 175236062467 172.20.186.43 user_rw1
查看BINLOG信息
ll -h --time-style='+%Y-%m-%d %H:%M:%S' /export/data/mysql/data/mysql-bin*

在最后写入时间12:31:57分的binlog中,应该包含超大事务,导致异常。
问题原因
在数据库开发中,“将多个操作合并到一个事务“和“将多个操作拆分到多个事务”是两个对立的操作,需要根据业务需要和场景决定如何使用,在上面场景中,研发同事将大量DML操作( 超过1000行记录修改)封装为一个超大事务中,并且在事务处理过程中引入其他非数据库操作,导致整个事务周期较长,同时检查业务线程执行时间的作业按照processlist表中TIME字段判断线程情况,导致这种超大事务没有被及时KILL,当问题爆发后,超大事务被KILL,但回滚操作需要很长时间。
MySQL Transaction--事务无法正常回滚导致的异常的更多相关文章
- Avoid catching exceptions inside atomic! You may need to manually revert model state when rolling back a transaction. 避免异常程序不抛错误 回滚 导致 自增id不连续。
https://docs.djangoproject.com/en/3.0/topics/db/transactions/ You may need to manually revert model ...
- mysql transaction 事务
1.事务简介 一个"最小的"不可再分的"工作单元". 一个事务通常对应了一个完整的业务.如:银行的转账功能,a转账给b,a扣钱,b加钱. 一个事务包含一条或多条 ...
- SSM保姆级从创建项目到使用,包括事务和设置回滚
1. 简介 Spring 和 Mybaits整合 2. 创建项目 负责将代理类记性扫描,扫描的是Mapper接口所在的包,这个是mybatis提供的,所以会去找SqlSessionFactory 2. ...
- EF Core利用Transaction对数据进行回滚保护
What? 首先,说一下什么是EF Core中的Transaction Transaction允许以原子方式处理多个数据库操作,如果事务已提交,则所有操作都应用于数据库,如果事务回滚,则没有任何操作应 ...
- Spring事务注解@Transactional回滚问题
Spring配置文件,声明事务时,如果rollback-for属性没有指定异常或者默认不写:经测试事务只回滚运行时异常(RuntimeException)和错误(Error). <!-- 配置事 ...
- Spring事务控制和回滚
1在一个项目中ssh结构,spring2.5,事务控制采用的是tx拦截器的方式. 自己写了个 int a=1/0;异常抛出了,但是事务还是提交了,怎么搞都不行. 现将看到的一些事务控制总结下来: 事务 ...
- NESTED内部事务异常会回滚 外部事务不会回滚 ;内部事务没有异常,外部事务有异常 则整体事务都回滚
NESTED内部事务异常会回滚 外部事务不会回滚 :内部事务没有异常,外部事务有异常 则整体事务都回滚
- nestd事务如果报错了 则回滚到外部事物保存点 且外部事物如果没异常的话 会正常提交 nested事务并不会提交;如果外部事物报错了 内部事务会一同回滚
nestd事务如果报错了 则回滚到外部事物保存点 且外部事物如果没异常的话 会正常提交 nested事务并不会提交:如果外部事物报错了 内部事务会一同回滚
- MySQL Replication--修改主键为NULL导致的异常
测试环境:MySQL 5.5.14/MySQL 5.6.36 测试脚本: create table tb001(id int primary key,c1 int); alter table tb00 ...
随机推荐
- 哈希表概念和实现,C/C++实现
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 1.3 CPU简介
目录 CPU的功能模块 cpu总线 CPU寄存器 16位cpu的寄存器组 32位cpu的寄存器组 64位cpu的寄存器组 CPU的功能模块 CPU从逻辑上可以划分成3个模块,分别是控制单元.运算单元和 ...
- node(3)Buffer缓冲区
buffer 专门用来存放二进制数据的缓冲区:处理文件流 TCP流 const buf = Buffer.from('runoob', 'ascii'); // 创建一个长度为 10.且用 0x1 填 ...
- idea自动生成serialVersionUID(转)
原文链接:http://blog.sina.com.cn/s/blog_54b09dc90101d9bu.html Setting->Plugins 找到一个叫 GenerateSerialV ...
- java.lang.String 使用介绍
这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关的知识点,包括字符串常量池.StringBuffer.StringBuilder,以及equals和==的用法 ...
- webpack进阶构建项目(一):1.理解webpack加载器
1.理解webpack加载器 webpack的设计理念,所有资源都是“模块”,webpack内部实现了一套资源加载机制,这与Requirejs.Sea.js.Browserify等实现有所不同. We ...
- 互评Final版本
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2478] 基于NABCD评论作品,及改进建议 杨老师粉丝群.作品:<Pin ...
- L295 how to turn down a job but keep a good relationship with the hiring manager
Let’s say you’re on the hunt for a new job. Three interviews in, you realize it’s not the place for ...
- L271 操纵太空中航天器的几种方法
Manoeuvring a satellite in orbit usually requires thrusters. Sometimes the thrust is provided by a f ...
- L248 词汇题 2006
The audience, hostile at first, were greatly impressed by her excellent performance. He wanted to st ...