"ping" not reconnecting in Python MySQLdb client interface

J

John Nagle

I have some long-running Python programs that can be idle
for hours, and, of course, the MySQL connection times out.
So I call

db.ping()

at the beginning of a new request cycle. This should
reestablish the connection, but it doesn't:

Traceback (most recent call last):
File "rateapiv1.fcgi", line 60, in QuickSitetruthQuery
db.ping() # reattach connection if necessary
OperationalError: (2006, 'MySQL server has gone away')

The MySQL server is up, and new connection attempts succeed.

This problem was reported two years ago relative to TurboGears:

http://trac.turbogears.org/ticket/872

I suspect that MySQL has auto-reconnect turned off, but doesn't document this.

(MySQL 5 on Fedora Core)

John Nagle
 
J

John Nagle

JJohn said:
> I have some long-running Python programs that can be idle
> for hours, and, of course, the MySQL connection times out.
> So I call
>
> db.ping()
>
> at the beginning of a new request cycle. This should
> reestablish the connection, but it doesn't. ....
> I suspect that MySQL has auto-reconnect turned off, but doesn't document
> this.

That seems to be the case. MySQLdb.connect turns off auto-reconnect, and
doesn't document "ping".

I'm currently trying this:

if db : # if previously attached
try :
db.ping() # test connection
except MySQLdb.OperationalError, message: # loss of connection
db = None # we lost database connection
if db is None : # if no valid database handle
db = MySQLdb.connect(...) # connect to database

which is a bit ugly.

John Nagle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top