# Questions about connection pools

**URL:** <https://www.ruby-forum.com/t/questions-about-connection-pools/162911>\
**Category:** Rails\
**Created:** [March 15, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/questions-about-connection-pools/162911 "2009-03-15T17:56:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [March 15, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/questions-about-connection-pools/162911/1 "2009-03-15T17:56:45Z")

</div>

Hi all,

I currently am part of a team writing an app and I’m running into an  
issue with connection pools.

Quick infrastructure overview: Rails 2.2.2, Ruby 1.8.7p72, Apache/  
Mongrel, mongrel running single threaded. Due to legacy schema, the  
mongrels are making connections to a couple of different DB.

We currently are running about 28 mongrels, and obviously with a  
default 5 connections/per mongrel, we’re getting 140 connections to  
the db. This seems kind of wasteful, especially as we scale larger.

So the first thing I tried is setting the pool size to 1. I figured  
since only one request is being processed at a time, there’s only a  
need for one db connection. However, this cause the app to start  
spewing ‘could not obtain db connection’ errors.

So I bumped the pool size up to 2, and things seem to be OK, however  
when I go into mysql and show processlist, it looks like there are 4  
or 5 connections open.

Basically, I realize that there the connection pool is necessary to  
support multithreading, but is there any way to pare things down to  
one connection per mongrel like the old day, when we aren’t using the  
multithreading?

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [March 15, 2009, 6:28pm UTC](https://www.ruby-forum.com/t/questions-about-connection-pools/162911/2 "2009-03-15T18:28:11Z")

</div>

Andrew S. wrote:

> We currently are running about 28 mongrels, and obviously with a  
> default 5 connections/per mongrel, we’re getting 140 connections to  
> the db. This seems kind of wasteful, especially as we scale larger.

Searching your post I don’t see the word ‘cache’. Explain?

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [March 16, 2009, 2:28am UTC](https://www.ruby-forum.com/t/questions-about-connection-pools/162911/3 "2009-03-16T02:28:07Z")

</div>

On Mar 15, 4:56 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> default 5 connections/per mongrel, we’re getting 140 connections to
> 
> Basically, I realize that there the connection pool is necessary to  
> support multithreading, but is there any way to pare things down to  
> one connection per mongrel like the old day, when we aren’t using the  
> multithreading?

You can set the connection pool size to 1, but if you do that you need  
to be careful not to leak connections. During the normal request/  
response cycle this is all handled for you but at app load times you  
will have to be careful. Stuff like

class MyControllerClass \< ActionController::Base  
SOME\_CONSTANT = People.find :all  
end

(contrived example but you get the point) will leak a connection  
because a connection will be used to execute the query but it won’t be  
returned to the pool. You can clear out connections with  
ActiveRecord::Base.clear\_active\_connections!

Fred
