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

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

Find the problem

Recently, when I was filling in the previous data, the program suddenly reported the following error:

[2017-02-10 13:12:06.678] [INFO] mysqlLog - update tbl_playerdata_error: { [Error: ER_LOCK_DEADLOCK: Deadlock found when trying to get lock; try restarting transaction]
 code: 'ER_LOCK_DEADLOCK',
 errno: 1213,
 sqlState: '40001',
 index: 0 }

At first glance, it is obvious that MySQL has a deadlock problem. In fact, the program running above has been running on the test server for a long time without any problems. Why does MySQL deadlock problem occur on the official server? The first reaction is that the amount of data is too large (more than 3 million records), but it is impossible. Besides, what does deadlock have to do with these things? It seems that I need to solve it properly.

Problem Analysis

My analysis is: Since we are now processing the data of the official server, and there are many users operating on the official server, there should be a deadlock with the data update on my side when the user is querying or performing other operations (first of all, I will explain that I am using the InnoDB storage engine. Since the query or other operation on the user side locked the resources I needed, and the update on my side also locked part of the resources for the user's operation, both sides were waiting for the other side to release resources, which led to a deadlock).

Workaround

After knowing the error code, first check the MySQL description. For the above Error: 1213 SQLSTATE: 40001, see: Server Error Codes and Messages

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

InnoDB reports this error when a transaction encounters a deadlock and is automatically rolled back so that your application can take corrective action. To recover from this error, run all the operations in this transaction again. A deadlock occurs when requests for locks arrive in inconsistent order between transactions. The transaction that was rolled back released all its locks, and the other transaction can now get all the locks it requested. Thus, when you re-run the transaction that was rolled back, it might have to wait for other transactions to complete, but typically the deadlock does not recur. If you encounter frequent deadlocks, make the sequence of locking operations (LOCK TABLES, SELECT ... FOR UPDATE, and so on) consistent between the different transactions or applications that experience the issue. See Section 14.8.5, “Deadlocks in InnoDB” for details.

There are two sentences above:

To recover from this error, run all the operations in this transaction again<br><br>If you encounter frequent deadlocks, make the sequence of locking operations (<code class="literal">LOCK TABLES</code>, <code class="literal">SELECT ... FOR UPDATE</code>, and so on) <br>consistent between the different transactions or applications that experience the issue

These two sentences also explain how to deal with deadlock. When a deadlock error occurs, I use a timer to re-update the operation, thus avoiding the above problems.

In addition, refer to the answer on stack overflow: http://stackoverflow.com/questions/2332768/how-to-avoid-mysql-deadlock-found-when-trying-to-get-lock-try-restarting-trans

One easy trick that can help with most deadlocks is sorting the operations in a specific order.

You get a deadlock when two transactions are trying to lock two locks at opposite orders, ie:

connection 1: locks key(1), locks key(2);
connection 2: locks key(2), locks key(1);
If both run at the same time, connection 1 will lock key(1), connection 2 will lock key(2) and each connection will wait for the other to release the key -> deadlock.

Now, if you changed your queries such that the connections would lock the keys at the same order, ie:

connection 1: locks key(1), locks key(2);
connection 2: locks key(1), locks key(2);
it will be impossible to get a deadlock.

So this is what I suggest:

Make sure you have no other queries that lock access more than one key at a time except for the delete statement. if you do (and I suspect you do), order their WHERE in (k1,k2,..kn) in ascending order.
Fix your delete statement to work in ascending order:
Change

DELETE FROM onlineusers WHERE datetime <= now() - INTERVAL 900 SECOND
To

DELETE FROM onlineusers WHERE id IN (SELECT id FROM onlineusers
 WHERE datetime <= now() - INTERVAL 900 SECOND order by id) u;
Another thing to keep in mind is that mysql documentation suggest that in case of a deadlock the client should retry automatically. you can add this logic to your client code. (Say, 3 retries on this particular error before giving up).

Reference: http://blog.sina.com.cn/s/blog_4acbd39c01014gsq.html

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • SqlTransaction in C# - Detailed explanation of transactions
  • mysql Non-Transactional Database Only (only supports MyISAM)
  • A Brief Introduction to Transact-SQL
  • Introduction to MySQL database transaction isolation level (Transaction Isolation Level)
  • Transact_SQL Manual
  • Commonly used Transact-SQL scripts for Sql Server database (recommended)

<<:  Example of using JSX to create a Markup component style development (front-end componentization)

>>:  Application of Hadoop counters and data cleaning

Recommend

WeChat applet implements simple chat room

This article shares the specific code of the WeCh...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

Reasons why MySQL 8.0 statistics are inaccurate

Preface Whether it is Oracle or MySQL, the new fe...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...

An article to solve the echarts map carousel highlight

Table of contents Preface toDoList just do it Pre...

How to pass parameters to JS via CSS

1. Background that needs to be passed through CSS...

Solve the problem of secure_file_priv null

Add secure_file_priv = ' '; then run cmd ...

HTML thead tag definition and usage detailed introduction

Copy code The code is as follows: <thead> &...

A record of pitfalls in JS regular matching

I recently discovered a pitfall in regular expres...

Summary of related functions for Mysql query JSON results

The JSON format field is a new attribute added in...

MySQL 8.0.15 version installation tutorial connect to Navicat.list

The pitfalls 1. Many tutorials on the Internet wr...