MySQL 8.3.0
Source Code Documentation
transaction.cc File Reference
#include "sql/transaction.h"
#include <stddef.h>
#include "lex_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/my_loglevel.h"
#include "mysql/psi/mysql_transaction.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/auth/auth_common.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/debug_sync.h"
#include "sql/handler.h"
#include "sql/log.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/query_options.h"
#include "sql/rpl_context.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_rli.h"
#include "sql/rpl_transaction_write_set_ctx.h"
#include "sql/session_tracker.h"
#include "sql/sql_class.h"
#include "sql/sql_lex.h"
#include "sql/system_variables.h"
#include "sql/tc_log.h"
#include "sql/transaction_info.h"
#include "sql/xa.h"

Functions

void trans_track_end_trx (THD *thd)
 Helper: Tell tracker (if any) that transaction ended. More...
 
void trans_reset_one_shot_chistics (THD *thd)
 Helper: transaction ended, SET TRANSACTION one-shot variables revert to session values. More...
 
bool trans_check_state (THD *thd)
 Check if we have a condition where the transaction state must not be changed (committed or rolled back). More...
 
bool trans_begin (THD *thd, uint flags)
 Begin a new transaction. More...
 
bool trans_commit (THD *thd, bool ignore_global_read_lock)
 Commit the current transaction, making its changes permanent. More...
 
bool trans_commit_implicit (THD *thd, bool ignore_global_read_lock)
 Implicitly commit the current transaction. More...
 
bool trans_rollback (THD *thd)
 Rollback the current transaction, canceling its changes. More...
 
bool trans_rollback_implicit (THD *thd)
 Implicitly rollback the current transaction, typically after deadlock was discovered. More...
 
bool trans_commit_stmt (THD *thd, bool ignore_global_read_lock)
 Commit the single statement transaction. More...
 
bool trans_rollback_stmt (THD *thd)
 Rollback the single statement transaction. More...
 
bool trans_commit_attachable (THD *thd)
 Commit the attachable transaction. More...
 
static SAVEPOINT ** find_savepoint (THD *thd, LEX_STRING name)
 
bool trans_savepoint (THD *thd, LEX_STRING name)
 Set a named transaction savepoint. More...
 
bool trans_rollback_to_savepoint (THD *thd, LEX_STRING name)
 Rollback a transaction to the named savepoint. More...
 
bool trans_release_savepoint (THD *thd, LEX_STRING name)
 Remove the named savepoint from the set of savepoints of the current transaction. More...
 

Function Documentation

◆ find_savepoint()

static SAVEPOINT ** find_savepoint ( THD thd,
LEX_STRING  name 
)
static

◆ trans_begin()

bool trans_begin ( THD thd,
uint  flags 
)

Begin a new transaction.

Note
Beginning a transaction implicitly commits any current transaction and releases existing locks.
Parameters
thdCurrent thread
flagsTransaction flags
Return values
falseSuccess
trueFailure

◆ trans_check_state()

bool trans_check_state ( THD thd)

Check if we have a condition where the transaction state must not be changed (committed or rolled back).

Currently we check that we are not executing a stored program and that we don't have an active XA transaction.

Returns
true if the commit/rollback cannot be executed, false otherwise.

◆ trans_commit()

bool trans_commit ( THD thd,
bool  ignore_global_read_lock 
)

Commit the current transaction, making its changes permanent.

Parameters
[in]thdCurrent thread
[in]ignore_global_read_lockAllow commit to complete even if a global read lock is active. This can be used to allow changes to internal tables (e.g. slave status tables, analyze table).
Return values
falseSuccess
trueFailure

◆ trans_commit_attachable()

bool trans_commit_attachable ( THD thd)

Commit the attachable transaction.

Note
This is slimmed down version of trans_commit_stmt() which commits attachable transaction but skips code which is unnecessary and unsafe for them (like dealing with GTIDs).
Parameters
thdCurrent thread
Return values
False- Success
True- Failure

◆ trans_commit_implicit()

