How to find out uncommitted transaction information in MySQL

How to find out uncommitted transaction information in MySQL

A while ago, I wrote a blog post titled "Can we find SQL statements of uncommitted transactions in ORACLE?" So in the MySQL database, can we find out the SQL statements executed by uncommitted transactions or related information of uncommitted transactions?

The experiment verified that if there is an uncommitted transaction in a session (connection) and no operation is performed, the thread is in the Sleep state.

mysql> select connection_id() from dual;
+-----------------+
| connection_id() |
+-----------------+
| 6 |
+-----------------+
1 row in set (0.00 sec)
 
mysql> set session autocommit=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql> delete from kkk where id =1;
Query OK, 1 row affected (0.00 sec)
 
mysql>

In another session (connection), query the detailed information of the transaction that has not been committed for more than 10 seconds:

SELECT t.trx_mysql_thread_id
 ,t.trx_state
 ,t.trx_tables_in_use
 ,t.trx_tables_locked
 ,t.trx_query
 ,t.trx_rows_locked 
 ,t.trx_rows_modified
 ,t.trx_lock_structs
 ,t.trx_started
 ,t.trx_isolation_level
 ,p.time 
 ,p.user
 ,p.host
 ,p.db
 ,p.command
FROM information_schema.innodb_trx t 
 INNER JOIN information_schema.processlist p 
  ON t.trx_mysql_thread_id = p.id 
WHERE t.trx_state = 'RUNNING' 
 AND p.time > 10 
 AND p.command = 'Sleep'\G 

As shown in the above screenshot, trx_query is NULL value. It is basically impossible to find SQL statements for uncommitted transactions. The information about transactions inside MySQL is not very detailed, and can even be said to be a bit concise. I can't even locate where the lock is happening on that table. Only information such as trx_row_locked, trx_row_modified, and trx_started can be seen. The same is true when using show engine innodb status . You can only see some basic information.

mysql> show engine innodb status;

---TRANSACTION 1282583, ACTIVE 11937 sec

2 lock struct(s), heap size 360, 8 row lock(s), undo log entries 1

MySQL thread id 6, OS thread handle 0x7f8da2de3700, query id 190 localhost root

If the uncommitted transaction blocks other sessions, then it is possible (only a possibility, in many scenarios, it is impossible to find the SQL related to the committed transaction) to find the SQL executed by the uncommitted transaction

As shown in the following test, the delete operation was executed in the session (connection connection_id=11 ), but the transaction was not committed.

mysql> set session autocommit=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 11 |
+-----------------+
1 row in set (0.01 sec)
 
mysql> delete from kkk where id=1;
Query OK, 1 row affected (0.00 sec)
 
mysql>

Another session (connection) performs an operation to update a record. At this time, SQL will be blocked.

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 13 |
+-----------------+
1 row in set (0.00 sec)
 
mysql> 
mysql> update kkk set id=100 where id=1;

In another session, we can execute the following SQL to find the last SQL executed by the uncommitted transaction.

mysql> SELECT r.trx_id waiting_trx_id, 
 -> r.trx_mysql_thread_id waiting_thread, 
 -> r.trx_query waiting_query, 
 -> b.trx_id blocking_trx_id, 
 -> b.trx_mysql_thread_id blocking_thread, 
 -> b.trx_query blocking_query 
 -> FROM information_schema.innodb_lock_waits w 
 -> INNER JOIN information_schema.innodb_trx b 
 -> ON b.trx_id = w.blocking_trx_id 
 -> INNER JOIN information_schema.innodb_trx r 
 -> ON r.trx_id = w.requesting_trx_id; 
+----------------+----------------+----------------------------------+-----------------+-----------------+----------------+
| waiting_trx_id | waiting_thread | waiting_query | blocking_trx_id | blocking_thread | blocking_query |
+----------------+----------------+----------------------------------+-----------------+-----------------+----------------+
| 2830 | 13 | update kkk set id=100 where id=1 | 2825 | 11 | NULL |
+----------------+----------------+----------------------------------+-----------------+-----------------+----------------+
1 row in set (0.00 sec)
 
mysql> SELECT a.sql_text, 
 -> c.id, 
 -> d.trx_started 
 -> FROM performance_schema.events_statements_current a 
 -> join performance_schema.threads b 
 -> ON a.thread_id = b.thread_id 
 -> join information_schema.processlist c 
 -> ON b.processlist_id = c.id 
 -> join information_schema.innodb_trx d 
 -> ON c.id = d.trx_mysql_thread_id 
 -> where c.id=11
 -> ORDER BY d.trx_started\G;
*************************** 1. row ***************************
 sql_text: delete from kkk where id =1
  id: 11
trx_started: 2019-06-12 23:36:13
1 row in set (0.03 sec)
 
ERROR: 
No query specified
 
mysql> 

Summarize:

Basically MySQL can only find the basic information of uncommitted transactions, such as trx_mysql_thread_id, etc. In some scenarios, we have almost no way to find out detailed information such as the SQL executed by the uncommitted transaction. It is unclear what operations were performed on the uncommitted transactions!

Well, that’s all for this article. I hope the content of this article will be of certain reference value to your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Example of viewing and modifying MySQL transaction isolation level
  • Detailed explanation of the implementation principle of ACID transaction in Mysql
  • Explanation of mysql transaction select for update and data consistency processing
  • Mysql queries the transactions being executed and how to wait for locks
  • Example of MySQL transaction processing operation implemented in Java
  • Solution to PHP+MySQL high-concurrency locked transaction processing problem
  • How to start a transaction in MySQL

<<:  A brief discussion on the magic of parseInt() in JavaScript

>>:  Detailed explanation of using split command to split Linux files

Recommend

Centos8 bridge static IP configuration method in VMware virtual machine

1. Make sure the network connection method is bri...

How to use MySQL DATEDIFF function to get the time interval between two dates

describe Returns the time interval between two da...

Steps for Docker to build its own local image repository

1. Environment and preparation 1. Ubuntu 14.04 2....

Explanation and example usage of 4 custom instructions in Vue

Four practical vue custom instructions 1. v-drag ...

jQuery implements font size adjustment case

This article shares the specific code of jQuery t...

Causes and solutions for slow MySQL query speed and poor performance

1. What affects database query speed? 1.1 Four fa...

Vue implements click feedback instructions for water ripple effect

Table of contents Water wave effect Let's see...

Detailed explanation of XML syntax

1. Documentation Rules 1. Case sensitive. 2. The a...

Solution to the error when installing Docker on CentOS version

1. Version Information # cat /etc/system-release ...

Detailed explanation of several ways to export data in Mysql

There are many purposes for exporting MySQL data,...

JavaScript to implement the countdown for sending SMS

This article shares the specific code of JavaScri...

An example of the difference between the id and name attributes in input

I have been making websites for a long time, but I...

Vue Element-ui form validation rule implementation

Table of contents 1. Introduction 2. Entry mode o...

Instructions for using MySQL isolation Read View

Which historical version can the current transact...