Lost connection to MySQL server during query

Discussion in 'Ruby' started by Eric Schwartz, Sep 14, 2004.

  1. The following script reliably gives me a DBI::DatabaseError

    $ cat /tmp/test.rb
    #!/usr/bin/ruby
    require 'dbi'

    dbh = DBI.connect("DBI:mysql:host=test.test;db=test", "test", "test")
    fork {
    fork {
    $stderr.close
    $stdout.close
    $stdin.close
    exec("/bin/ls >/dev/null 2>&1")
    }
    }
    sth = dbh.prepare("select * from machines")
    sth.execute

    $ /tmp/test.rb
    /usr/lib/ruby/1.8/DBD/Mysql/Mysql.rb:403:in `execute': Lost connection to MySQL server during query (DBI::DatabaseError)
    from /usr/lib/ruby/1.8/dbi/dbi.rb:769:in `execute'
    from /tmp/test.rb:11
    $

    The context this problem came from is: I have a CGI program that must
    fire off a long-running process. I want to use the standard Unix
    double-fork trick to allow the long-running process to detach itself
    and run independently. My first thought was that if there's an error
    in the child process, it exits first, and closes the DBI connection
    automatically. That does NOT seem to be the case, however, as if I
    change the exec() to

    exec('sleep 20; /bin/ls >/dev/null 2>&1')

    then I still get the Lost connection error. What causes this, and are
    there standard patterns for fixing it?

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Sep 14, 2004
    #1
    1. Advertising

  2. "Eric Schwartz" <> schrieb im Newsbeitrag
    news:...

    > then I still get the Lost connection error. What causes this, and are
    > there standard patterns for fixing it?


    Did you try to increase the connection timeout on SQL Server (or set it to
    infinite)?

    robert
    Robert Klemme, Sep 15, 2004
    #2
    1. Advertising

  3. Another note...

    "Eric Schwartz" <> schrieb im Newsbeitrag
    news:...
    > The following script reliably gives me a DBI::DatabaseError
    >
    > $ cat /tmp/test.rb
    > #!/usr/bin/ruby
    > require 'dbi'
    >
    > dbh = DBI.connect("DBI:mysql:host=test.test;db=test", "test", "test")
    > fork {
    > fork {
    > $stderr.close
    > $stdout.close
    > $stdin.close
    > exec("/bin/ls >/dev/null 2>&1")
    > }
    > }
    > sth = dbh.prepare("select * from machines")
    > sth.execute


    Another reason might be the fork after the DBI.connect() - the child might
    close the connection on exit. I'd move the DBI.connect to the line
    directly prepending "sth = ...".

    >
    > $ /tmp/test.rb
    > /usr/lib/ruby/1.8/DBD/Mysql/Mysql.rb:403:in `execute': Lost connection

    to MySQL server during query (DBI::DatabaseError)
    > from /usr/lib/ruby/1.8/dbi/dbi.rb:769:in `execute'
    > from /tmp/test.rb:11
    > $
    >
    > The context this problem came from is: I have a CGI program that must
    > fire off a long-running process. I want to use the standard Unix
    > double-fork trick to allow the long-running process to detach itself
    > and run independently. My first thought was that if there's an error
    > in the child process, it exits first, and closes the DBI connection
    > automatically.


    Maybe it closes the connection on startup. Since your child process
    doesn't seem to need the connection I'd create it immediately before I use
    it.

    > That does NOT seem to be the case, however, as if I
    > change the exec() to
    >
    > exec('sleep 20; /bin/ls >/dev/null 2>&1')
    >
    > then I still get the Lost connection error. What causes this, and are
    > there standard patterns for fixing it?


    See above.

    Kind regards

    robert
    Robert Klemme, Sep 15, 2004
    #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. Jason
    Replies:
    1
    Views:
    3,758
    Latha Rajeev
    Jun 25, 2003
  2. Bassel Tabbara [MSFT]

    RE: The transport lost its connection to the server

    Bassel Tabbara [MSFT], Jun 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    637
    Bassel Tabbara [MSFT]
    Jun 27, 2003
  3. Joe Linke

    Lost connection during query

    Joe Linke, Feb 22, 2004, in forum: Java
    Replies:
    1
    Views:
    580
    Jayaram
    Feb 23, 2004
  4. johnny
    Replies:
    1
    Views:
    788
    Jerry Hill
    Dec 8, 2006
  5. Love4llamas
    Replies:
    0
    Views:
    798
    Love4llamas
    Oct 13, 2011
Loading...

Share This Page