這篇文章主要介紹“MySQL中begin后事務(wù)為什么不提交”,在日常操作中,相信很多人在MySQL中begin后事務(wù)為什么不提交問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MySQL中begin后事務(wù)為什么不提交”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)專業(yè)提供成都服務(wù)器托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買成都服務(wù)器托管服務(wù),并享受7*24小時金牌售后服務(wù)。
今天順便看了一下,主要流程就是跟蹤為什么begin后事物不會提交,最后發(fā)現(xiàn)在:
MYSQL_BIN_LOG::commit 函數(shù)中包含這個判斷
if (!cache_mngr->trx_cache.is_binlog_empty() && ending_trans(thd, all) && !trx_stuff_logged)
如果begin的話ending_trans(thd, all) 將會返回為false,也就不會調(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);
}其實就是在判斷是都option_bits的對應(yīng)位上為1。因此簡單了我們就看看什么時候設(shè)置OPTION_BEGIN位就好了。
實際上是函數(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);
}實際上就是在MySQL層設(shè)置一些標示,如果是 START TRANSACTION WITH CONSISTENT SNAPSHOT 還會開啟一個一致性快照,就是一個readview。一旦設(shè)置了個標示將會不自動提交了。
到此,關(guān)于“MySQL中begin后事務(wù)為什么不提交”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站名稱:MySQL中begin后事務(wù)為什么不提交
文章路徑:http://www.chinadenli.net/article42/pgcghc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、標簽優(yōu)化、微信小程序、小程序開發(fā)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)