# Threading with Activerecord

**URL:** <https://www.ruby-forum.com/t/threading-with-activerecord/190911>\
**Category:** Ruby\
**Created:** [June 30, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/threading-with-activerecord/190911 "2010-06-30T15:57:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d26b3c/32.png) [@joe1](https://www.ruby-forum.com/u/joe1)\
**Post date:** [June 30, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/threading-with-activerecord/190911/1 "2010-06-30T15:57:39Z")

</div>

Hello all,  
First, let me give a little background. I have two applications, which  
both connect to two different databases (but on the same server.) These  
applications run on about 40+ computers. The default pool size (5) is  
too large since it can possibly take up 800+ connections to my single  
MySQL server (Remember there are two (2) databases). However, when I  
decrease the pool size to try and keep the connections more compact, I  
run into the infamous “ConnectionTimeoutError”. Essentially telling me  
that the pool size is too small. After some debugging, I’ve discovered  
that some threads that should be done with the database are not  
releasing their connection back into the pool. Now, I’ve tried a few  
different Monkey Patches that I found on the internet to try and combat  
this problem, but none have worked thus far. So I guess my question is,  
how I can I ensure that a connection gets released back into a pool  
after my Model is done being used.

I am aware of the with\_connection() method, but that appears to be used  
outside of Models. I did find this:  
[http://coderrr.wordpress.com/2009/05/05/activerecords-with\_connection-is-now-useful/](http://coderrr.wordpress.com/2009/05/05/activerecords-with_connection-is-now-useful/)  
but that looks like I’d have to hand edit a lot of my Models, and since  
I have ~50 models with lots of methods inside them, I’m not sure this is  
the easiest/best approach.

I also have another monkey patch in place that attempts to cleanup the  
connection with every call. I am having difficulties finding the URL to  
it right now, but that doesn’t work either.

If you need code posted, I can post some code, but since this is an in  
house thing, I am limited on what I can show.
