[Backgroundrb-devel] MySQL too many connections?

Ezra Zygmuntowicz ezmobius at gmail.com
Mon Jul 31 10:50:59 EDT 2006

On Jul 30, 2006, at 8:49 PM, Ben Johnson wrote:

> Does any have any idea why I would get:
> Mysql::Error: Too many connections
> I am creating new threads in my background process. I know  
> allow_concurrency is set to true. Does this create a new database  
> connection when a new thread is created? Because what I'm doing is  
> similar to this:
> @threads[model.id] = Thread.new do
> end
> I also kill off these threads and over write some of the @threads  
> keys. Does it clean up the connections for me or do I need to do  
> this manually?
> Lastly, could this be because 2 threads are trying to access the  
> database at the same time?
> Thanks for your help.
> Thank You,
> Ben Johnson
> E: bjohnson at contuitive.com

Hey Ben-

	I'm not sure if it is a good idea to use threads within a worker  
class. Since the workers are each run in their own threads and you  
are spawning more threads within those threads, I am not sure how  
that will behave. It's not clear from the ActiveRecord docs what  
allow_concurrency really does. It is quite possible that you may be  
opening new db connections for each thread but I don't think that is  
the case. Can you monitor mysql to see how many connection handles  
are open when you are running your workers? Also if you are using  
threads in your worker you may need to join these threads to make  
sure they clean up after themselves. So in your worker class after  
the work is done and before you delete the worker it would probably  
be a good idea to do @threads[model.id].join

	BgDRb already makes heavy use of threads and mutexes so I don't  
think its a good idea to create more threads in your worker classes.  
You would be better off instantiating a new worker for each task  
instead of multiple threads within one worker. Of course if you can  
make it work then more power to you. I enjoy seeing this plugin  
pushed as far as it can go ;)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/backgroundrb-devel/attachments/20060731/238e1891/attachment.html 

More information about the Backgroundrb-devel mailing list