Skip to content

Commit

Permalink
*: refine code for StmtTxn (pingcap#5763)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Feb 8, 2018
1 parent 498bf74 commit c92b305
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 88 deletions.
2 changes: 1 addition & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Context interface {
StoreQueryFeedback(feedback interface{})

// StmtCommit flush all changes by the statement to the underlying transaction.
StmtCommit() error
StmtCommit()
// StmtRollback provides statement level rollback.
StmtRollback()
// StmtGetMutation gets the binlog mutation for current statement.
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func checkCases(tests []testCase, ld *executor.LoadDataInfo,
c.Assert(data, DeepEquals, tt.restData,
Commentf("data1:%v, data2:%v, data:%v", string(tt.data1), string(tt.data2), string(data)))
}
terror.Log(ctx.StmtCommit())
ctx.StmtCommit()
err1 = ctx.Txn().Commit(goctx.Background())
c.Assert(err1, IsNil)
r := tk.MustQuery(selectSQL)
Expand Down
7 changes: 3 additions & 4 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -279,7 +278,7 @@ func (e *DeleteExec) deleteSingleTable(goCtx goctx.Context) error {
batchSize := e.ctx.GetSessionVars().DMLBatchSize
for {
if batchDelete && rowCount >= batchSize {
terror.Log(e.ctx.StmtCommit())
e.ctx.StmtCommit()
if err := e.ctx.NewTxn(); err != nil {
// We should return a special error for batch insert.
return ErrBatchInsertFail.Gen("BatchDelete failed with error: %v", err)
Expand Down Expand Up @@ -347,7 +346,7 @@ func (e *DeleteExec) deleteSingleTableByChunk(goCtx goctx.Context) error {

for chunkRow := iter.Begin(); chunkRow != iter.End(); chunkRow = iter.Next() {
if batchDelete && rowCount >= batchDMLSize {
terror.Log(e.ctx.StmtCommit())
e.ctx.StmtCommit()
if err = e.ctx.NewTxn(); err != nil {
// We should return a special error for batch insert.
return ErrBatchInsertFail.Gen("BatchDelete failed with error: %v", err)
Expand Down Expand Up @@ -909,7 +908,7 @@ func (e *InsertExec) exec(goCtx goctx.Context, rows [][]types.Datum) (Row, error
continue
}
if batchInsert && rowCount >= batchSize {
terror.Log(e.ctx.StmtCommit())
e.ctx.StmtCommit()
if err := e.ctx.NewTxn(); err != nil {
// We should return a special error for batch insert.
return nil, ErrBatchInsertFail.Gen("BatchInsert failed with error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion new_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (s *testSessionSuite) TestInTrans(c *C) {
tk.MustExec("insert t values ()")
c.Assert(tk.Se.Txn().Valid(), IsTrue)
tk.MustExec("rollback")
c.Assert(tk.Se.Txn().Valid(), IsFalse)
c.Assert(tk.Se.Txn(), IsNil)
}

func (s *testSessionSuite) TestRetryPreparedStmt(c *C) {
Expand Down
4 changes: 2 additions & 2 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ func insertDataWithCommit(goCtx goctx.Context, prevData, curData []byte, loadDat
if !reachLimit {
break
}
terror.Log(loadDataInfo.Ctx.StmtCommit())
loadDataInfo.Ctx.StmtCommit()
// Make sure that there are no retries when committing.
if err = loadDataInfo.Ctx.RefreshTxnCtx(goCtx); err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -788,7 +788,7 @@ func (cc *clientConn) handleLoadData(goCtx goctx.Context, loadDataInfo *executor
}

txn := loadDataInfo.Ctx.Txn()
terror.Log(loadDataInfo.Ctx.StmtCommit())
loadDataInfo.Ctx.StmtCommit()
if err != nil {
if txn != nil && txn.Valid() {
if err1 := txn.Rollback(); err1 != nil {
Expand Down
Loading

0 comments on commit c92b305

Please sign in to comment.