Skip to content

Commit

Permalink
Merge branch 'release-6.1' into cherry-pick-36051-to-release-6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivy-YinSu authored Jan 18, 2023
2 parents 17a382c + e96bc9b commit fe14d1c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -530,7 +531,7 @@ func getLockWaitTime(ctx sessionctx.Context, lockInfo *ast.SelectLockInfo) (lock
// autocommit to 0. If autocommit is enabled, the rows matching the specification are not locked.
// See https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
sessVars := ctx.GetSessionVars()
if !sessVars.IsAutocommit() || sessVars.InTxn() {
if !sessVars.IsAutocommit() || sessVars.InTxn() || config.GetGlobalConfig().PessimisticTxn.PessimisticAutoCommit.Load() {
lock = true
waitTime = sessVars.LockWaitTimeout
if lockInfo.LockType == ast.SelectLockForUpdateWaitN {
Expand Down
16 changes: 15 additions & 1 deletion tests/realtikvtest/pessimistictest/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3108,13 +3108,21 @@ func TestPessimisticAutoCommitTxn(t *testing.T) {

tk.MustExec("set tidb_txn_mode = 'pessimistic'")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (i int)")
tk.MustExec("create table t (i int primary key)")
tk.MustExec("insert into t values (1)")
tk.MustExec("set autocommit = on")

rows := tk.MustQuery("explain update t set i = -i").Rows()
explain := fmt.Sprintf("%v", rows[1])
require.NotRegexp(t, ".*SelectLock.*", explain)
rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows()
explain = fmt.Sprintf("%v", rows[1])
require.Regexp(t, ".*handle:-1.*", explain)
require.NotRegexp(t, ".*handle:-1, lock.*", explain)
rows = tk.MustQuery("explain update t set i = -i where i in (-1, 1)").Rows()
explain = fmt.Sprintf("%v", rows[1])
require.Regexp(t, ".*handle:\\[-1 1\\].*", explain)
require.NotRegexp(t, ".*handle:\\[-1 1\\].*, lock.*", explain)

originCfg := config.GetGlobalConfig()
defer config.StoreGlobalConfig(originCfg)
Expand All @@ -3125,6 +3133,12 @@ func TestPessimisticAutoCommitTxn(t *testing.T) {
rows = tk.MustQuery("explain update t set i = -i").Rows()
explain = fmt.Sprintf("%v", rows[1])
require.Regexp(t, ".*SelectLock.*", explain)
rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows()
explain = fmt.Sprintf("%v", rows[1])
require.Regexp(t, ".*handle:-1, lock.*", explain)
rows = tk.MustQuery("explain update t set i = -i where i in (-1, 1)").Rows()
explain = fmt.Sprintf("%v", rows[1])
require.Regexp(t, ".*handle:\\[-1 1\\].*, lock.*", explain)
}

func TestPessimisticLockOnPartition(t *testing.T) {
Expand Down

0 comments on commit fe14d1c

Please sign in to comment.