Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ServerStatusInTrans flag #159

Merged
merged 11 commits into from
Sep 16, 2015
9 changes: 9 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func (s *SessionVars) SetStatus(status uint16) {
s.Status = status
}

// SetStatusInTrans sets the status flags about ServerStatusInTrans.
func (s *SessionVars) SetStatusInTrans(isInTrans bool) {
if isInTrans {
s.Status |= mysql.ServerStatusInTrans
return
}
s.Status &= (^mysql.ServerStatusInTrans)
}

// GetNextPreparedStmtID generates and return the next session scope prepared statement id
func (s *SessionVars) GetNextPreparedStmtID() uint32 {
s.preparedStmtID++
Expand Down
3 changes: 3 additions & 0 deletions stmt/stmts/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (s *BeginStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
// the transaction with COMMIT or ROLLBACK. The autocommit mode then
// reverts to its previous state.
variable.GetSessionVars(ctx).DisableAutocommit = true
variable.GetSessionVars(ctx).SetStatusInTrans(true)
return
}

Expand Down Expand Up @@ -97,6 +98,7 @@ func (s *CommitStmt) SetText(text string) {
func (s *CommitStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
err = ctx.FinishTxn(false)
variable.GetSessionVars(ctx).DisableAutocommit = false
variable.GetSessionVars(ctx).SetStatusInTrans(false)
return
}

Expand Down Expand Up @@ -130,5 +132,6 @@ func (s *RollbackStmt) SetText(text string) {
func (s *RollbackStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
err = ctx.FinishTxn(true)
variable.GetSessionVars(ctx).DisableAutocommit = false
variable.GetSessionVars(ctx).SetStatusInTrans(false)
return
}
39 changes: 39 additions & 0 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,45 @@ func (s *testSessionSuite) TestAutoicommit(c *C) {
mustExecSQL(c, se, s.dropDBSQL)
}

func checkInTrans(c *C, se Session, stmt string, isNil bool, expect uint16) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems no need to use expect or isNil variable? Use one of them is enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep.

mustExecSQL(c, se, stmt)
if isNil {
c.Assert(se.(*session).txn, IsNil)
} else {
c.Assert(se.(*session).txn, NotNil)
}
ret := variable.GetSessionVars(se.(*session)).Status & mysql.ServerStatusInTrans
c.Assert(ret, Equals, expect)
}

// See: https://dev.mysql.com/doc/internals/en/status-flags.html
func (s *testSessionSuite) TestInTrans(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)
mustExecSQL(c, se, "drop table if exists t")
c.Assert(se.(*session).txn, IsNil)
checkInTrans(c, se, "create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)", true, 0)
checkInTrans(c, se, "insert t values ()", true, 0)
checkInTrans(c, se, "begin", false, 1)
checkInTrans(c, se, "insert t values ()", false, 1)
se.Execute("drop table if exists t;")
c.Assert(se.(*session).txn, IsNil)
c.Assert(variable.GetSessionVars(se.(*session)).Status&mysql.ServerStatusInTrans, Equals, uint16(1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little confused, here why txn is nil but in ServerStatusInTrans?

checkInTrans(c, se, "create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)", true, 1)
checkInTrans(c, se, "insert t values ()", false, 1)
checkInTrans(c, se, "commit", true, 0)
checkInTrans(c, se, "insert t values ()", true, 0)

se.Execute("drop table if exists t;")
mustExecSQL(c, se, "create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)")
c.Assert(variable.GetSessionVars(se.(*session)).Status&mysql.ServerStatusInTrans, Equals, uint16(0))
checkInTrans(c, se, "begin", false, 1)
checkInTrans(c, se, "insert t values ()", false, 1)
checkInTrans(c, se, "rollback", true, 0)

mustExecSQL(c, se, s.dropDBSQL)
}

// See: http://dev.mysql.com/doc/refman/5.7/en/commit.html
func (s *testSessionSuite) TestRowLock(c *C) {
store := newStore(c, s.dbName)
Expand Down