Skip to content

Commit

Permalink
*: clear delta info when roll back transaction (#5390)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx committed Dec 13, 2017
1 parent 12299b8 commit 713bc79
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions executor/simple.go
Expand Up @@ -128,6 +128,7 @@ func (e *SimpleExec) executeRollback(s *ast.RollbackStmt) error {
log.Infof("[%d] execute rollback statement", sessVars.ConnectionID)
sessVars.SetStatusFlag(mysql.ServerStatusInTrans, false)
if e.ctx.Txn().Valid() {
e.ctx.GetSessionVars().TxnCtx.ClearDelta()
return e.ctx.Txn().Rollback()
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/session.go
Expand Up @@ -101,6 +101,11 @@ func (tc *TransactionContext) UpdateDeltaForTable(tableID int64, delta int64, co
tc.TableDeltaMap[tableID] = item
}

// ClearDelta clears the delta map.
func (tc *TransactionContext) ClearDelta() {
tc.TableDeltaMap = nil
}

// SessionVars is to handle user-defined or global variables in the current session.
type SessionVars struct {
// UsersLock is a lock for user defined variables.
Expand Down
23 changes: 23 additions & 0 deletions statistics/update_test.go
Expand Up @@ -134,6 +134,29 @@ func (s *testStatsUpdateSuite) TestSingleSessionInsert(c *C) {
rs.Check(testkit.Rows("40", "70"))
}

func (s *testStatsUpdateSuite) TestRollback(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("create table t (a int, b int)")
testKit.MustExec("begin")
testKit.MustExec("insert into t values (1,2)")
testKit.MustExec("rollback")

is := s.do.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tableInfo := tbl.Meta()
h := s.do.StatsHandle()
h.HandleDDLEvent(<-h.DDLEventCh())
h.DumpStatsDeltaToKV()
h.Update(is)

stats := h.GetTableStats(tableInfo.ID)
c.Assert(stats.Count, Equals, int64(0))
c.Assert(stats.ModifyCount, Equals, int64(0))
}

func (s *testStatsUpdateSuite) TestMultiSession(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
Expand Down

0 comments on commit 713bc79

Please sign in to comment.