Lost connection to MySQL server during query

Another note...

"Eric Schwartz" <emschwar@pobox.com> schrieb im Newsbeitrag
news:etoy8jck980.fsf@wilson.emschwar...

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" <bob.news@gmx.net> writes:

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 = ...".

I suspect that may be the case, but in the actual application, I have
an already-open DBI handle that I don't want to lose just because I
fork(). That is, my overall flow is:

$DBH = connectDB(...)
# do lots of stuff, some of it with $DBH
fork {
  fork {
    # do child stuff here
  }
}
# do more $DBH stuff here

If there's no way around it, there's no way around it, and life sucks,
and I just re-connect after the fork, and hope the original gets
properly cleaned up somehow.

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's unfortunately a side effect of my attempt to create a clean
example.

-=Eric

···

--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.