Skip to content

Commit

Permalink
*: fix transaction lazy initialization in pessimistic mode (#14446) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and jackysp committed Jan 15, 2020
1 parent 87fdf87 commit 91f8a2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,19 @@ func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, e Executor) (sqlex

func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error {
sctx := a.Ctx
txn, err := sctx.Txn(true)
// Do not active the transaction here.
// When autocommit = 0 and transaction in pessimistic mode,
// statements like set xxx = xxx; should not active the transaction.
txn, err := sctx.Txn(false)
if err != nil {
return err
}
txnCtx := sctx.GetSessionVars().TxnCtx
for {
_, err = a.handleNoDelayExecutor(ctx, e)
if !txn.Valid() {
return err
}
if err != nil {
// It is possible the DML has point get plan that locks the key.
e, err = a.handlePessimisticLockError(ctx, err)
Expand Down
8 changes: 8 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,17 @@ func (s *testSessionSuite) TestAutocommit(c *C) {
// TestTxnLazyInitialize tests that when autocommit = 0, not all statement starts
// a new transaction.
func (s *testSessionSuite) TestTxnLazyInitialize(c *C) {
testTxnLazyInitialize(s, c, false)
testTxnLazyInitialize(s, c, true)
}

func testTxnLazyInitialize(s *testSessionSuite, c *C, isPessimistic bool) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int)")
if isPessimistic {
tk.MustExec("set tidb_txn_mode = 'pessimistic'")
}

tk.MustExec("set @@autocommit = 0")
txn, err := tk.Se.Txn(true)
Expand Down

0 comments on commit 91f8a2a

Please sign in to comment.