From: Joel VanderWerf Date: 2008-03-27T12:14:07+09:00 Subject: Re: fork Joey Marino wrote: > I see, > I have a class to handle interfacing to the database. The initialization of > the class will connect to the db and another method will close the > connection and there are query functions for handling the queries and > results. This object is then passed into new objects on their > initialization. The problem comes in when I iterate through a set of objects > creating a fork for each one. After the first object I am getting a > "database went away error". My code looks like the following: > > db = MySQL.new > > fork do > obj1 = Object1.new(db) > obj1.addToDb At this point in the child, the process is about to exit, the db object in the child is freed, and (my guess) a finalizer is executed which closes the database connection. > end > Process.wait > > fork do > obj2 = Object2.new(db) #Object1 and Object2 are inherited from the same > superclass > ob2.addToDb #db error occurs here ...which is the same as the connection you're trying to use here (same connection because of the fork), through _this_ copy of the db object. But that's only my guess. I don't know how the MySQL ruby interface works, or if there is a way to tell it not to close the connection when the child exits. Possibly informative: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/117013 If you really need the forks to access the database, they could start new connections. If there's a performance problem with that, you could keep a pool of child processes and send them commands from the parent. (The slave gem might be a good way to do that.) > end > Process.wait > > db.closeDB -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407