Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
database/sql: notify driver connection when returning to pool #22049
Connections are re-used between queries. However connection sessions are state-full and modifying that state may affect the next query. It would be beneficial to have a means of notifying the driver when a connection is being returned to the pool asynchronously to user code to allow driver connections to reset their session.
This would also allow driver connections to notify the pool that their connection state is bad, while still returning the real error to the user.
This would also fix #20807
I don't know any implications regarding Oracle.
Where has this requirement appeared?
Maybe @mjibson has some suggestions.
I also like how it cleanly handles the case of notifying the pool of bad driver connections.
@tgulacsi This was opened from a MS SQL Server driver issue, but I also have personal interest in it. Most of the drivers I'm aware of link session to connection, so the connection pool is really a session pool logically. This isn't really a problem, until a flaky query leave behind a temp table or something, then blows up the next time it gets run in the same session.
I agree Return is probably a bad name. Maybe ResetSession(...). Good point, in the above proposal I don't have good docs saying when this will be called. I'll fix to be clearer. It won't be called then the driver connection is busy with anything else, is will be called after the user code has finished the query, finished the rows, committed the Tx, or returned the dedicated connection.
A single database connection ususally maps to a single session. A connection pool is logically also a session pool. Most sessions have a way to reset the session state which is desirable to prevent one bad query from poisoning another later query with temp table name conflicts or other persistent session resources. It also lets drivers provide users with better error messages from queryies when the underlying transport or query method fails. Internally the driver connection should now be marked as bad, but return the actual connection. When ResetSession is called on the connection it should return driver.ErrBadConn to remove it from the connection pool. Previously drivers had to choose between meaningful error messages or poisoning the connection pool. Lastly update TestPoolExhaustOnCancel from relying on a WAIT query fixing a flaky timeout issue exposed by this change. Fixes #22049 Fixes #20807 Change-Id: I2b5df6d954a38d0ad93bf1922ec16e74c827274c Reviewed-on: https://go-review.googlesource.com/73033 Run-TryBot: Daniel Theophanes <firstname.lastname@example.org> TryBot-Result: Gobot Gobot <email@example.com> Reviewed-by: Ian Lance Taylor <firstname.lastname@example.org>