Transaction is the core of the relational database, throughout the source code, first to look at the relevant data structure, uncover the veil:
The server layer and the InnoDB engine layer correspond to different data structures, but are related to each other:
- The server tier requires the engine to register the transaction so that the server layer can reference it.
- The INNODB layer needs to maintain the state of the transaction and the specific implementation of the state transition of the transaction.
1. Related Data structures
The following is a snapshot of the transaction interaction between server and InnoDB:
Briefly describe:
The server layer associates all the participating engines through the stmt and all linked lists. Each connection, in the InnoDB engine assigns a TRX structure, which maintains to the end of the session.
No matter how many statements a transaction has, the number of participating table, an engine is registered only once, because a session currently has only one active transaction. The server notifies the engine to complete transaction-related operations by registering the engine.
Related Data structures:
Thd_trans
Ha_data
Ha_trx_info
Trx_sys_struct
Trx_struct
Specific reference to the source code.
2. How transactions Begin
Test: SELECT * FROM t
The engine of the T-table is InnoDB
1. First time connection
The first connection, when creating the ThD object, is the initial Ha_data[max_ha] array, but the initialization of the engine is completed.
The Thd->transaction.stmt/all are empty.
2. Create TRX: Corresponds to each connection, representing the transaction context of the connection
When the statement executes, create the INNODB TRX structure, corresponding to the ha_data[8].ha_ptr.
function Call Stack:
Ha_innobase::store_lock
Check_trx_exists
Innobase_trx_allocate
Trx_allocate_for_mysql:
1. Trx_create: Creating a TRX Structure
2. Incrementing the Trx_n_mysql_transactions global count
3. Join the Global link list trx_sys->mysql_trx_list
Innobase_trx_init
Initialize TRX associated with current server layer THD
Note: During the execution of the statement, all table of the polling statement is assigned TRX in the Get_table_lock process because the transaction and the lock are related. All transaction-related registrations are closely tied to the table_lock process.
3. Register the engine that participates in the transaction to Thd->transaction
Ha_info->is_started () If Ha_info is already associated with the engine, then the transaction has already started and has been registered.
Here are two things to do:
1. If autocommit=0
InnoDB engine needs to register Stmt,all two linked list
2. If autocommit=1
The InnoDB engine only needs to register the stmt linked list
The results after registration are:
(GDB) P Thd->transaction.all.ha_list.m_ht.db_type
$66 = Db_type_innodb
4. Transaction Submission
If autocommit=0, when the transaction commits:
Release Lock:
Ha_innobase::external_lock
trx->n_mysql_tables_in_use--; Clean the TRX structure during the release of table lock
Submit:
Trans_commit_stmt
if (thd->transaction.stmt.ha_list)
Ha_commit_trans (THD, FALSE);
Thd->transaction.stmt.reset (); Reset stmt Linked List
Commit_one_phase_low; resets ha_trx_info to no use status.
Here is just the beginning of the business, not to be continued ...