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.