From a825f10e5e1fa45c37c8d9ea9db3a22dba7b2a98 Mon Sep 17 00:00:00 2001 From: shenli Date: Thu, 26 Nov 2015 15:10:32 +0800 Subject: [PATCH 1/2] tidb: Fix bug for add prepared stmt into history --- tidb.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tidb.go b/tidb.go index 6d166a3f8e..395572aca7 100644 --- a/tidb.go +++ b/tidb.go @@ -194,16 +194,11 @@ func runStmt(ctx context.Context, s stmt.Statement, args ...interface{}) (rset.R var rs rset.Recordset // before every execution, we must clear affectedrows. variable.GetSessionVars(ctx).SetAffectedRows(0) - switch s.(type) { + switch ts := s.(type) { case *stmts.PreparedStmt: - ps := s.(*stmts.PreparedStmt) - return runPreparedStmt(ctx, ps) + rs, err = runPreparedStmt(ctx, ts) case *stmts.ExecuteStmt: - es := s.(*stmts.ExecuteStmt) - rs, err = runExecute(ctx, es, args...) - if err != nil { - return nil, errors.Trace(err) - } + rs, err = runExecute(ctx, ts, args...) default: if s.IsDDL() { err = ctx.FinishTxn(false) @@ -215,6 +210,7 @@ func runStmt(ctx context.Context, s stmt.Statement, args ...interface{}) (rset.R rs, err = s.Exec(ctx) stmt.ClearExecArgs(ctx) } + // All the history should be added here. se := ctx.(*session) switch ts := s.(type) { case *stmts.PreparedStmt: From 8b75da26a9c9339df30378c0f9e8e572eb7aef36 Mon Sep 17 00:00:00 2001 From: shenli Date: Thu, 26 Nov 2015 15:54:30 +0800 Subject: [PATCH 2/2] *: Add test case for retry prepared statement --- session_test.go | 27 +++++++++++++++++++++++++++ tidb_test.go | 4 ---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/session_test.go b/session_test.go index c2ec056850..cde1203146 100644 --- a/session_test.go +++ b/session_test.go @@ -1168,6 +1168,33 @@ func (s *testSessionSuite) TestIssue571(c *C) { wg.Wait() } +func (s *testSessionSuite) TestRetryPreparedStmt(c *C) { + store := newStore(c, s.dbName) + se := newSession(c, store, s.dbName) + se1 := newSession(c, store, s.dbName) + se2 := newSession(c, store, s.dbName) + + mustExecSQL(c, se, "drop table if exists t") + c.Assert(se.(*session).txn, IsNil) + mustExecSQL(c, se, "create table t (c1 int, c2 int, c3 int)") + mustExecSQL(c, se, "insert t values (11, 2, 3)") + + mustExecSQL(c, se1, "begin") + mustExecSQL(c, se1, "update t set c2=? where c1=11;", 21) + + mustExecSQL(c, se2, "begin") + mustExecSQL(c, se2, "update t set c2=? where c1=11", 22) + mustExecSQL(c, se2, "commit") + + mustExecSQL(c, se1, "commit") + + se3 := newSession(c, store, s.dbName) + r := mustExecSQL(c, se3, "select c2 from t where c1=11") + row, err := r.FirstRow() + c.Assert(err, IsNil) + match(c, row, 21) +} + // Testcase for session func (s *testSessionSuite) TestSession(c *C) { store := newStore(c, s.dbName) diff --git a/tidb_test.go b/tidb_test.go index 3e45c16c1a..f7aa71d0e3 100644 --- a/tidb_test.go +++ b/tidb_test.go @@ -348,10 +348,6 @@ func exec(c *C, se Session, sql string, args ...interface{}) (rset.Recordset, er if err != nil { return nil, err } - err = se.DropPreparedStmt(stmtID) - if err != nil { - return nil, err - } return rs, nil }