MySQLdb reconnect

Discussion in 'Python' started by Damjan, Jun 22, 2005.

  1. Damjan

    Damjan Guest

    Does MySQLdb automatically reconnect if the connection to the database is
    broken?

    I'm asking this since I have a longrunning Python precess that is connected
    to Mysql-4.1.11, and I execute "set names utf8" when I connect to it.

    But after running a day the results from the python program were displayed
    as if the "set names utf8" was not executed i.e. I got question marks where
    utf-8 cyrillics should've appeared. After restarting the Python program
    everything was ok, just as when I first started it.

    The long running Python process is actually a scgi quixote web application.


    --
    damjan
     
    Damjan, Jun 22, 2005
    #1
    1. Advertising

  2. Damjan

    Eloff Guest

    I don't beleive that it does. You can however call ping() on the
    connection which should attempt an automatic reconnection.

    See the docs for mysql_ping:

    http://dev.mysql.com/doc/mysql/en/mysql-ping.html

    I've never tested that, but I have a need for it also so let me know if
    it works or not.

    -Dan
     
    Eloff, Jun 22, 2005
    #2
    1. Advertising

  3. Damjan

    Damjan Guest

    > Does MySQLdb automatically reconnect if the connection to the database is
    > broken?


    It seems so.

    > I'm asking this since I have a longrunning Python precess that is
    > connected to Mysql-4.1.11, and I execute "set names utf8" when I connect
    > to it.
    >
    > But after running a day the results from the python program were displayed
    > as if the "set names utf8" was not executed i.e. I got question marks
    > where utf-8 cyrillics should've appeared. After restarting the Python
    > program everything was ok, just as when I first started it.


    This is the sollution I've come to:

    try:
    # This will fail on MySQL < 4.1
    db = MySQLdb.connect(godot.dbhost, godot.dbuser, godot.dbpass,
    godot.dbname, use_unicode=1, init_command="set names utf8")
    except MySQLdb.OperationalError:
    db = MySQLdb.connect(godot.dbhost, godot.dbuser, godot.dbpass,
    godot.dbname, use_unicode=1)
    db.charset = 'utf8'


    --
    damjan
     
    Damjan, Jun 29, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Koser

    How to reconnect to an LDAP Server

    Stephan Koser, Jul 20, 2005, in forum: Java
    Replies:
    0
    Views:
    1,562
    Stephan Koser
    Jul 20, 2005
  2. Replies:
    0
    Views:
    581
  3. John_Woo
    Replies:
    0
    Views:
    1,348
    John_Woo
    Jun 8, 2006
  4. Replies:
    0
    Views:
    966
  5. Bob Graton

    Auto reconnect to EJB

    Bob Graton, Nov 18, 2006, in forum: Java
    Replies:
    2
    Views:
    406
    Danno
    Nov 18, 2006
Loading...

Share This Page