Skip to content

Commit

Permalink
tidb: Fix bug for stmt retry and add unit test case
Browse files Browse the repository at this point in the history
  • Loading branch information
shenli committed Nov 12, 2015
1 parent d5cf283 commit b00955f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (c *driverConn) Commit() error {
}
_, err := c.s.Execute(txCommitSQL)

if terror.ErrorEqual(err, kv.ErrConditionNotMatch) {
if kv.IsRetryableError(err) {
return c.s.Retry()
}

Expand All @@ -259,7 +259,7 @@ func (c *driverConn) Commit() error {
}

err = c.s.FinishTxn(false)
if terror.ErrorEqual(err, kv.ErrConditionNotMatch) {
if kv.IsRetryableError(err) {
return c.s.Retry()
}
return errors.Trace(err)
Expand Down
3 changes: 1 addition & 2 deletions tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/pingcap/tidb/store/localstore/boltdb"
"github.com/pingcap/tidb/store/localstore/engine"
"github.com/pingcap/tidb/store/localstore/goleveldb"
"github.com/pingcap/tidb/terror"
)

// Engine prefix name
Expand Down Expand Up @@ -217,7 +216,7 @@ func runStmt(ctx context.Context, s stmt.Statement, args ...interface{}) (rset.R
if err == nil && (s.IsDDL() || autocommit.ShouldAutocommit(ctx)) {
err = ctx.FinishTxn(false)
// We should retry for autocommit
if terror.ErrorEqual(err, kv.ErrConditionNotMatch) {
if kv.IsRetryableError(err) {
se, ok := ctx.(Session)
if ok {
return nil, se.Retry()
Expand Down
35 changes: 35 additions & 0 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,41 @@ func (s *testSessionSuite) TestFieldText(c *C) {
}
}

// For https://github.com/pingcap/tidb/issues/571
func (s *testSessionSuite) TestIssue571(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)

mustExecSQL(c, se, "begin")
mustExecSQL(c, se, "drop table if exists t")
mustExecSQL(c, se, "create table t (c int)")
mustExecSQL(c, se, "insert t values (1), (2), (3)")
mustExecSQL(c, se, "commit")

se1 := newSession(c, store, s.dbName)
mustExecSQL(c, se1, "SET SESSION autocommit=1;")
se2 := newSession(c, store, s.dbName)
mustExecSQL(c, se2, "SET SESSION autocommit=1;")

var wg sync.WaitGroup
wg.Add(2)
f1 := func() {
defer wg.Done()
for i := 0; i < 30; i++ {
mustExecSQL(c, se1, "update t set c = 1;")
}
}
f2 := func() {
defer wg.Done()
for i := 0; i < 30; i++ {
mustExecSQL(c, se2, "update t set c = 1;")
}
}
go f1()
go f2()
wg.Wait()
}

func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
c.Assert(err, IsNil)
Expand Down

0 comments on commit b00955f

Please sign in to comment.