Lost connection to MySQL server during query

E

Eric Schwartz

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
 
R

Robert Klemme

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
 
R

Robert Klemme

Another note...

Eric Schwartz said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top