From 78f2b6d2c05440c13e35bde8a4505e6ae1b030fb Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Mon, 9 Dec 2019 13:41:09 +0800 Subject: [PATCH] session: if txn invalid do not active it and return an error (#13935) Conflicts: executor/batch_point_get.go executor/point_get.go --- executor/adapter.go | 2 +- executor/builder.go | 2 +- executor/executor_test.go | 2 +- executor/point_get.go | 2 +- executor/simple.go | 2 +- session/session.go | 3 +++ session/session_test.go | 8 +++++--- 7 files changed, 13 insertions(+), 8 deletions(-) diff --git a/executor/adapter.go b/executor/adapter.go index aac68603173ff..888ffda6716cb 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -523,7 +523,7 @@ func (a *ExecStmt) handlePessimisticLockError(ctx context.Context, err error) (E if err != nil { tsErr := UpdateForUpdateTS(a.Ctx, 0) if tsErr != nil { - return nil, tsErr + logutil.Logger(ctx).Warn("UpdateForUpdateTS failed", zap.Error(tsErr)) } } return nil, err diff --git a/executor/builder.go b/executor/builder.go index 50ea69f340859..08ca8272d387f 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -1214,7 +1214,7 @@ func (b *executorBuilder) getStartTS() (uint64, error) { if err != nil { return 0, err } - if startTS == 0 && txn.Valid() { + if startTS == 0 { startTS = txn.StartTS() } b.startTS = startTS diff --git a/executor/executor_test.go b/executor/executor_test.go index a18101565f633..a1e54d17c99f1 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -2443,7 +2443,7 @@ func (s *testSuite) TestSelectForUpdate(c *C) { tk.MustExec("drop table if exists t, t1") txn, err := tk.Se.Txn(true) - c.Assert(err, IsNil) + c.Assert(kv.ErrInvalidTxn.Equal(err), IsTrue) c.Assert(txn.Valid(), IsFalse) tk.MustExec("create table t (c1 int, c2 int, c3 int)") tk.MustExec("insert t values (11, 2, 3)") diff --git a/executor/point_get.go b/executor/point_get.go index 2da6cd6011e02..1d9d3de891f8d 100644 --- a/executor/point_get.go +++ b/executor/point_get.go @@ -174,7 +174,7 @@ func (e *PointGetExecutor) encodeIndexKey() (_ []byte, err error) { } func (e *PointGetExecutor) get(key kv.Key) (val []byte, err error) { - txn, err := e.ctx.Txn(true) + txn, err := e.ctx.Txn(false) if err != nil { return nil, err } diff --git a/executor/simple.go b/executor/simple.go index 29640ebf9705e..a26f4510e3a00 100644 --- a/executor/simple.go +++ b/executor/simple.go @@ -618,7 +618,7 @@ func (e *SimpleExec) executeRollback(s *ast.RollbackStmt) error { sessVars := e.ctx.GetSessionVars() logutil.Logger(context.Background()).Debug("execute rollback statement", zap.Uint64("conn", sessVars.ConnectionID)) sessVars.SetStatusFlag(mysql.ServerStatusInTrans, false) - txn, err := e.ctx.Txn(true) + txn, err := e.ctx.Txn(false) if err != nil { return err } diff --git a/session/session.go b/session/session.go index 2c5b952e745e6..d2aca2512f6ee 100644 --- a/session/session.go +++ b/session/session.go @@ -1267,6 +1267,9 @@ func (s *session) DropPreparedStmt(stmtID uint32) error { } func (s *session) Txn(active bool) (kv.Transaction, error) { + if !s.txn.validOrPending() && active { + return &s.txn, kv.ErrInvalidTxn + } if s.txn.pending() && active { // Transaction is lazy initialized. // PrepareTxnCtx is called to get a tso future, makes s.txn a pending txn, diff --git a/session/session_test.go b/session/session_test.go index 2bce04245a38d..87624a73cbcff 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -286,7 +286,7 @@ func (s *testSessionSuite) TestRowLock(c *C) { tk.MustExec("drop table if exists t") txn, err := tk.Se.Txn(true) - c.Assert(err, IsNil) + c.Assert(kv.ErrInvalidTxn.Equal(err), IsTrue) c.Assert(txn.Valid(), IsFalse) tk.MustExec("create table t (c1 int, c2 int, c3 int)") tk.MustExec("insert t values (11, 2, 3)") @@ -375,7 +375,9 @@ func (s *testSessionSuite) TestTxnLazyInitialize(c *C) { tk.MustExec("create table t (id int)") tk.MustExec("set @@autocommit = 0") - txn, err := tk.Se.Txn(false) + txn, err := tk.Se.Txn(true) + c.Assert(kv.ErrInvalidTxn.Equal(err), IsTrue) + txn, err = tk.Se.Txn(false) c.Assert(err, IsNil) c.Assert(txn.Valid(), IsFalse) tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) @@ -669,7 +671,7 @@ func (s *testSessionSuite) TestRetryPreparedStmt(c *C) { tk.MustExec("drop table if exists t") txn, err := tk.Se.Txn(true) - c.Assert(err, IsNil) + c.Assert(kv.ErrInvalidTxn.Equal(err), IsTrue) c.Assert(txn.Valid(), IsFalse) tk.MustExec("create table t (c1 int, c2 int, c3 int)") tk.MustExec("insert t values (11, 2, 3)")