1
0
mirror of https://github.com/golang/go synced 2024-10-04 20:21:22 -06:00
go/src/pkg/database/sql
Brad Fitzpatrick 277047f52a database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.

The core of this patch is removing one line:

     s.db.addDep(dc, s)

A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).

The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.

Instead, I now track per-Conn open statements and close them
when the the conn closes.  The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.

I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.

I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.

I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking.  It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.

Fixes #5323

R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
..
driver database/sql: refcounting and lifetime fixes 2013-02-20 15:35:27 -08:00
convert_test.go database/sql: optimized []byte copy + []byte(nil) -> *interface fix 2013-03-25 17:43:30 -07:00
convert.go database/sql: optimized []byte copy + []byte(nil) -> *interface fix 2013-03-25 17:43:30 -07:00
doc.txt database/sql: fix doc references to old package name 2013-02-13 18:47:25 -08:00
example_test.go database/sql: document args, add a couple examples 2013-01-11 14:46:49 -08:00
fakedb_test.go database/sql: fix driver Conn refcounting with prepared statements 2013-04-25 14:45:56 -07:00
sql_test.go database/sql: fix driver Conn refcounting with prepared statements 2013-04-25 14:45:56 -07:00
sql.go database/sql: fix driver Conn refcounting with prepared statements 2013-04-25 14:45:56 -07:00