diff --git a/session/session.go b/session/session.go index 06897363a60f3..8912f55795c84 100644 --- a/session/session.go +++ b/session/session.go @@ -664,9 +664,7 @@ func (s *session) retry(ctx context.Context, maxCnt uint) (err error) { if err != nil { return err } - if !s.sessionVars.IsAutocommit() { - s.sessionVars.SetStatusFlag(mysql.ServerStatusInTrans, true) - } + s.sessionVars.SetStatusFlag(mysql.ServerStatusInTrans, true) } else { s.PrepareTxnCtx(ctx) } diff --git a/session/session_test.go b/session/session_test.go index abeb64c2ce7fc..96670b909aa37 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -3547,3 +3547,24 @@ func (s *testSessionSuite2) TestRetryWithSet(c *C) { tk1.MustExec("commit") tk1.MustQuery("select c1 > 0 from t").Check(testkit.Rows("1")) } + +func (s *testSessionSuite2) TestRetryCommitWithSet(c *C) { + tk1 := testkit.NewTestKitWithInit(c, s.store) + tk1.MustExec("drop table if exists t") + tk1.MustExec("create table t(pk varchar(50), c1 varchar(50), c2 varchar(50), key k1(c1, c2), primary key(pk))") + tk1.MustExec("insert into t values ('1', '10', '100')") + + tk1.MustExec("set tidb_disable_txn_auto_retry = 0") + tk1.MustExec("set autocommit = 0") + tk1.MustExec("set tidb_txn_mode = ''") + tk2 := testkit.NewTestKitWithInit(c, s.store) + tk1.MustExec("update t set c1 = '11' where pk = '1'") + tk1.MustExec("update t set c2 = '101' where pk = '1'") + tk2.MustExec("begin optimistic") + tk2.MustQuery("select * from t for update").Check(testkit.Rows("1 10 100")) + tk2.MustExec("commit") + tk1.MustExec("set autocommit = 1") + tk2.MustExec("admin check table t") + tk2.MustQuery("select * from t use index(k1)").Check(testkit.Rows("1 11 101")) + tk2.MustQuery("select * from t where pk = '1'").Check(testkit.Rows("1 11 101")) +}