bool trans_commit_implicit ( THD thd,
bool  ignore_global_read_lock 
)

Implicitly commit the current transaction.

Note
A implicit commit does not releases existing table locks.
Parameters
[in]thdCurrent thread
[in]ignore_global_read_lockAllow commit to complete even if a global read lock is active. This can be used to allow changes to internal tables (e.g. slave status tables, analyze table).
Return values
falseSuccess
trueFailure

◆ trans_commit_stmt()

bool trans_commit_stmt ( THD thd,
bool  ignore_global_read_lock 
)

Commit the single statement transaction.

Note
Note that if the autocommit is on, then the following call inside InnoDB will commit or rollback the whole transaction (= the statement). The autocommit mechanism built into InnoDB is based on counting locks, but if the user has used LOCK TABLES then that mechanism does not know to do the commit.
Parameters
[in]thdCurrent thread
[in]ignore_global_read_lockAllow commit to complete even if a global read lock is active. This can be used to allow changes to internal tables (e.g. slave status tables, analyze table).
Return values
falseSuccess
trueFailure

◆ trans_release_savepoint()

bool trans_release_savepoint ( THD thd,
LEX_STRING  name 
)

Remove the named savepoint from the set of savepoints of the current transaction.

Note
No commit or rollback occurs. It is an error if the savepoint does not exist.
Parameters
thdCurrent thread
nameSavepoint name
Return values
falseSuccess
trueFailure

◆ trans_reset_one_shot_chistics()

void trans_reset_one_shot_chistics ( THD thd)

Helper: transaction ended, SET TRANSACTION one-shot variables revert to session values.

Let the transaction state tracker know.

◆ trans_rollback()

bool trans_rollback ( THD thd)

Rollback the current transaction, canceling its changes.

Parameters
thdCurrent thread
Return values
falseSuccess
trueFailure

◆ trans_rollback_implicit()

bool trans_rollback_implicit ( THD thd)

Implicitly rollback the current transaction, typically after deadlock was discovered.

Parameters
thdCurrent thread
Return values
FalseSuccess
TrueFailure
Note
ha_rollback_low() which is indirectly called by this function will mark XA transaction for rollback by setting appropriate RM error status if there was transaction rollback request.

◆ trans_rollback_stmt()

bool trans_rollback_stmt ( THD thd)

Rollback the single statement transaction.

Parameters
thdCurrent thread
Return values
falseSuccess
trueFailure

◆ trans_rollback_to_savepoint()

bool trans_rollback_to_savepoint ( THD thd,
LEX_STRING  name 
)

Rollback a transaction to the named savepoint.

Note
Modifications that the current transaction made to rows after the savepoint was set are undone in the rollback.
Savepoints that were set at a later time than the named savepoint are deleted.
Parameters
thdCurrent thread
nameSavepoint name
Return values
falseSuccess
trueFailure

Checking whether it is safe to release metadata locks acquired after savepoint, if rollback to savepoint is successful.

Whether it is safe to release MDL after rollback to savepoint depends on storage engines participating in transaction:

  • InnoDB doesn't release any row-locks on rollback to savepoint so it is probably a bad idea to release MDL as well.
  • Binary log implementation in some cases (e.g when non-transactional tables involved) may choose not to remove events added after savepoint from transactional cache, but instead will write them to binary log accompanied with ROLLBACK TO SAVEPOINT statement. Since the real write happens at the end of transaction releasing MDL on tables mentioned in these events (i.e. acquired after savepoint and before rollback of it) can break replication, as concurrent DROP TABLES statements will be able to drop these tables before events will get into binary log,

◆ trans_savepoint()

bool trans_savepoint ( THD thd,
LEX_STRING  name 
)

Set a named transaction savepoint.

Parameters
thdCurrent thread
nameSavepoint name
Return values
falseSuccess
trueFailure

◆ trans_track_end_trx()

void trans_track_end_trx ( THD thd)

Helper: Tell tracker (if any) that transaction ended.