Bug #67650 Ping command always reconnects to the database
Submitted: 20 Nov 2012 11:53 Modified: 3 Jan 2013 13:09
Reporter: Rudy Metzger Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:1.0.7 OS:Any
Assigned to: Geert Vanderkelen CPU Architecture:Any

[20 Nov 2012 11:53] Rudy Metzger
Description:
Currently, the ping command always reconnects to the database, regardless if there is already a connection or not. While this is unnecessary, the more severe impact is, that this will interrupt every possible pending transaction!

START TRANSACTION
UPDATE foo
ping()
UPDATE faa
COMMIT

Only faa will be committed, foo is rolled back as ping() creates a new connection.

NOTE: currently ping() also always reconnects if there is an existing database connection, regardless of the parameter "reconnect"

How to repeat:
    def ping(self, reconnect=False, attempts=1, delay=0):
        """Check availability to the MySQL server

        When reconnect is set to True, one or more attempts are made to try
        to reconnect to the MySQL server using the reconnect()-method.

        delay is the number of seconds to wait between each retry.

        When the connection is not available, an InterfaceError is raised. Use
        the is_connected()-method if you just want to check the connection
        without raising an error.

        Raises InterfaceError on errors.
        """
        try:
            self.cmd_ping()
        except:
            if not reconnect:
                raise errors.InterfaceError("Connection to MySQL is"
                                            " not available.")

        self.reconnect(attempts=attempts, delay=delay)

Suggested fix:
The preferred solution would be to have ping() only reconnect if:
a) the connection has gone away AND
b) the parameter "reconnect" is set to True

The parameter "reconnect" should only be considered if the connection has gone away.
[26 Nov 2012 8:43] Geert Vanderkelen
Verified with 1.0.7. This is an indent gone wrong. Thanks for reporting this.
[3 Jan 2013 13:09] Geert Vanderkelen
Fixed with MySQL Connector/Python v1.0.8