Home > database >  Ask everybody to help mysql high concurrency deadlock problem
Ask everybody to help mysql high concurrency deadlock problem

Time:09-17

Recently made a timer, and then at a point in time to modify the data, but led to a database deadlock
Error message:
 # # # : SQL UPDATE user_minute_data SET version=? The WHERE (user_id=? AND minute BETWEEN? The AND? AND version=? 
# # # Cause: com. Mysql. JDBC. Exceptions. Jdbc4. MySQLTransactionRollbackException: Deadlock found when trying to get the lock; Try restarting transaction


According to the Internet, to select between the update... For update but will still be error
The code
 @ Transactional 
Public int sumMinuteDataByUserAndDay (String userId, String date) {
//first query time under the condition of current summary
EntityWrapper EntityWrapper=new EntityWrapper<> (a);
HourData searchData=https://bbs.csdn.net/topics/new HourData ();
SearchData. SetUserId (userId);
SearchData. SetHour (date);
EntityWrapper. SetEntity (searchData);
List List=this selectList (entityWrapper);
//to each data to find and began to collect
If (a list. The size ()==0) {
The info (messageBlock + "summary, began to collect");
Int I=this. BaseMapper. InsertSumMinuteDataByUserAndDay (userId, date);
If (I==1) {
//summary, after the completion of tag low level data for version=2
The info (messageBlock + "completion, will have a summary data markup version");
MinuteData lowData=https://bbs.csdn.net/topics/new MinuteData ();
LowData. SetVersion (" 2 ");
EntityWrapper EntityWrapper2=new EntityWrapper<> (a);
//query condition in the index order to
EntityWrapper2. Eq (" user_id ", userId);
EntityWrapper2. Between (" minute, "the date +" 00 ", the date + 59 ":");
EntityWrapper2. Eq (" version ", "1");

EntityWrapper EntityWrapper3=new EntityWrapper<> (a);
//query condition in the index order to
EntityWrapper3. Eq (" user_id ", userId);
EntityWrapper3. Between (" minute, "the date +" 00 ", the date + 59 ":");
EntityWrapper3. Eq (" version ", "1");
EntityWrapper3. Last (" for update ");
LowService. SelectList (entityWrapper3);
Boolean update=lowService. Update (lowData entityWrapper2);
If (update) {
The info (messageBlock + "modify the state of complete success");
return 1;
} else {
Log. The error (messageBlock + "successful revision status is unusual, rollback, attention to the problem of trying to");
Throw new RuntimeException (messageBlock + "successfully modify the abnormal state");
}
} else {
Error log. The error (messageBlock + ", rollback, attention to the problem of trying to ");
Throw new RuntimeException (messageBlock + "summary error");
}
} else if (list. The size ()==1) {
The info (messageBlock + "has been summary");//, to summarize to achieve coverage summary method ");
} else {
The info (messageBlock + "summary data has repeated, user id:" + "time:" + date + + userId "to database cleanup");
}
return 0;
}


The show database engine innodb status
[align=left] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LATEST DETECTED DEADLOCK
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The 2019-10-22 16:03:00 18 a0
* * * (1) the TRANSACTION:
TRANSACTION 403535, ACTIVE 0 SEC fetching rows
Mysql tables in use 1, locked 1
The LOCK WAIT 17 LOCK struct (s), the heap size of 2936, 311 row LOCK (s), the undo log entries 2
MySQL thread id 19, OS thread handle 0 x271c, query id 72870 localhost 127.0.0.1 root updating
The UPDATE user_minute_data SET version='2'
The WHERE (user_id='2' AND minute BETWEEN '2019-10-22 15:00' AND '2019-10-22 s 15:59' AND version='1')
* * * (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD the LOCKS space id page 18 no 6 n bits 248 index ` PRIMARY ` of table ` demo `. ` user_minute_data ` TRX id 403535 lock_mode X waiting
* * * (2) the TRANSACTION:
TRANSACTION 403537, ACTIVE 0 SEC starting index read
Mysql tables in use 1, locked 1
14 the lock struct (s), the heap size of 2936, 255 row lock (s), the undo log entries 1
MySQL thread id 16, OS thread handle 0 x18a0, query id 72874 localhost 127.0.0.1 root updating
The UPDATE user_minute_data SET version='2'
The WHERE (user_id='3' AND minute BETWEEN '2019-10-22 15:00' AND '2019-10-22 s 15:59' AND version='1')
Doesn * * * (2) THE LOCK (S) :
RECORD the LOCKS space id page 18 no 6 n bits 248 index ` PRIMARY ` of table ` demo `. ` user_minute_data ` TRX id 403537 LOCKS lock mode S rec but not gap
* * * (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD the LOCKS space id page 18 no 6 n bits 248 index ` PRIMARY ` of table ` demo `. ` user_minute_data ` TRX id 403537 lock_mode X waiting
* * * WE ROLL BACK the TRANSACTION (2) [/align]
  • Related