From: Ken Bloom Date: 2009-05-21T01:10:04+09:00 Subject: Re: mysql vs. fork On Wed, 20 May 2009 22:14:53 +0900, Daniel DeLorme wrote: > It's commonly known that database connections do not play well with > Process.fork; I thought that using at_exit{exit!} was the correct way to > solve this problem, but it appears imperfect. Take this pseudo-code: > > 1 db1 = mysql_connect() > 2 Process.fork do > 3 at_exit{exit!} > 4 db2 = mysql_connect() > 5 db2.query(...) > 6 end > 7 Process.wait > 8 db1.query(...) > > With line 3, only the at_exit handlers defined in the child process are > run when the child exits, ensuring that db1 stays alive and line 8 keeps > working. > > At least that's what I thought. But in the past 2 days I've been getting > some "MySQL server has gone away" errors from our servers, indicating > that at_exit{exit!} is not enough to protect the db1 connection. It's > not consistently reproducible; I can't get the error when directly > testing but the flow of errormails is unmistakable. So what exactly was > wrong in my understanding of at_exit, sockets, mysql, and fork? > > -Daniel One solution is to encapsulate the database connection in a DRb server. I have written about this in ruby-talk:296032 (see http:// blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/296032) -- Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/