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 ...
随机推荐
- 深度学习----Xavier初始化方法
“Xavier”初始化方法是一种很有效的神经网络初始化方法,方法来源于2010年的一篇论文<Understanding the difficulty of training deep feedf ...
- tomcat原理详解
tomcat的启动是通过Bootstrap类的main方法(tomcat6开始也可以直接通过Catlina的main启动) Bootstrap的启动 Bootstrap的main方法先new了一个自己 ...
- Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- 监控中的TP50
TP指标: TP50:指在一个时间段内(如5分钟),统计该方法每次调用所消耗的时间,并将这些时间按从小到大的顺序进行排序,取第50%的那个值作为TP50 值:配置此监控指标对应的报警阀值后,需要保证在 ...
- L305 发邮件15分钟
发个邮件-不用那么纠结-把事情讲清楚就好-限制在15分钟写完-长的邮件25分钟-难点是讲清楚细节-比如软件调试bug-DFM-这里有些专业词汇 发现问题:发给客户的There are some qua ...
- day 53 练习
1 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- dubbo AdaptiveExtension
AdaptiveExtension 自适应Extension,作者其实在使用Extension方和Extension之间插入AdaptiveExtension用来自适应,也可以说是适配. 所以,我们发 ...
- Mac os fatal error: 'numpy/arrayobject.h' file not found
$ python setup.py install 出错信息如: clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g ...
- 倍增求lca
/* 节点维护的信息多样 如果用树状数组维护到根节点的边权或者点权, 可以直接插入点权和边权值,不需要预处理, 但是记得一定要使用ot[]消除影响.即差分. Housewife Wind 这个坑踩得死 ...