问题表现:系统增删改操作明显变慢(由原来的几十毫秒变为几十秒)

查看未提交事务

## 查看未提交的事务 ##
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--事务无法正常回滚导致的异常的更多相关文章

  1. 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 ...

  2. mysql transaction 事务

    1.事务简介 一个"最小的"不可再分的"工作单元". 一个事务通常对应了一个完整的业务.如:银行的转账功能,a转账给b,a扣钱,b加钱. 一个事务包含一条或多条 ...

  3. SSM保姆级从创建项目到使用,包括事务和设置回滚

    1. 简介 Spring 和 Mybaits整合 2. 创建项目 负责将代理类记性扫描,扫描的是Mapper接口所在的包,这个是mybatis提供的,所以会去找SqlSessionFactory 2. ...

  4. EF Core利用Transaction对数据进行回滚保护

    What? 首先,说一下什么是EF Core中的Transaction Transaction允许以原子方式处理多个数据库操作,如果事务已提交,则所有操作都应用于数据库,如果事务回滚,则没有任何操作应 ...

  5. Spring事务注解@Transactional回滚问题

    Spring配置文件,声明事务时,如果rollback-for属性没有指定异常或者默认不写:经测试事务只回滚运行时异常(RuntimeException)和错误(Error). <!-- 配置事 ...

  6. Spring事务控制和回滚

    1在一个项目中ssh结构,spring2.5,事务控制采用的是tx拦截器的方式. 自己写了个 int a=1/0;异常抛出了,但是事务还是提交了,怎么搞都不行. 现将看到的一些事务控制总结下来: 事务 ...

  7. NESTED内部事务异常会回滚 外部事务不会回滚 ;内部事务没有异常,外部事务有异常 则整体事务都回滚

    NESTED内部事务异常会回滚 外部事务不会回滚 :内部事务没有异常,外部事务有异常 则整体事务都回滚

  8. nestd事务如果报错了 则回滚到外部事物保存点 且外部事物如果没异常的话 会正常提交 nested事务并不会提交;如果外部事物报错了 内部事务会一同回滚

    nestd事务如果报错了 则回滚到外部事物保存点 且外部事物如果没异常的话 会正常提交 nested事务并不会提交:如果外部事物报错了 内部事务会一同回滚

  9. MySQL Replication--修改主键为NULL导致的异常

    测试环境:MySQL 5.5.14/MySQL 5.6.36 测试脚本: create table tb001(id int primary key,c1 int); alter table tb00 ...

随机推荐

  1. vue-router-1-安装与基本使用

    npm install vue-router import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) / ...

  2. Ajax 以及 前端JSP页面如何查看数值

    $.ajax({ url: ctx + "/unit/rsdl/qyjy/getDljgCode", type: "post", success: functi ...

  3. Linux:Apache安装与启动

    Apache安装与启动 1.查看是否安装:rpm -qa| grep httpd2.挂载系统 mount /dev/cdrom /mnt/cdrom3.进入软件包 cd /mnt/cdrom/Pack ...

  4. 句柄线程做参数和PostMessage的用法

    当我们启动一个线程,并且要给线程函数传递的参数是窗口句柄时,我们应该这样做: HWND hHwnd = GetSafeHwnd(); HANDLE hThread; DWORd dwThreadId; ...

  5. UIIimageView读取图片的两种方式及动画的执行

    /**count:图片数量 name:图片名称*/ - (void)runAnimationWithCount:(int)count name:(NSString *)name { if(self.t ...

  6. django面试八

    1. 对Django的认识? #1.Django是走大而全的方向,它最出名的是其全自动化的管理后台:只需要使用起ORM,做简单的对象定义,它就能自动生成数据库结构.以及全功能的管理后台. #2.Dja ...

  7. DHCP服务配置

    DHCP(Dynamic Host Configuration Protocol)动态主机配置协议 -->是由Internet工作任务小组设计开发的,专用于对TCP/IP网络中的计算机自定分配T ...

  8. python day06 作业答案

    1. count=1 while count<11: fen=input('请第{}个评委打分' .format( count)) if int(fen) >5 and int(fen) ...

  9. FZU 2272 Frog 第八届福建省赛 (鸡兔同笼水题)

    Problem Description Therearex frogs and y chicken in a garden. Kim found there are n heads and m leg ...

  10. 路由器DHCP服务及DHCP中继

    实验要求:掌握路由配置DHCP服务配置 拓扑如下: R1enable 进入特权模式config terminal   进入全局模式interface s0/0/0 进入端口ip address 192 ...