# Lighty+Mongrel: Limiting persistent db connections?

**URL:** <https://www.ruby-forum.com/t/lighty-mongrel-limiting-persistent-db-connections/55078>\
**Category:** Rails\
**Created:** [March 16, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/lighty-mongrel-limiting-persistent-db-connections/55078 "2006-03-16T21:28:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 16, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/lighty-mongrel-limiting-persistent-db-connections/55078/1 "2006-03-16T21:28:15Z")

</div>

I’m not sure if it’s due to setting up multiple mongrel servers and  
proxying them with lighty, but something is keeping postgres connections  
around. I only have a couple Rails apps running on a new server, with  
not a whole lot of traffic, yet today I actually maxed out my connection  
limit of 100 for postgres. How do I turn off persistent connections in  
Mongrel?

Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 17, 2006, 6:48am UTC](https://www.ruby-forum.com/t/lighty-mongrel-limiting-persistent-db-connections/55078/2 "2006-03-17T06:48:58Z")

</div>

Joe,

Each mongrel you run takes a -n setting (-P on win32 though) that sets  
the  
number of processor threads. The way ActiveRecord works is that you get  
one  
DB connection for each thread using AR. Since the default is 20  
processor  
threads then you’ll get 20 DB connections per mongrel setup.

The next release will have an option to turn this off for AR so that  
there’s  
one DB connection for each Mongrel.

Still, it would better if AR just had a real LRU connection pool. 🙂

Zed A. Shaw

> **[Zed A. shaw](https://zedshaw.com/)**
>
> Personal essays and writings by Zed A. Shaw.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 17, 2006, 11:55am UTC](https://www.ruby-forum.com/t/lighty-mongrel-limiting-persistent-db-connections/55078/3 "2006-03-17T11:55:34Z")

</div>

Ah, thanks. I decreased -n and that seems to have improved the  
situation.

Is there any advantage in setting -n to greater than 1 when used with  
Rails?

Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [March 17, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/lighty-mongrel-limiting-persistent-db-connections/55078/4 "2006-03-17T15:00:29Z")

</div>

Yes, even though Rails runs in a synchronized block, the entire rest of  
Mongrel tries to be thread safe. So, if you want to be able to have  
mongrel  
deal with more than one connection at a time then keep it above 1.

The catch is that if you increase it too much then the threads fight  
over  
the socket queue and things start to get slower. 20 was about the right  
setting, but YMMV.

Zed
