Deadlock found when trying to get lock; try restarting transaction解决方式

MySQL 事务的学习整理:[url]http://blog.youkuaiyun.com/mchdba/article/details/12242685[/url]
mysql事务处理用法与实例详解:[url]http://www.cnblogs.com/ymy124/p/3718439.html[/url]
常见的表死锁情况及解决方法:[url]http://www.cnblogs.com/jeffry/p/6014881.html[/url]
MySQL事务autocommit自动提交:[url]http://www.qttc.net/201208175.html[/url]
MySql 死锁时的一种解决办法:[url]http://www.cnblogs.com/farb/p/MySqlDeadLockOneOfSolutions.html[/url]
Mysql并发时经典常见的死锁原因及解决方法:[url]http://www.cnblogs.com/zejin2008/p/5262751.html[/url]
mysql死锁几种情况的测试:[url]http://www.2cto.com/database/201605/507289.html[/url],
[url]http://blog.youkuaiyun.com/aoerqileng/article/details/51354357[/url]
Mysql中那些锁机制之InnoDB:[url]http://www.2cto.com/database/201508/429967.html[/url]
InnoDB Record, Gap, and Next-Key Locks:[url]http://www.cnblogs.com/zemliu/p/3503496.html[/url]

[b]准备工作:[/b]
建表,初始化数据,

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment COMMENT 'id',
`name` varchar(10) default NULL,
`age` int(11) default NULL,
`registerTime` timestamp NULL default NULL on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'jack', '23', null);
INSERT INTO `user` VALUES ('2', 'mark', '67', null);
INSERT INTO `user` VALUES ('3', 'donald', null, '2017-06-13 16:23:23');

[b]模拟死锁:[/b]
开启会话A,开启一个事务
Session A:
mysql> begin;
Query OK, 0 rows affected

mysql> select * from user where id=1 lock in share mode;;
+----+------+-----+--------------+
| id | name | age | registerTime |
+----+------+-----+--------------+
| 1 | jack | 23 | NULL |
+----+------+-----+--------------+
1 row in set


开启会话B,删除用户id为1的用户
Session B:
mysql> delete from user where id = 1;


回到会话A,删除用户id为1的用户
Session A:
mysql> mysql> delete from user where id = 1;
1213 - Deadlock found when trying to get lock; try restarting transaction



[b]0.查看MySQL当前连接线程:[/b]

mysql> show processlist;
+---------+-----------------+-------------------+------+---------+---------+------------------------+------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+---------+-----------------+-------------------+------+---------+---------+------------------------+------------------------------+
| 1 | event_scheduler | localhost | NULL | Daemon | 3031152 | Waiting on empty queue | NULL |
| 1019932 | donald | 192.168.31.153:15217 | NULL | Sleep | 1872 | | NULL |
| 1019933 | donald | 192.168.31.153:15218 | test | Sleep | 188 | | NULL |
| 1020542 | donald | 192.168.31.153:16735 | test | Sleep | 179 | | NULL |
| 1020543 | donald | 192.168.31.153:16748 | test | Sleep | 14 | | NULL |
| 1020544 | donald | 192.168.31.153:16751 | test | Query | 72 | updating | delete from user where id =1 |
| 1020545 | donald | 192.168.31.153:16753 | test | Query | 0 | init | show processlist |
+---------+-----------------+-------------------+------+---------+---------+------------------------+------------------------------+
7 rows in set



mysql>
[b]1.查看引擎日志分析死锁的原因:[/b]
show engine innodb status\G;

查看死锁信息

