"ping" not reconnecting in Python MySQLdb client interface

This is a discussion on "ping" not reconnecting in Python MySQLdb client interface within the MySQL Database forums, part of the Database Forums category; I have some long-running Python programs that can be idle for hours, and, of course, the MySQL connection times ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-03-2008
John Nagle
 
Posts: n/a
Default "ping" not reconnecting in Python MySQLdb client interface

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
Reply With Quote
  #2 (permalink)  
Old 02-03-2008
John Nagle
 
Posts: n/a
Default Re: "ping" not reconnecting in Python MySQLdb client interface

JJohn Nagle wrote:
> 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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:09 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0