Skip to content

Commit

Permalink
session: reset affected rows in retry. (pingcap#2949)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored and shenli committed Mar 29, 2017
1 parent a2eb611 commit ad13ca1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func (s *session) retry(maxCnt int) error {
log.Warnf("[%d] Retry [%d] query [%d]", connID, retryCnt, i)
}
s.sessionVars.StmtCtx = sr.stmtCtx
s.sessionVars.StmtCtx.ResetForRetry()
_, err = st.Exec(s)
if err != nil {
break
Expand Down
19 changes: 19 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2692,3 +2692,22 @@ func (s *testSessionSuite) TestRetryCleanTxn(c *C) {
c.Assert(se.Txn(), IsNil)
c.Assert(se.sessionVars.InTxn(), IsFalse)
}

func (s *testSessionSuite) TestRetryResetStmtCtx(c *C) {
defer testleak.AfterTest(c)()
dbName := "test_retry_reset_stmtctx"
se := newSession(c, s.store, dbName).(*session)
se.Execute("create table retrytxn (a int unique, b int)")
_, err := se.Execute("insert retrytxn values (1, 1)")
c.Assert(err, IsNil)
se.Execute("begin")
se.Execute("update retrytxn set b = b + 1 where a = 1")

// Make retryable error.
se2 := newSession(c, s.store, dbName)
se2.Execute("update retrytxn set b = b + 1 where a = 1")

err = se.CommitTxn()
c.Assert(err, IsNil)
c.Assert(se.AffectedRows(), Equals, uint64(1))
}
9 changes: 9 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,12 @@ func (sc *StatementContext) HandleTruncate(err error) error {
}
return err
}

// ResetForRetry resets the changed states during execution.
func (sc *StatementContext) ResetForRetry() {
sc.mu.Lock()
sc.mu.affectedRows = 0
sc.mu.foundRows = 0
sc.mu.warnings = nil
sc.mu.Unlock()
}
5 changes: 5 additions & 0 deletions sessionctx/variable/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ func (*testSessionSuite) TestSession(c *C) {
// For last insert id
ctx.GetSessionVars().SetLastInsertID(1)
c.Assert(ctx.GetSessionVars().LastInsertID, Equals, uint64(1))

ss.ResetForRetry()
c.Assert(ss.AffectedRows(), Equals, uint64(0))
c.Assert(ss.FoundRows(), Equals, uint64(0))
c.Assert(ss.WarningCount(), Equals, uint16(0))
}

0 comments on commit ad13ca1

Please sign in to comment.