| InnoDB |      |
=====================================
2017-07-18 18:25:02 650ceb70 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 6 seconds
-----------------
BACKGROUND THREAD 后台线程
-----------------
srv_master_thread loops: 530 srv_active, 0 srv_shutdown, 3056084 srv_idle
srv_master_thread log flush and writes: 3027072
----------
SEMAPHORES 信号量
----------
OS WAIT ARRAY INFO: reservation count 1059
OS WAIT ARRAY INFO: signal count 1047
Mutex spin waits 1035, rounds 8167, OS waits 165
RW-shared spins 870, rounds 26073, OS waits 867
RW-excl spins 33, rounds 1021, OS waits 25
Spin rounds per wait: 7.89 mutex, 29.97 RW-shared, 30.94 RW-excl
------------------------
LATEST DETECTED DEADLOCK 上次探测到死锁的状态
------------------------
2017-07-18 18:16:22 698d9b70
*** (1) TRANSACTION:事务1035410,MySQL事务线程1020087
TRANSACTION 1035410, ACTIVE 73 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 312, 1 row lock(s)
MySQL thread id 1020087, OS thread handle 0x69879b70, query id 195420 192.168.31.153 donald updating
delete from user where id = 1
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:等待锁行级锁RECORD LOCKS,X锁,
RECORD LOCKS space id 7405 page no 3 n bits 72 index `PRIMARY` of table `test`.`user`
trx id 1035410 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 32
0: len 4; hex 80000001; asc ;;
1: len 6; hex 0000000fcc91; asc ;;
2: len 7; hex 68000080210a23; asc h ! #;;
3: len 4; hex 6a61636b; asc jack;;
4: len 4; hex 80000017; asc ;;
5: SQL NULL;

*** (2) TRANSACTION:事务1035409,MySQL事务线程1020088
TRANSACTION 1035409, ACTIVE 107 sec starting index read
mysql tables in use 1, locked 1
3 lock struct(s), heap size 312, 2 row lock(s), undo log entries 1
MySQL thread id 1020088, OS thread handle 0x698d9b70, query id 195422 192.168.31.153 donald Sending data
select * from user where id =1 lock in share mode
*** (2) HOLDS THE LOCK(S):当前事务持有锁,S锁,
RECORD LOCKS space id 7405 page no 3 n bits 72 index `PRIMARY` of table `test`.`user`
trx id 1035409 lock_mode X locks rec but not gapRecord lock, 非gap锁
heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 32
0: len 4; hex 80000001; asc ;;
1: len 6; hex 0000000fcc91; asc ;;
2: len 7; hex 68000080210a23; asc h ! #;;
3: len 4; hex 6a61636b; asc jack;;
4: len 4; hex 80000017; asc ;;
5: SQL NULL;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 7405 page no 3 n bits 72 index `PRIMARY` of table `test`.`user`
trx id 1035409 lock mode S waiting Record lock, heap no 2 PHYSICAL RECORD:
n_fields 6; compact format; info bits 32
0: len 4; hex 80000001; asc ;;
1: len 6; hex 0000000fcc91; asc ;;
2: len 7; hex 68000080210a23; asc h ! #;;
3: len 4; hex 6a61636b; asc jack;;
4: len 4; hex 80000017; asc ;;
5: SQL NULL;

*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS 回滚事务1035425
------------
Trx id counter 1035425
Purge done for trx's n:o < 1035408 undo n:o < 0 state: running but idle
History list length 1046
LIST OF TRANSACTIONS FOR EACH SESSION:每个会话的事务
---TRANSACTION 0, not started,
MySQL thread id 1020076, OS thread handle 0x650ceb70, query id 195466 192.168.31.153 donald init
show engine innodb status
---TRANSACTION 1035423, not started
MySQL thread id 1020089, OS thread handle 0x6503bb70, query id 195454 192.168.31.153 donald cleaning up
---TRANSACTION 1035424, not started
MySQL thread id 1020087, OS thread handle 0x69879b70, query id 195455 192.168.31.153 donald cleaning up
---TRANSACTION 0, not started
MySQL thread id 1019933, OS thread handle 0x6506cb70, query id 195428 192.168.31.153 donald cleaning up
---TRANSACTION 1035409, ACTIVE 627 sec 事务1035409,激活状态,MySQL线程id,1020088
3 lock struct(s), heap size 312, 5 row lock(s), undo log entries 1
MySQL thread id 1020088, OS thread handle 0x698d9b70, query id 195456 192.168.31.153 donald cleaning up
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
3034 OS file reads, 46735 OS file writes, 12238 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 26781, seg size 26783, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 9239933, node heap has 24 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 709253455618
Log flushed up to 709253455618
Pages flushed up to 709253455618
Last checkpoint at 709253455618
0 pending log writes, 0 pending chkp writes
3279 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 2136604672; in additional pool allocated 0
Dictionary memory allocated 745414
Buffer pool size 128000
Free buffers 93253
Database pages 34723
Old database pages 12657
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 1, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 2779, created 31944, written 37451
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 34723, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size 16000
Free buffers 11538
Database pages 4459
Old database pages 1625
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 387, created 4072, written 5592
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4459, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size 16000
Free buffers 11712
Database pages 4285
Old database pages 1563
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 373, created 3912, written 4091
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4285, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size 16000
Free buffers 11728
Database pages 4269
Old database pages 1557
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 283, created 3986, written 4206
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4269, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 3
...
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 4
....
---BUFFER POOL 5
...
---BUFFER POOL 6
....
---BUFFER POOL 7
....
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Main thread process no. 4472, id 1741499248, state: sleeping
Number of rows inserted 1222447, updated 48, deleted 1745, read 61951636
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================


