Skip to content

Commit a8186bb

Browse files
committed
correctly close rows objects if there are multiple result sets
1 parent 8837942 commit a8186bb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

conn.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,12 @@ func (rs *rows) Close() error {
13391339
switch err {
13401340
case nil:
13411341
case io.EOF:
1342-
return nil
1342+
// rs.Next can return io.EOF on both 'Z' (ready for query) and 'T' (row
1343+
// description, used with HasNextResultSet). We need to fetch messages until
1344+
// we hit a 'Z', which is done by waiting for done to be set.
1345+
if rs.done {
1346+
return nil
1347+
}
13431348
default:
13441349
return err
13451350
}

conn_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -1583,3 +1583,32 @@ func TestRowsResultTag(t *testing.T) {
15831583
}
15841584
}
15851585
}
1586+
1587+
// TestQuickClose tests that closing a query early allows a subsequent query to work.
1588+
func TestQuickClose(t *testing.T) {
1589+
db := openTestConn(t)
1590+
defer db.Close()
1591+
1592+
tx, err := db.Begin()
1593+
if err != nil {
1594+
t.Fatal(err)
1595+
}
1596+
rows, err := tx.Query("SELECT 1; SELECT 2;")
1597+
if err != nil {
1598+
t.Fatal(err)
1599+
}
1600+
if err := rows.Close(); err != nil {
1601+
t.Fatal(err)
1602+
}
1603+
1604+
var id int
1605+
if err := tx.QueryRow("SELECT 3").Scan(&id); err != nil {
1606+
t.Fatal(err)
1607+
}
1608+
if id != 3 {
1609+
t.Fatalf("unexpected %d", id)
1610+
}
1611+
if err := tx.Commit(); err != nil {
1612+
t.Fatal(err)
1613+
}
1614+
}

0 commit comments

Comments
 (0)