Hi guys, i need help with this:
# enviroment.rb
config.active_record.allow_concurrency = true
# billing.rb
pid = Fork.begin(:detach => true) do # Fork.begin is a custom module, makes a fork with the properly mysql connection and detach it ...
while service.status # while status remains true... search for incoming billing (MAIN LOOP)
messages = self.find(:first, :select => "count(id) as messages_count", :conditions => "etc etc etc") if messages.messages_count.to_i > 0 # if nothing, search again for incoming billing
self.connect_billing_socket(service, operator) # attempt to connect to the billing socket
10.times do |i| Thread.new do while true break unless message = self.get_message(service, operator) rescue false # get a message to make the bill message.cobro_socket(service, operator) # send through socket the message, phone number of remitent, and others info to make the billing transaction with the operator end end end
end sleep 5 service.reload end
end
So.. basically this is the most important part of the script. I skipped many things to simplify this example. The script connects to a socket to send a billing info, and this socket can process up to 10 asynchronous request. The problem is than each thread creates a new connection to the database, with the time, the database crash for have too many connections open, this happens when all threads finish and the "MAIN LOOP" start over and find more incoming billings, then "10.times do" makes 10 threads again and each thread creates a new database connection...
How i can re-use open connections, or best, can multiples threads share a single database connection??
I really not whant to close the old connections manually..
Thanks