[b]2.查看Mysql事务:[/b]
2.a 查看当前事务:

mysql> select * from information_schema.INNODB_TRX;
+---------+-----------+---------------------+-----------------------+---------------------+------------+---------------------+-------------------------------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
| trx_id | trx_state | trx_started | trx_requested_lock_id | trx_wait_started | trx_weight | trx_mysql_thread_id | trx_query | trx_operation_state | trx_tables_in_use | trx_tables_locked | trx_lock_structs | trx_lock_memory_bytes | trx_rows_locked | trx_rows_modified | trx_concurrency_tickets | trx_isolation_level | trx_unique_checks | trx_foreign_key_checks | trx_last_foreign_key_error | trx_adaptive_hash_latched | trx_adaptive_hash_timeout | trx_is_read_only | trx_autocommit_non_locking |
+---------+-----------+---------------------+-----------------------+---------------------+------------+---------------------+-------------------------------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
| 1035424 | LOCK WAIT | 2017-07-18 18:17:43 | 1035424:7405:3:2 | 2017-07-18 18:17:43 | 2 | 1020087 | delete from user where id = 1 | starting index read | 1 | 1 | 2 | 312 | 1 | 0 | 0 | REPEATABLE READ | 1 | 1 | NULL | 0 | 10000 | 0 | 0 |
| 1035409 | RUNNING | 2017-07-18 18:14:35 | NULL | NULL | 4 | 1020088 | NULL | NULL | 0 | 0 | 3 | 312 | 5 | 1 | 0 | REPEATABLE READ | 1 | 1 | NULL | 0 | 10000 | 0 | 0 |
+---------+-----------+---------------------+-----------------------+---------------------+------------+---------------------+-------------------------------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
2 rows in set

mysql>

2.b 查看当前锁定的事务

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
+------------------+-------------+-----------+-----------+---------------+------------+------------+-----------+----------+-----------+
| lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data |
+------------------+-------------+-----------+-----------+---------------+------------+------------+-----------+----------+-----------+
| 1035424:7405:3:2 | 1035424 | X | RECORD | `test`.`user` | PRIMARY | 7405 | 3 | 2 | 1 |
| 1035409:7405:3:2 | 1035409 | S | RECORD | `test`.`user` | PRIMARY | 7405 | 3 | 2 | 1 |
+------------------+-------------+-----------+-----------+---------------+------------+------------+-----------+----------+-----------+
2 rows in set


2.c 查看当前等锁的事务
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS; 
+-------------------+-------------------+-----------------+------------------+
| requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
+-------------------+-------------------+-----------------+------------------+
| 1035424 | 1035424:7405:3:2 | 1035409 | 1035409:7405:3:2 |
| 1035424 | 1035424:7405:3:2 | 1035409 | 1035409:7405:3:2 |
+-------------------+-------------------+-----------------+------------------+
2 rows in set



找出死锁关联的事务线程id(trx_mysql_thread_id),从上面的分析得出,持有锁的MySQL事务线程id为1020088


