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

How to define data examples in Vue

Preface In the development process, defining vari...

HTML+CSS to achieve drop-down menu

1. Drop-down list example The code is as follows:...

Simple tutorial on using Navicat For MySQL

recommend: Navicat for MySQL 15 Registration and ...

Introduction to generating Kubernetes certificates using OpenSSL

Kubernetes supports three types of authentication...

The leftmost matching principle of MySQL database index

Table of contents 1. Joint index description 2. C...

Detailed explanation of web page loading progress bar (recommended)

(When a web page is loading, sometimes there is t...

Vue realizes simple effect of running light

This article shares the specific code of Vue to a...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...

How to configure mysql5.6 to support IPV6 connection in Linux environment

Introduction: This article mainly introduces how ...

Multiple ways to calculate age by birthday in MySQL

I didn't use MySQL very often before, and I w...

Teach you step by step to configure MySQL remote access

Preface When using the MySQL database, sometimes ...

Vue implements carousel animation

This article example shares the specific code of ...

VUE+Canvas realizes the whole process of a simple Gobang game

Preface In terms of layout, Gobang is much simple...

CSS to achieve single-select folding menu function

Don’t introduce a front-end UI framework unless i...