MySQL connector checks connection in is_connect()
(.local/lib/python3.3/site-packages/mysql/connector/connection.py) but it doesn't try to reconnect. I add reconnect()
in is_connect()
and now I have no problem with "MySQL Connection not available."
before:
def is_connected(self):
try:
self.cmd_ping()
except:
return False # This method does not raise
return True
after:
def is_connected(self):
try:
#print("DEBUG: cmd_ping()")
self.cmd_ping()
except:
try:
print("DEBUG: reconnect()")
#self.reconnect(attempts=3, delay=1)
self.reconnect()
except:
print("DEBUG: is_connected(): False")
return False # This method does not raise
print("DEBUG: is_connected(): True")
return True
You can see DEBUG messages in [YOUR_LOGIN].pythonanywhere.com.server.log .
Now is_connected()
looks similar to ping()
so maybe it can be done this way:
def is_connected(self):
try:
self.ping(True) # ping() in place of cmd_ping()
except:
return False # This method does not raise
return True
Thera are two problems.
-
is_connected()
(and ping()
) calls reconnect()
and reconnect()
calls is_connected()
- it is recursion. It can create almost infinitive loop if mysql is turned off. Python will raise exception "maximum recursion depth exceeded" but after almost 1000 recursions.
-
is_connected()
is not the best place for reconnect()
. I think is_connected()
shouldn't make reconnection. But it is the simplest solution.