[b]3.Kill 关联事务线程[/b]

mysql> kill 1020088;
Query OK, 0 rows affected


[b]4.再次查看当前等待锁的事务及当前事务:[/b]

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
Empty set

mysql>
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
Empty set

mysql> select * from information_schema.INNODB_TRX;
Empty set



在分析之前先来看一下MySQL的锁机制,MySQLl锁机制有行级锁和表级锁,
InnoDB实现了两种类型的行锁:
共享锁(S):允许一个事务去读一行,阻止其他事务获得相同的数据集的排他锁。
排他锁(X):允许获得排他锁的事务更新数据,但是组织其他事务获得相同数据集的共享锁和排他锁。

共享锁就是我读的时候,你可以读,但是不能写。排他锁就是我写的时候,你不能读也不能写。其实就是MyISAM的读锁和写锁,但是针对的对象不同了而已。

除此之外InnoDB还有两个表锁:
意向共享锁(IS):表示事务准备给数据行加入共享锁,也就是说一个数据行加共享锁前必须先取得该表的IS锁
意向排他锁(IX):类似上面,表示事务准备给数据行加入排他锁,说明事务在一个数据行加排他锁前必须先取得该表的IX锁。

InnoDB行锁模式兼容列表,见下这篇文章
InnoDB的行锁模式及加锁方法:[url]http://lib.youkuaiyun.com/article/mysql/8747[/url]

[color=red][b]注意:[/b][/color]
当一个事务请求的锁模式与当前的锁兼容,InnoDB就将请求的锁授予该事务;反之如果请求不兼容,则该事务就等待锁释放。意向锁是InnoDB自动加的,不需要用户干预。

对于insert、update、delete,InnoDB会自动给涉及的数据加排他锁(X);
对于一般的Select语句,InnoDB不会加任何锁,事务可以通过以下语句给显示加共享锁或排他锁。

共享锁:select * from table_name where .....lock in share mode
排他锁:select * from table_name where .....for update
[b]分析死锁原因:[/b]
出现死锁的原因,是因为会话A开启一个事务,以共享锁S锁,获取user表id为1的一行记录;
会话B,删除user表id为1记录,请求排他锁X锁,由于SX锁互斥,会话B等待会话A释放共享锁S锁,进入请求队列等待;这时,会话A删除user表id为1记录,请求排他锁X锁,但会话B在请求队列中,还轮不到会话A,会话A就等待,这种循环等待出现,死锁就出现了。

附:
这部分与上面无关,只作为记录
检查数据库表状态:
mysql> show table status from test_db;
+---------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+----------------------------------------------------------------------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+----------------------------------------------------------------------+
| user | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2017-07-17 19:39:33 | NULL | NULL | utf8_general_ci | NULL | | |
| tb_message | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 0 | 0 | 14 | 2017-07-17 19:14:02 | NULL | NULL | utf8_general_ci | NULL | | |
| tb_log | InnoDB | 10 | Compact | 1 | 16384 | 16384 | 0 | 0 | 0 | 2 | 2017-07-18 02:00:31 | NULL | NULL | utf8_general_ci | NULL | |
+---------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+----------------------------------------------------------------------+
3 rows in set


mysql>

检查指定表状态:
mysql> check table user;
+----------------------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------------------+-------+----------+----------+
| test_db.user | check | status | OK |
+----------------------------+-------+----------+----------+
1 row in set


如果表状态不OK,则修复:
mysql>  repair table user;



查看数据库当前事务提交状态:
mysql> show variables like 'autocommit';  
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set


查看数据库事务隔离级别:
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set

mysql>

