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

How to Change Colors and Themes in Vim on Linux

Vim is a text editor that we use very often in Li...

Script to quickly list all host names (computer names) in the LAN under Linux

Recently, I have a need to list all host names in...

A brief discussion on the definition and precautions of H tags

Judging from the results, there is no fixed patte...

How to clear the timer elegantly in Vue

Table of contents Preface optimization Derivative...

base target="" controls the link's target open frame

<base target=_blank> changes the target fram...

How to import js configuration file on Vue server

Table of contents background accomplish Supplemen...

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...

Will Update in a Mysql transaction lock the table?

Two cases: 1. With index 2. Without index Prerequ...

Example code for implementing hexagonal borders with CSS3

The outermost boxF rotates 120 degrees, the secon...

Service management of source package installation under Linux

Table of contents 1. Startup management of source...

Detailed explanation of MySql data type tutorial examples

Table of contents 1. Brief Overview 2. Detailed e...

Solution to mysql ERROR 1045 (28000) problem

I encountered mysql ERROR 1045 and spent a long t...

vue3+ts+EsLint+Prettier standard code implementation

Table of contents use Use of EsLint Add a profile...