Skip to content

Commit

Permalink
session: if txn invalid do not active it and return an error (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored and XiaTianliang committed Dec 21, 2019
1 parent 65ffb21 commit d5e4d0a
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,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
Expand Down
2 changes: 1 addition & 1 deletion executor/batch_point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error {
return err
}

txn, err := e.ctx.Txn(true)
txn, err := e.ctx.Txn(false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,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
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,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)")
Expand Down
2 changes: 1 addition & 1 deletion executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (e *PointGetExecutor) lockKeyIfNeeded(ctx context.Context, key []byte) erro
}

func (e *PointGetExecutor) get(ctx context.Context, 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
}
Expand Down
2 changes: 1 addition & 1 deletion executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func (e *SimpleExec) executeRollback(s *ast.RollbackStmt) error {
sessVars := e.ctx.GetSessionVars()
logutil.BgLogger().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
}
Expand Down
3 changes: 3 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,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,
Expand Down
8 changes: 5 additions & 3 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,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)")
Expand Down Expand Up @@ -389,7 +389,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"))
Expand Down Expand Up @@ -719,7 +721,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)")
Expand Down

0 comments on commit d5e4d0a

Please sign in to comment.