MySQL的线程状态快照:
快照1:
mysql> show processlist
-> ;
+------+---------+-------------------+----------------+---------+------+---------------------------------+----------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+------+---------+-------------------+----------------+---------+------+---------------------------------+----------------------------------------------------------------------------------------------------+
| 2721 | donald | 192.168.31.153:50725 | test_db | Query | 1070 | Waiting for table metadata lock | ALTER TABLE `user`
CHANGE COLUMN `REGISTRER_NUM` `REGISTER_NUM` varchar(13) CHARACTER SET u |
| 2724 | donald | 192.168.31.153:57376 | NULL | Sleep | 2432 | | NULL |
| 2725 | donald | 192.168.31.153:57381 | test_db | Sleep | 2423 | | NULL |
| 2727 | donald | 192.168.31.153:51135 | test_db | Sleep | 0 | | NULL |
| 2728 | donald | 192.168.31.153:51156 | test_db | Sleep | 2017 | | NULL |
| 2731 | donald | 192.168.31.153:51789 | test_db | Query | 647 | Waiting for table metadata lock | CREATE TABLE `user` (
`COM_ID` bigint(20) NOT NULL,
`BUSI_NO` varchar(32) DEFAULT NULL |
| 2735 | donald | 192.168.31.153:51813 | test_db | Query | 546 | Waiting for table metadata lock | CREATE TABLE `user` (
`COM_ID` bigint(20) NOT NULL,
`BUSI_NO` varchar(32) DEFAULT NULL |
| 2736 | donald | 192.168.31.153:58929 | test_db | Query | 0 | init | show processlist |
+------+---------+-------------------+----------------+---------+------+---------------------------------+----------------------------------------------------------------------------------------------------+
8 rows in set

快照2:
mysql> show processlist;
+---------+-----------------+-------------------+------+---------+---------+------------------------+---------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+---------+-----------------+-------------------+------+---------+---------+------------------------+---------------------------------------------------------------------+
| 1 | event_scheduler | localhost | NULL | Daemon | 3030806 | Waiting on empty queue | NULL |
| 1019932 | donald | 192.168.31.153:15217 | NULL | Sleep | 1526 | | NULL |
| 1019933 | donald | 192.168.31.153:15218 | test | Sleep | 1174 | | NULL |
| 1020076 | donald | 192.168.31.153:15254 | test | Query | 0 | init | show processlist |
| 1020087 | donald | 192.168.31.153:15262 | test | Sleep | 14 | | NULL |
| 1020089 | donald | 192.168.31.153:15612 | test | Query | 41 | update | INSERT INTO `user` (`id`, `name`, `age`) VALUES ('1', 'jack', '23') |
| 1020540 | donald | 192.168.31.153:16592 | test | Sleep | 64 | | NULL |
+---------+-----------------+-------------------+------+---------+---------+------------------------+---------------------------------------------------------------------+
7 rows in set
Deadlock found when trying to get lock;try restarting transaction Deadlock found when trying to get lock;try restarting transaction 阅读详情

相关推荐

避坑指南:Qt5.14.2 ARM交叉编译中那些你可能遇到的‘坑’及解决方案

本文深度解析了Qt 5.14.2在ARM架构上进行交叉编译的完整流程与常见难题。重点阐述了如何精准匹配交叉编译器与目标板环境、正确配置sysroot以及使用configure参数的艺术,并针对编译过程中的头文件缺失、链接错误等典型“坑”提供了经过验证的解决方案,旨在帮助开发者成功构建适用于嵌入式设备的Qt运行时库。

weixin_29279101的博客 354

Mysql报Deadlock found when trying to get lock; try restarting transaction问题解决

mysql deadLock

a1k2l45k的博客 5万+

薄膜电容寿命预测全解析:从理论到实践,5步搞定可靠性评估

本文全面解析薄膜电容寿命预测的理论与实践,提供五步可靠性评估流程,涵盖温度应力、电压应力等关键因素分析,并分享提升寿命的实用技巧。特别适用于新能源、电动汽车等领域的设计工程师,帮助准确预测薄膜电容在实际工况下的使用寿命。

weixin_33712987的博客 433

Deadlock found when trying to get lock; try restarting transaction 【MySQL死锁问题解决】

Deadlock found when trying to get lock; try restarting transaction 【MySQL死锁问题解决】 最近在调试接口的时候遇到了MySQL死锁问题,我自己测试的时候一切都好好的,但在并发下,就死锁了 其实死锁问题,并没有一个类似“万金油”的解决办法,解铃还需系铃人,能做的只有写这个代码的人自己去解决 第一次出现死锁,想必你也会和我一样整个人都是懵的,不知道如何下手,等你看过下面的文章明白了死锁,就不会害怕了.........

小道仙的后宫 4万+

从零到一:使用阿里云百炼搭建企业级 AI Agent 完全实战手册(2026超详细版)

本文是一份企业级AI Agent构建手册,主要面向企业技术负责人、AI产品经理和后端开发工程师。手册详细介绍了如何在阿里云百炼平台上搭建企业智能体应用的全流程,包括环境准备、知识库构建、智能体创建、工具集成、工作流编排等核心环节。重点内容包括: 准备工作:阿里云账号注册、百炼服务开通、免费额度领取、API Key管理 知识库构建:文档格式要求、内容优化原则、RAG系统搭建 智能体开发:Agent创建、工具集成、复杂业务工作流设计 生产部署:多渠道接入、监控运维、安全治理 成本控制:Token预算管理、选型决

u010323587的专栏 1095

对接金蝶云星空调用即时库存信息查询API(附JAVA实现)

这篇文章教你如何对接金蝶API并且使用JAVA调用金蝶云星空的即时库存查询API

实用的博客 2609

mysql中update时出现时报错Deadlock found when trying to get lock; try restarting transaction的原因以及解决方式

最近线上项目里的一条update语句出现了报错 Deadlock found when trying to get lock; try restarting transaction update f_order set delivery_status = ?, version = ? + 1 where order_id = ? and version = ? and data_state = 0 查看表结构发现order_id 和 delivery_status 都是非主键索引 翻阅 资料后发现

qq_42303467的博客 7134

【Docker】完美解决拉取镜像超时报错:ERROR: Get https://registry-1.docker.io/v2/

因为我们下载的镜像是外网的镜像资源,所以下载的速度会非常的慢,甚至大概率下载时会报错,所以需要我们配置一个国内的服务器镜像地址,国内服务器镜像地址有很多,这里咱们就说一下阿里镜像加速器的配置方法。

liu_chen_yang的博客 12万+

MySql 更新死锁问题 Deadlock found when trying to get lock; try restarting transaction

文章导航-readme MySql 更新死锁问题 Deadlock found when trying to get lock; try restarting transaction 1.场景 //table1 CREATE TABLE `retailtrades` ( `TradeId` bigint(20) NOT NULL COMMENT '主键', `TradeCode`...

yufaxingxing的博客 6967

【异常】Cursor编辑器 “Taking longer than expected...“ 报错全链路排查与解决方案

该提示并非传统的程序崩溃或语法报错,本质是Cursor编辑器的超时预警机制——当核心任务(云端AI推理请求、本地项目索引等)的执行耗时超出系统预设阈值时,触发的用户告知类提示,核心诱因是任务执行全链路出现阻塞或性能瓶颈。以下方案按排查优先级从高到低排序,覆盖从快速急救到根因根治的全流程,可按步骤逐步验证解决。针对大仓库、多依赖项目的索引超时问题,核心是通过规则过滤无效文件,从根源降低索引负载。

奶爸的编程之路,也就一周冷个三天 3103

Android AMS——AMS初始化(二)

Android AMS 也是一个系统服务,属于系统服务中的引导服务,在 startBootstrapServices() 方法中启动。这里我们就来看一下 ActivityManagerService 的启动流程。

小旭的专栏 779

Mysql死锁问题Deadlock found when trying to get lock;try restarting transaction

表的排他锁(lock_mode X),相当于两个事务都在等对方持有的排他锁,于是就死锁了。先去服务器上,看看死锁的日志,找到 mysql 的安装路径,使用如下命令登录 mysql。mysql -h 数据库IP地址 -P 数据库端口 -u 用户名 -p 库名。表的排他锁(lock_mode X),而事务2在根据主键ID更新。可以看到事务1在根据主键ID更新。去看了下代码,操作步骤如下。增加分布式锁,让并发不存在。1、根据主键ID更新。2、根据主键ID更新。3、根据主键ID更新。

dongdong199033的博客 4363

YOLOv13改进策略【Neck】| GSConv+Slim Neck:混合深度可分离卷积和标准卷积的轻量化网络设计

实时检测器架构的轻量级设计GsConv。

Limiiiing的博客 91

Deadlock found when trying to get lock; try restarting transaction

近遇到一个MYSQL update语句出现Deadlock found when trying to get lock的问题,分析一下原因。 什么情况下会出现Deadlock found when trying to get lock? https://dev.mysql.com/doc/refman/5.6/en/innodb-deadlocks.html 出现死锁需要2个条件: 1)至少2个client(A,B)同时在执行事务 2)clientA锁定了某一行,未提交事务,此时clientB也需要upda

weixin_43883790的博客 3218

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

MySQL死锁(Deadlock found when trying to get lock; try restarting transaction)

前言:最近一个SpringBoot项目在一段时间内出现了大量的死锁,花了些时间,最总解决了问题,在此记录一下。 写在前面:本次问题的核心是在事务检索和操作时没有使用索引,数据库使用了表锁,导致操作时间过长,从而使锁占用时间过长,最终导致死锁。 环境: 数据库:MySQL 5.7 存储引擎:InnoDB (1).定位问题 进入数据库命令行,执行以下指令,查看最近一次死锁的信息: show engine innodb status; MySQL默认只能看到最近一次的死锁,如果想要把死锁信息保存到MySQL

xhy1999的博客 7765

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

重复插入相同数据导致deadlock问题:Deadlock found when trying to get lock; try restarting transaction

场景: 业务逻辑:第三方登录情况下,获取到用户的实名信息。之后判断该用户在用户表中是否存在,如果不存在或非实名,那么将其实名;如果已经实名,那么不做处理,直接登录。ORM使用的是spring data jpa,用户表在mobile字段上有唯一索引idx_mobile 发现不定期的发生业务报错:Deadlock found when trying to get lock; try restart...

u013905744的专栏 6万+

记录一次 Deadlock found when trying to get lock; try restarting transaction 错误

在MySql插入数据的时候遇到 Deadlock found when trying to get lock 错误,日志如下 SequelizeDatabaseError: Deadlock found when trying to get lock; try restarting transaction at Query.formatError (/www/server/node_modules/sequelize/lib/dialects/mysql/query.js:244:16)

ctbinzi的专栏 7859

数据库死锁的问题,Deadlock found when trying to get lock; try restarting transaction at Query.formatError

场景: 应用刚上线排除大批量请求的问题 线上多次出现的Deadlock found when trying to get lock错误 代码: async batchUpdate(skus, { transaction }) { const result = await Promise.all(skus.map(async sku => { const record = await this.app.model.Sku.upsert(sku, { transaction...

cnmeimei的博客 1411

Deadlock found when trying to get lock; try restarting transaction

1、错误描述 [ERROR:]2015-06-09 16:56:19,481 [抄送失败] org.hibernate.exception.LockAcquisitionException: error executing work at org.hibernate.dialect.MySQLDialect$1.convert(MySQLDialect.java:451) at org.h

游海东的技术专栏 4453

mysql 死锁 Deadlock found when trying to get lock; try restarting transaction

Deadlock found when trying to get lock; try restarting transaction   1213 - Deadlock found when trying to get lock; try restarting transaction出现这个原因要记住一点就是:innodb的行锁 和解锁都是针对主键索引的。如果查询时根据索引锁表,但更...

hehaibo 2055

MySQL error : Deadlock found when trying to get lock; try restarting transaction

在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”。 在 14.5.5.3How to Minimize and Handle Deadlocks 中有这样一句话: Deadlocks are not dangerous. Just try a...

diqiexie4777的博客 1545

程序导致Mysql死锁Deadlock found when trying to get lock; try restarting transaction解决方案

程序导致Mysql死锁Deadlock found when trying to get lock; try restarting transaction解决方案

weixin_38203586的博客 4432
上一篇: No operation was found with the name解决方法总结
下一篇: Netty 通道处理器ChannelHandler和适配器定义ChannelHandlerAdapter
Donald_Draper
博客等级 码龄14年 9粉丝 106原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值