// sql/sql_parse.cc:mysql_execute_command
// 事务开始
// case SQLCOM_BEGIN:
if (trans_begin(thd, lex->start_transaction_opt)) { // 释放事务锁,其他错误处理,忽略}
// sql/transaction.cc
trans_begin:
if (trans_check(thd) DEBUG_RETURN(TRUE);
if (thd->in_multi_stmt_transaction_mode() ||
(thd->variables.option_bits & OPTION_TABLE_LOCK)) {
thd->server_status &= ~SERVER_STATUS_IN_TRANS;
res = test(ha_commit_trans(thd, TRUE));
}
if (res) DEBUG_RETURN(TRUE);
// 待解,啥是consistent_snapshot
if (flags & MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
res = ha_start_consistent_snapshot(thd);
// 事务提交
// case SQLCOM_COMMIT:
if (trans_commit(thd)) { //释放事务锁,其他错误处理,忽略}
// sql/transaction.cc
trans_commit:
trans_check(thd);
res = ha_commit_trans(thd, TRUE);
# 如果res==0, 则事务提交成功
if (res)
RUN_HOOK(transaction, after_rollback, (thd, FALSE));
else
RUN_HOOK(transaction, after_commit, (thd, FALSE));
// sql/handler.cc
ha_commit_trans:
// 如果是读写事务
if (thd->mdl_context.acquire_lock(&mld_request, thd->variables.lock_wait_timeout)) {
ha_rollback_trans(thd, all
}
// 如果是非两阶段事务,则直接提交
if (trans->no_2pc || (rw_ha_count <= 1)) {
error = ha_commit_one_phase(thd, all);
goto done;
}
ha_commit_one_phase:
res = commit_one_phase_2(thd, all, trans, is_real_trans);
commit_one_phase_2:
// 所有参与本次事务的存储引擎
Ha_trx_info *ha_info = trans->ha_list, *ha_info_next;
if (ha_info) {
for(; ha_info; ha_info = ha_info_next) {
handlerton *ht = ha_info->ht();
if (( err = ht->commit(ht, thd, all))) {
// 存储引擎commit失败,错误处理
}
ha_info_next = ha_info->next();
// 不解,直接reset后,怎么释放ha_info
ha_info->reset();
}
}