Opened 7 years ago

Last modified 3 weeks ago

#4964 defect assigned

adbapi.Transaction.reopen doesn't reconnect when mysql backend closes a connection

Reported by: Jay Sweeney Owned by: James Hilliard
Priority: high Milestone:
Component: core Keywords: connection, adbapi, transaction, error
Cc: Jay Sweeney, posita Branch:


Hi twisteds,

Not entirely sure if this is a bug with mysql drivers/backend or with adbapi, but, the behaviour I'm seeing is that if a mysql decides to close a client connection, then a connection in adbapi is not reconnecting even if the ConnectionPool has self.reconnect=True.

An easy way to test this is to set mysql's 'wait_timeout' setting to something very low and then create an adbapi pool and make queries separated by larger time intervals than the timeout setting. The result is that connections in the pool are disconnected by mysql and they never reconnect. I've tried this using both the MySQLdb and pymysql drivers.

The problem is, Line 106 of trunk in adbapi.Transaction.reopen:

self._cursor = self._connection.cursor()

is treated as though it would throw an exception if the underlying connection to the mysql server were closed. This is not the case. No exception is thrown by the .cursor() call on a dead connection. If you were to run, say, self._pool.good_sql, then the exception would indeed be thrown, and the connection would be reconnected as expected.

Attachments (1) (784 bytes) - added by Jay Sweeney 7 years ago.
example which triggers the problem

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by Jay Sweeney

Attachment: added

example which triggers the problem

comment:1 Changed 7 years ago by Jay Sweeney

Cc: Jay Sweeney added
Type: enhancementdefect

comment:2 Changed 7 years ago by Jay Sweeney

Priority: normalhigh

comment:4 Changed 4 years ago by Pesach Weinstock

Keywords: connection adbapi transaction error added

The link added by amberite above, referencing a possible solution, appears to be dead. An archived version of the solution can currently be found here:

comment:5 Changed 4 years ago by posita

Cc: posita added

comment:6 Changed 12 months ago by James Hilliard

Keywords: review added
Owner: set to James Hilliard
Status: newassigned

I have a possible fix for this here:

Note: See TracTickets for help on using tickets.