Bobcares

Clear Deadlock MariaDB | Solution Revealed

by | Nov 25, 2022

This article provides a brief explanation of deadlocks in MariaDB and the method to clear it. At Bobcares, with our Server Management Services, we can handle your server issues.

How do we clear deadlocks in MariaDB?

Deadlocks in MariaDB: Deadlocks are frequently caused by an app issue rather than a database issue. The database itself indicates the application problem by sending the following message to the application as an error:

clear deadlock mariadb

If this error message appears in the application, we know exactly where the issue is. However, a deadlock always arises between two distinct connections. So we’ve to locate the other component of the issue. Let’s look into an entire deadlock situation:

SQL> SHOW ENGINE InnoDB STATUS\G
...
------------------------
LATEST DETECTED DEADLOCK
------------------------
2019-12-23 18:55:18 0x7f51045e3700
*** (1) TRANSACTION:
TRANSACTION 847, ACTIVE 10 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1
MySQL thread id 46, OS thread handle 139985942054656, query id 839 localhost root Updating
delete from t where id = 10
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 40 page no 3 n bits 80 index PRIMARY of table `test`.`t` trx id 847 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 32
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 00000000034e; asc N;;
2: len 7; hex 760000019c0495; asc v ;;

*** (2) TRANSACTION:
TRANSACTION 846, ACTIVE 25 sec starting index read
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1
MySQL thread id 39, OS thread handle 139985942361856, query id 840 localhost root Updating
delete from t where id = 11
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 40 page no 3 n bits 80 index PRIMARY of table `test`.`t` trx id 846 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 32
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 00000000034e; asc N;;
2: len 7; hex 760000019c0495; asc v ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 40 page no 3 n bits 80 index PRIMARY of table `test`.`t` trx id 846 lock_mode X locks rec but not gap waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 32
0: len 4; hex 8000000b; asc ;;
1: len 6; hex 00000000034f; asc O;;
2: len 7; hex 770000019d031d; asc w ;;

*** WE ROLL BACK TRANSACTION (2)
...

Here, we can only see the last Deadlock error. So we can use the below configuration option in the MariaDB configuration file (my.cnf) to record all deadlock errors in the error log for MariaDB.

innodb_print_all_deadlocks = ON

Clear/Fix deadlocks in MariaDB

Deadlocks occur when multiple (at least 2) transactions do some action including INSERT, UPDATE, REPLACE, or DELETE on the same rows of data, which then are locked.

In a transactional database system with fine-grained locking, deadlocks are always possible. As a result, the program needs to be aware of deadlocks, detect the deadlock issue, and attempt the transaction again.

try restarting transaction

Deadlock Prevention

Only no-concurrency conditions can promise Zero Deadlocks. But when we say “prevent,” we really mean “lower the likelihood of a deadlock to almost zero.” We can follow any of the below methods to prevent the deadlock:

  1. We must consider changing the behavior of the app which causes this deadlock.
  2. We must ensure that the locks are held for the shortest amount of time possible and that the conflicting transactions are processed as quickly as possible.
  3. Also, ensure that the number of locks is decreased. A deadlock is less likely to occur if the conflicting transactions lock fewer rows.
  4. We must reduce certain tasks. For e.g., refresh data less often or poll every 10 seconds instead of every second.
  5. Consider the order of transaction execution. Is it necessary for them to start running simultaneously?
  6. Find the deadlock error and retry the transaction again a few milliseconds later.
  7. Modify the transaction isolation level to a level causing fewer locks.

[Need further help? We’re available 24/7.]

Conclusion

Some DB rows are prone to deadlocks. Here, we’ve included the steps we can follow to avoid the deadlock to a great extent. We also included the command provided by our Tech team to clear the deadlock in MariaDB.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF