@@ -545,7 +545,7 @@ func (s *testSessionSuite) TestRetryCleanTxn(c *C) {
545
545
c .Assert (err , IsNil )
546
546
stmt , _ := session .Compile (context .TODO (), tk .Se , stmtNode )
547
547
executor .ResetContextOfStmt (tk .Se , stmtNode )
548
- history .Add (0 , stmt , tk .Se .GetSessionVars ().StmtCtx )
548
+ history .Add (stmt , tk .Se .GetSessionVars ().StmtCtx )
549
549
_ , err = tk .Exec ("commit" )
550
550
c .Assert (err , NotNil )
551
551
txn , err := tk .Se .Txn (false )
@@ -559,6 +559,7 @@ func (s *testSessionSuite) TestReadOnlyNotInHistory(c *C) {
559
559
tk .MustExec ("create table history (a int)" )
560
560
tk .MustExec ("insert history values (1), (2), (3)" )
561
561
tk .MustExec ("set @@autocommit = 0" )
562
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 0" )
562
563
tk .MustQuery ("select * from history" )
563
564
history := session .GetHistory (tk .Se )
564
565
c .Assert (history .Count (), Equals , 0 )
@@ -572,6 +573,76 @@ func (s *testSessionSuite) TestReadOnlyNotInHistory(c *C) {
572
573
c .Assert (history .Count (), Equals , 0 )
573
574
}
574
575
576
+ func (s * testSessionSuite ) TestNoHistoryWhenDisableRetry (c * C ) {
577
+ tk := testkit .NewTestKitWithInit (c , s .store )
578
+ tk .MustExec ("create table history (a int)" )
579
+ tk .MustExec ("set @@autocommit = 0" )
580
+
581
+ // retry_limit = 0 will not add history.
582
+ tk .MustExec ("set @@tidb_retry_limit = 0" )
583
+ tk .MustExec ("insert history values (1)" )
584
+ c .Assert (session .GetHistory (tk .Se ).Count (), Equals , 0 )
585
+
586
+ // Disable auto_retry will add history for auto committed only
587
+ tk .MustExec ("set @@autocommit = 1" )
588
+ tk .MustExec ("set @@tidb_retry_limit = 10" )
589
+ tk .MustExec ("set @@tidb_disable_txn_auto_retry = 1" )
590
+ c .Assert (failpoint .Enable ("github.com/pingcap/tidb/session/keepHistory" , `1*return(true)->return(false)` ), IsNil )
591
+ tk .MustExec ("insert history values (1)" )
592
+ c .Assert (session .GetHistory (tk .Se ).Count (), Equals , 1 )
593
+ c .Assert (failpoint .Disable ("github.com/pingcap/tidb/session/keepHistory" ), IsNil )
594
+ tk .MustExec ("begin" )
595
+ tk .MustExec ("insert history values (1)" )
596
+ c .Assert (session .GetHistory (tk .Se ).Count (), Equals , 0 )
597
+ tk .MustExec ("commit" )
598
+
599
+ // Enable auto_retry will add history for both.
600
+ tk .MustExec ("set @@tidb_disable_txn_auto_retry = 0" )
601
+ c .Assert (failpoint .Enable ("github.com/pingcap/tidb/session/keepHistory" , `1*return(true)->return(false)` ), IsNil )
602
+ tk .MustExec ("insert history values (1)" )
603
+ c .Assert (failpoint .Disable ("github.com/pingcap/tidb/session/keepHistory" ), IsNil )
604
+ c .Assert (session .GetHistory (tk .Se ).Count (), Equals , 1 )
605
+ tk .MustExec ("begin" )
606
+ tk .MustExec ("insert history values (1)" )
607
+ c .Assert (session .GetHistory (tk .Se ).Count (), Equals , 2 )
608
+ tk .MustExec ("commit" )
609
+ }
610
+
611
+ func (s * testSessionSuite ) TestNoRetryForCurrentTxn (c * C ) {
612
+ tk := testkit .NewTestKitWithInit (c , s .store )
613
+ tk1 := testkit .NewTestKitWithInit (c , s .store )
614
+ tk .MustExec ("create table history (a int)" )
615
+ tk .MustExec ("insert history values (1)" )
616
+
617
+ // Firstly, disable retry.
618
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 1" )
619
+ tk .MustExec ("begin" )
620
+ tk .MustExec ("update history set a = 2" )
621
+ // Enable retry now.
622
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 0" )
623
+
624
+ tk1 .MustExec ("update history set a = 3" )
625
+ c .Assert (tk .ExecToErr ("commit" ), NotNil )
626
+ }
627
+
628
+ func (s * testSessionSuite ) TestRetryForCurrentTxn (c * C ) {
629
+ tk := testkit .NewTestKitWithInit (c , s .store )
630
+ tk1 := testkit .NewTestKitWithInit (c , s .store )
631
+ tk .MustExec ("create table history (a int)" )
632
+ tk .MustExec ("insert history values (1)" )
633
+
634
+ // Firstly, enable retry.
635
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 0" )
636
+ tk .MustExec ("begin" )
637
+ tk .MustExec ("update history set a = 2" )
638
+ // Disable retry now.
639
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 1" )
640
+
641
+ tk1 .MustExec ("update history set a = 3" )
642
+ tk .MustExec ("commit" )
643
+ tk .MustQuery ("select * from history" ).Check (testkit .Rows ("2" ))
644
+ }
645
+
575
646
// TestTruncateAlloc tests that the auto_increment ID does not reuse the old table's allocator.
576
647
func (s * testSessionSuite ) TestTruncateAlloc (c * C ) {
577
648
tk := testkit .NewTestKitWithInit (c , s .store )
@@ -990,6 +1061,7 @@ func (s *testSessionSuite) TestBinaryReadOnly(c *C) {
990
1061
id2 , _ , _ , err := tk .Se .PrepareStmt ("insert into t values (?)" )
991
1062
c .Assert (err , IsNil )
992
1063
tk .MustExec ("set autocommit = 0" )
1064
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 0" )
993
1065
_ , err = tk .Se .ExecutePreparedStmt (context .Background (), id , []types.Datum {types .NewDatum (1 )})
994
1066
c .Assert (err , IsNil )
995
1067
c .Assert (session .GetHistory (tk .Se ).Count (), Equals , 0 )
@@ -2177,6 +2249,7 @@ func (s *testSessionSuite) TestStatementCountLimit(c *C) {
2177
2249
defer func () {
2178
2250
config .GetGlobalConfig ().Performance .StmtCountLimit = saved
2179
2251
}()
2252
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 0" )
2180
2253
tk .MustExec ("begin" )
2181
2254
tk .MustExec ("insert into stmt_count_limit values (1)" )
2182
2255
tk .MustExec ("insert into stmt_count_limit values (2)" )
@@ -2195,6 +2268,7 @@ func (s *testSessionSuite) TestStatementCountLimit(c *C) {
2195
2268
func (s * testSessionSuite ) TestBatchCommit (c * C ) {
2196
2269
tk := testkit .NewTestKitWithInit (c , s .store )
2197
2270
tk .MustExec ("set tidb_batch_commit = 1" )
2271
+ tk .MustExec ("set tidb_disable_txn_auto_retry = 0" )
2198
2272
tk .MustExec ("create table t (id int)" )
2199
2273
saved := config .GetGlobalConfig ().Performance
2200
2274
config .GetGlobalConfig ().Performance .StmtCountLimit = 3
@@ -2440,7 +2514,7 @@ func (s *testSchemaSuite) TestDisableTxnAutoRetry(c *C) {
2440
2514
// session 1 starts a transaction early.
2441
2515
// execute a select statement to clear retry history.
2442
2516
tk1 .MustExec ("select 1" )
2443
- tk1 .Se .NewTxn (context .Background ())
2517
+ tk1 .Se .PrepareTxnCtx (context .Background ())
2444
2518
// session 2 update the value.
2445
2519
tk2 .MustExec ("update no_retry set id = 4" )
2446
2520
// AutoCommit update will retry, so it would not fail.
0 commit comments