#32 Interface to set MYSQL_OPT_RECONNECT

MySQLdb (53)

My hosting provider reboots their mysql database every
hour, causing my persistent mysqldb apps to crash with
"mysql server has gone away".

This patch exposes the MYSQL_OPT_RECONNECT flag in the
connect() method. You can now send a keyword argument
of "reconnect=1" to cause the mysql API to attempt to
reconnect if the connection is lost.


  • Paul Eastham

    Paul Eastham - 2006-05-13

    here's the patch

  • Sridhar Ratnakumar

    Logged In: YES

    Is not this patch applied yet to the latest release?

  • sophana

    sophana - 2006-12-27

    Logged In: YES
    Originator: NO

    This patch is not in the 1.2.2 and it should!
    It does apply correctly on 1.2.2

  • Charles V.

    Charles V. - 2007-05-14

    Logged In: YES
    Originator: NO

    Is there a reason to not apply this patch ?

  • Wladimir van der Laan

    Logged In: YES
    Originator: NO

    why is this still not in the latest release, it's very useful to include

  • phatmonkey

    phatmonkey - 2007-07-24

    Logged In: YES
    Originator: NO

    Please include! It is indeed very useful

  • runz

    runz - 2007-10-04

    Logged In: YES
    Originator: NO

    there is a bug in this patch. see the lines where the reconnect option told to the server (somewhere at line 570):

    if (reconnect > 0) {
    my_bool bool = reconnect;
    mysql_options(conn, MYSQL_OPT_RECONNECT, &bool);

    this if statement has to be tested against the connection (conn) like this:

    if ((reconnect > 0) && (conn)) {
    my_bool bool = reconnect;
    mysql_options(conn, MYSQL_OPT_RECONNECT, &bool);

    Otherwise false parameters to connection will cause a segmentation fault at this point, because the connection doesn't exist at all.

  • Bernd Zeimetz

    Bernd Zeimetz - 2007-10-06

    Logged In: YES
    Originator: NO

    The developers of Zenoss modified the patch a little bit:

    • if (conn && reconnect > 0) {
    • my_bool bool = reconnect;
    • mysql_options(conn, MYSQL_OPT_RECONNECT, &bool);
    • }

    find the updated patch here:

    Please apply to the next version,



  • darking

    darking - 2008-10-23

    Isn't there already the option "reconnect" in the latest version?


  • Andy Dustman

    Andy Dustman - 2008-10-23

    There is, as an optional argument to connection.ping().

    This is an extremely dangerous option that I'm not going to support. It's disabled in MySQL-5.0 and up for a reason: It silently reconnects to the database which rolls back any transaction you might have in progress.

    I suppose I could make it a conditional compilation option, but if you use transactions, you should never use this. Write your application to recover from errors instead.

  • Andy Dustman

    Andy Dustman - 2008-10-23

    Your patch will not be applied.


Log in to post a comment.