Skip to content

Commit 5dc5bfe

Browse files
committed
Merge pull request #58 from pingcap/siddontang/fix-session-string-panic
tidb: fix string() panic if txn is nil
2 parents d9e3be8 + 0d53849 commit 5dc5bfe

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

session.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ func (s *session) String() string {
111111
"userName": s.userName,
112112
"currDBName": db.GetCurrentSchema(s),
113113
"sid": s.sid,
114-
"txn": s.txn.String(),
114+
}
115+
116+
if s.txn != nil {
117+
// if txn is committed or rolled back, txn is nil.
118+
data["txn"] = s.txn.String()
115119
}
116120

117121
b, _ := json.MarshalIndent(data, "", " ")

tidb_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ func (s *testSessionSuite) TestAffectedRows(c *C) {
348348

349349
}
350350

351+
func (s *testSessionSuite) TestString(c *C) {
352+
store := newStore(c, s.dbName)
353+
se := newSession(c, store, s.dbName)
354+
sessionExec(c, se, "select 1")
355+
// here to check the panic bug in String() when txn is nil after committed.
356+
c.Log(se.String())
357+
}
358+
351359
func (s *testSessionSuite) TestResultField(c *C) {
352360
store := newStore(c, s.dbName)
353361
se := newSession(c, store, s.dbName)

0 commit comments

Comments
 (0)