溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL中begin后事務(wù)為什么不提交

發(fā)布時(shí)間:2021-11-08 11:23:15 來源:億速云 閱讀:314 作者:iii 欄目:MySQL數(shù)據(jù)庫

這篇文章主要介紹“MySQL中begin后事務(wù)為什么不提交”,在日常操作中,相信很多人在MySQL中begin后事務(wù)為什么不提交問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MySQL中begin后事務(wù)為什么不提交”的疑惑有所幫助!接下來,請跟著小編一起來學(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í)就是在判斷是都o(jì)ption_bits的對應(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)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI