[Twisted-Python] Adbapi, cp_reconnect and MySQL

Yanick Dufresne yanick at iotum.com
Tue Jul 5 12:06:50 EDT 2005

Hi all, quick question:

In adbapi's Transaction.reopen:

    def reopen(self):
        if self._connection is None:

        if self._cursor is not None:

            self._cursor = self._connection.cursor()
            if not self._pool.reconnect:

My understand of the goal of cp_reconnect is to ensure that at the
beginning of a query, the connection be re-established if it timed out
since the last query (I understand that trying to reconnect in the
middle of an interaction could potentially have undesired and
unpredictable effects).

The code above seems to assume that self._connection.cursor() will raise
ConnectionLost if the connection has timed out.

My question is two-fold:
1) should it not specifically check for ConnectionLost rather than catch
2) I don't know about other backends, but MySQL will cheerfully give you
a cursor object whenever you ask for one, and only raises ConnectionLost
when you actually call execute on the cursor, making the code above
completely ineffective. Anyone has a clever solution to this, short of
inserting "            self._cursor.execute(self._pool.good_sql)" in the
try block? That works, but isn't exactly performance friendly...

Any help appreciated,

Yanick Dufresne
Iotum Corporation

More information about the Twisted-Python mailing list