Skip to content

no panic on closed connection reuse #143

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

Merged
merged 3 commits into from
Oct 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func (mc *mysqlConn) handleParams() (err error) {
}

func (mc *mysqlConn) Begin() (driver.Tx, error) {
if mc.netConn == nil {
return nil, errInvalidConn
}
err := mc.exec("START TRANSACTION")
if err == nil {
return &mysqlTx{mc}, err
Expand All @@ -122,6 +125,9 @@ func (mc *mysqlConn) Close() (err error) {
}

func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
if mc.netConn == nil {
return nil, errInvalidConn
}
// Send command
err := mc.writeCommandPacketStr(comStmtPrepare, query)
if err != nil {
Expand Down Expand Up @@ -150,6 +156,9 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
}

func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
if mc.netConn == nil {
return nil, errInvalidConn
}
if len(args) == 0 { // no args, fastpath
mc.affectedRows = 0
mc.insertId = 0
Expand Down Expand Up @@ -191,6 +200,9 @@ func (mc *mysqlConn) exec(query string) error {
}

func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
if mc.netConn == nil {
return nil, errInvalidConn
}
if len(args) == 0 { // no args, fastpath
// Send command
err := mc.writeCommandPacketStr(comQuery, query)
Expand Down
34 changes: 34 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows)
return rows
}

func TestReuseClosedConnection(t *testing.T) {
// this test does not use sql.database, it uses the driver directly
if !available {
t.Skipf("MySQL-Server not running on %s", netAddr)
}
driver := &MySQLDriver{}
conn, err := driver.Open(dsn)
if err != nil {
t.Fatalf("Error connecting: %s", err.Error())
}
stmt, err := conn.Prepare("DO 1")
if err != nil {
t.Fatalf("Error preparing statement: %s", err.Error())
}
_, err = stmt.Exec(nil)
if err != nil {
t.Fatalf("Error executing statement: %s", err.Error())
}
err = conn.Close()
if err != nil {
t.Fatalf("Error closing connection: %s", err.Error())
}
defer func() {
if err := recover(); err != nil {
t.Errorf("Panic after reusing a closed connection: %v", err)
}
}()
_, err = stmt.Exec(nil)
if err != nil && err != errInvalidConn {
t.Errorf("Unexpected error '%s', expected '%s'",
err.Error(), errInvalidConn.Error())
}
}

func TestCharset(t *testing.T) {
if !available {
t.Skipf("MySQL-Server not running on %s", netAddr)
Expand Down
6 changes: 6 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (stmt *mysqlStmt) NumInput() int {
}

func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
if stmt.mc.netConn == nil {
return nil, errInvalidConn
}
// Send command
err := stmt.writeExecutePacket(args)
if err != nil {
Expand Down Expand Up @@ -70,6 +73,9 @@ func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
}

func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
if stmt.mc.netConn == nil {
return nil, errInvalidConn
}
// Send command
err := stmt.writeExecutePacket(args)
if err != nil {
Expand Down