共計(jì) 2311 個(gè)字符,預(yù)計(jì)需要花費(fèi) 6 分鐘才能閱讀完成。
這篇文章主要介紹“MySQL 中 begin 后事務(wù)為什么不提交”,在日常操作中,相信很多人在 MySQL 中 begin 后事務(wù)為什么不提交問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”MySQL 中 begin 后事務(wù)為什么不提交”的疑惑有所幫助!接下來,請(qǐng)跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
今天順便看了一下,主要流程就是跟蹤為什么 begin 后事物不會(huì)提交,最后發(fā)現(xiàn)在:
MYSQL_BIN_LOG::commit 函數(shù)中包含這個(gè)判斷
if (!cache_mngr- trx_cache.is_binlog_empty()
ending_trans(thd, all) !trx_stuff_logged)
如果 begin 的話 ending_trans(thd, all) 將會(huì)返回為 false,也就不會(huì)調(diào)用 order_commit 流程了。
那么其主要判斷就是:
bool ending_single_stmt_trans(THD* thd, const bool all){ return (!all !thd- in_multi_stmt_transaction_mode());
}
下面是源碼注釋和函數(shù):
Returns TRUE if session is in a multi-statement transaction mode.
OPTION_NOT_AUTOCOMMIT: When autocommit is off, a multi-statement
transaction is implicitly started on the first statement after a
previous transaction has been ended.
OPTION_BEGIN: Regardless of the autocommit status, a multi-statement
transaction can be explicitly started with the statements START
TRANSACTION , BEGIN [WORK] , [COMMIT | ROLLBACK] AND CHAIN , etc.
Note: this doesn t tell you whether a transaction is active.
A session can be in multi-statement transaction mode, and yet
have no active transaction, e.g., in case of:
set @@autocommit=0;
set @a= 3; -- these statements don t
set transaction isolation level serializable; -- start an active
flush tables; -- transaction
I.e. for the above scenario this function returns TRUE, even
though no active transaction has begun.
@sa in_active_multi_stmt_transaction()
*/ inline bool in_multi_stmt_transaction_mode() const
{ return variables.option_bits (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
}
其實(shí)就是在判斷是都 option_bits 的對(duì)應(yīng)位上為 1。因此簡單了我們就看看什么時(shí)候設(shè)置 OPTION_BEGIN 位就好了。
實(shí)際上是函數(shù) trans_begin 設(shè)置的下面是這段代碼:
thd- variables.option_bits|= OPTION_BEGIN;
thd- server_status|= SERVER_STATUS_IN_TRANS; if (thd- tx_read_only)
thd- server_status|= SERVER_STATUS_IN_TRANS_READONLY;
DBUG_PRINT(info , ( setting SERVER_STATUS_IN_TRANS)); if (tst)
tst- add_trx_state(thd, TX_EXPLICIT); /* ha_start_consistent_snapshot() relies on OPTION_BEGIN flag set. */
if (flags MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
{ if (tst)
tst- add_trx_state(thd, TX_WITH_SNAPSHOT);
res= ha_start_consistent_snapshot(thd);
}
實(shí)際上就是在 MySQL 層設(shè)置一些標(biāo)示,如果是 START TRANSACTION WITH CONSISTENT SNAPSHOT 還會(huì)開啟一個(gè)一致性快照,就是一個(gè) readview。一旦設(shè)置了個(gè)標(biāo)示將會(huì)不自動(dòng)提交了。
到此,關(guān)于“MySQL 中 begin 后事務(wù)為什么不提交”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!