DATA-DOG / go-txdb

Immutable transaction isolated sql driver for golang
Other
667 stars 48 forks source link

txDriver.db is closed when last connection is closed #29

Closed sfllaw closed 4 years ago

sfllaw commented 4 years ago

database/sql.DB.Close closes all connections, but txdb did not actually do this:

txdb.Register("txdb", "driver", "dsn")
db, _ := sql.Open("txdb", "identifier") // Called db.Begin()
db.Close()                              // tx left open

This bug would block code from locking the entire underlying database after txdb grabbed its connection. For example, if TestMain created a temporary test database and opened it with txdb, it would be impossible to drop this database after the test was over.

This patch defers beginning a transaction until it is actually needed, which is when the connection will be opened.

When the number of txDriver.conns drops to zero, it closes the root txDriver.db. Since database/sql maintains a free pool of connections, this practically happens only when DB.Close is called.

Code that used to make database calls after DB.Close will now get a "sql: database is closed" error. They should either hoist the DB.Close call to where the txdb was opened, or reopen it again.

Closes: #28

l3pp4rd commented 4 years ago

thanks ;)