Skip to content
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

database/sql: dangling statement(s) #3865

Closed
alexbrainman opened this issue Jul 26, 2012 · 13 comments
Closed

database/sql: dangling statement(s) #3865

alexbrainman opened this issue Jul 26, 2012 · 13 comments
Assignees
Milestone

Comments

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Jul 26, 2012

What steps will reproduce the problem?
1. apply this diff to the go tree (hg id is ddaabb722563+)

diff -r ddaabb722563 src/pkg/database/sql/sql_test.go
--- a/src/pkg/database/sql/sql_test.go  Sun Jul 22 16:35:53 2012 -0700
+++ b/src/pkg/database/sql/sql_test.go  Thu Jul 26 10:24:41 2012 +1000
@@ -415,6 +415,28 @@
    }
 }
 
+func TestCloseStmtBeforeRows(t *testing.T) {
+   db := newTestDB(t, "people")
+   defer closeDB(t, db)
+
+   s, err := db.Prepare("SELECT|people|name|")
+   if err != nil {
+       t.Fatal(err)
+   }
+
+   r, err := s.Query()
+   if err != nil {
+       s.Close()
+       t.Fatal(err)
+   }
+   defer r.Close()
+
+   err = s.Close()
+   if err != nil {
+       t.Fatal(err)
+   }
+}
+
 // Tests fix for issue #2788, that we bind nil to a []byte if the
 // value in the column is sql null
 func TestNullByteSlice(t *testing.T) {

2. run "go test -v -run=Sim" command

What is the expected output?

Test should PASS

What do you see instead?

# go test
--- FAIL: TestCloseStmtBeforeRows (0.00 seconds)
sql_test.go:68:         error closing DB: can't close; dangling statement(s)
FAIL
exit status 1
FAIL    database/sql    0.011s
#
@gwenn
Copy link

@gwenn gwenn commented Aug 31, 2012

Comment 1:

The Rows (r) associated to the Stmt (s) isn't closed before the call to s.Close()
preventing the underlying driver.Stmt from being closed.
But there is no error raised in this case.
If a fix is made to raise an error on s.Close(), the test will still fail.
So I suggest to fix the test instead and call r.Close() before s.Close().
Or maybe the Rows associated to the Stmt should be closed automatically when the Stmt is
closed.
@alexbrainman
Copy link
Member Author

@alexbrainman alexbrainman commented Sep 3, 2012

Comment 2:

Did you read database/sql/driver/Stmt.Close documentation?
From http://golang.org/pkg/database/sql/driver/#Stmt: >>>
    // Closing a statement should not interrupt any outstanding
    // query created from that statement. That is, the following
    // order of operations is valid:
    //
    //  * create a driver statement
    //  * call Query on statement, returning Rows
    //  * close the statement
    //  * read from Rows
    //
<<<
I think my test here is fine.
Alex
@gwenn
Copy link

@gwenn gwenn commented Sep 4, 2012

Comment 3:

Sorry, in Nov 2011, there was no such documentation (when I started to implement it for
SQLite). I should have read it again.
I am curious to know if there is any driver that is conform to the documentation.
@alexbrainman
Copy link
Member Author

@alexbrainman alexbrainman commented Sep 4, 2012

Comment 4:

I tried. Probably, too hard.
http://code.google.com/p/odbc/source/browse/mssql_test.go#582
Alex
@gwenn
Copy link

@gwenn gwenn commented Sep 8, 2012

Comment 5:

Ok, I've just fixed driver.Stmt.Close in gosqlite like you so it will behave correctly
when this issue will be fixed.
Regards.
@rsc
Copy link
Contributor

@rsc rsc commented Sep 12, 2012

Comment 6:

Labels changed: added go1.1.

@rsc
Copy link
Contributor

@rsc rsc commented Dec 10, 2012

Comment 7:

Labels changed: added size-m.

@rsc
Copy link
Contributor

@rsc rsc commented Dec 10, 2012

Comment 8:

Labels changed: added suggested.

@rsc
Copy link
Contributor

@rsc rsc commented Jan 31, 2013

Comment 9:

Labels changed: added priority-later, removed priority-soon.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Feb 13, 2013

Comment 10:

This issue was updated by revision 8f2430a.

R=golang-dev, alex.brainman, nightlyone
CC=golang-dev
https://golang.org/cl/7324051
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Feb 20, 2013

Comment 11:

CL out for review: https://golang.org/cl/7363043

Status changed to Started.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Feb 20, 2013

Comment 12:

This issue was closed by revision f7a7716.

Status changed to Fixed.

@alexbrainman
Copy link
Member Author

@alexbrainman alexbrainman commented Feb 24, 2013

Comment 13:

Brad,
This little test
diff --git a/src/pkg/database/sql/sql_test.go b/src/pkg/database/sql/sql_test.go
--- a/src/pkg/database/sql/sql_test.go
+++ b/src/pkg/database/sql/sql_test.go
@@ -708,3 +708,17 @@
        t.Errorf("error = %q; want %q", err.Error(), want)
    }
 }
+
+func TestALEX(t *testing.T) {
+   db := newTestDB(t, "people")
+   defer closeDB(t, db)
+   stmt, err := db.Prepare("INSERT|people|name=?,age=?")
+   if err != nil {
+       t.Fatal(err)
+   }
+   defer stmt.Close()
+   _, err = stmt.Exec("Alice", 10)
+   if err != nil {
+       t.Fatal(err)
+   }
+}
always creates two driver connections. stmt.Close closes only one of those.
Is this expected? It feels wrong to me on both counts.
Alex
@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.