From 60ade2fe472ec02a784411cc90d051666e4e92a0 Mon Sep 17 00:00:00 2001 From: shenli Date: Fri, 25 Sep 2015 13:05:39 +0800 Subject: [PATCH] tidb-server: Fix issue 263 Open tidb connect with dsn which contains unexists schema causes a panic. Fix issue #263 --- tidb-server/server/conn.go | 5 ++++- tidb-server/server/server_test.go | 12 ++++++++++++ tidb-server/server/tidb_test.go | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tidb-server/server/conn.go b/tidb-server/server/conn.go index a96b0cccf7..5abfaec02b 100644 --- a/tidb-server/server/conn.go +++ b/tidb-server/server/conn.go @@ -107,7 +107,10 @@ func (cc *clientConn) Close() error { delete(cc.server.clients, cc.connectionID) cc.server.rwlock.Unlock() cc.conn.Close() - return cc.ctx.Close() + if cc.ctx != nil { + return cc.ctx.Close() + } + return nil } func (cc *clientConn) writeInitialHandshake() error { diff --git a/tidb-server/server/server_test.go b/tidb-server/server/server_test.go index 4e2badcd8c..60db7ba32f 100644 --- a/tidb-server/server/server_test.go +++ b/tidb-server/server/server_test.go @@ -236,3 +236,15 @@ func runTestAuth(c *C) { c.Assert(err, NotNil, Commentf("Wrong password should be failed")) db.Close() } + +func runTestIssues(c *C) { + // For issue #263 + unExistsSchemaDsn := "root@tcp(localhost:4001)/unexists_schema?strict=true" + db, err := sql.Open("mysql", unExistsSchemaDsn) + c.Assert(db, NotNil) + c.Assert(err, IsNil) + // Open may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping. + err = db.Ping() + c.Assert(err, NotNil, Commentf("Connecting to an unexists schema should be error")) + db.Close() +} diff --git a/tidb-server/server/tidb_test.go b/tidb-server/server/tidb_test.go index b5141f98e3..10579a7abc 100644 --- a/tidb-server/server/tidb_test.go +++ b/tidb-server/server/tidb_test.go @@ -71,3 +71,7 @@ func (ts *TidbTestSuite) TestConcurrentUpdate(c *C) { func (ts *TidbTestSuite) TestAuth(c *C) { runTestAuth(c) } + +func (ts *TidbTestSuite) TestIssues(c *C) { + runTestIssues(c) +}