# Can db connections be shared

**URL:** <https://www.ruby-forum.com/t/can-db-connections-be-shared/116658>\
**Category:** Rails\
**Created:** [October 8, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658 "2007-10-08T15:47:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/1 "2007-10-08T15:47:20Z")

</div>

If I have an application that makes numerous calls to a database for  
CRUD stuff over a period, does another user on a different PC when  
accessing the same database and same user make use of that existing  
connection. Or is another new connection opened, for the second user.

When is the connection closed? When is it re-used? Is it any different  
if using multiple Mongrels and Proxy Load Balancer with Apache?

Thanks

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/2 "2007-10-08T16:53:34Z")

</div>

No, none of them are shared. Each one is a separate connection. It would  
be the same with multiple instances of mongrel.

-Bill

Andrew C. wrote:

> Andrew

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/3 "2007-10-08T17:19:59Z")

</div>

On 8 Oct 2007, at 14:47, Andrew C. wrote:

> If I have an application that makes numerous calls to a database for  
> CRUD stuff over a period, does another user on a different PC when  
> accessing the same database and same user make use of that existing  
> connection. Or is another new connection opened, for the second user.
> 
> When is the connection closed? When is it re-used? Is it any different  
> if using multiple Mongrels and Proxy Load Balancer with Apache?

Unless you start establishing connections by hand, each instance of  
mongrel has one database connection, which it will reuse until it  
dies. If you only had one mongrel then there would only ever be 1  
connection.

Fred

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/4 "2007-10-08T17:36:23Z")

</div>

On 8 Oct 2007, at 16:30, William P. wrote:

> You will get multiple connections regardless of whether you create  
> them by hand, even if it’s using a named pipe. I fired up 2  
> connections by hand to illustrate the point:

What do you mean by “fire up 2 connections”. I always see one  
connection per mongrel (+ instances of query browser etc… obviously)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/5 "2007-10-08T17:31:42Z")

</div>

You will get multiple connections regardless of whether you create them  
by hand, even if it’s using a named pipe. I fired up 2 connections by  
hand to illustrate the point:

netstat -an | grep mysql.sock  
unix 2 [ACC] STREAM LISTENING 7235  
/var/lib/mysql/mysql.sock  
unix 3 [] STREAM CONNECTED 2373005  
/var/lib/mysql/mysql.sock  
unix 3 [] STREAM CONNECTED 2372935  
/var/lib/mysql/mysql.sock

-Bill

Frederick C. wrote:

> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/6 "2007-10-08T17:38:09Z")

</div>

You said “by hand”, which I assumed to mean running mysql from the  
command line. How are you creating a connection by hand with Mongrel?

Frederick C. wrote:

> > Frederick C. wrote:
> > 
> > > >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/7 "2007-10-08T17:41:05Z")

</div>

I read your post again. I misinterpreted it to read that if you create  
connections by hand, those connections would be shared. Just a  
misinterpretation.

-Bill

William P. wrote:

> /var/lib/mysql/mysql.sock
> 
> > > connection. Or is another new connection opened, for the second user.
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 8, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/can-db-connections-be-shared/116658/8 "2007-10-08T17:41:40Z")

</div>

On 8 Oct 2007, at 16:37, William P. wrote:

> You said “by hand”, which I assumed to mean running mysql from the  
> command line. How are you creating a connection by hand with Mongrel?

Well obviously that creates extra connections. By ‘by hand’ I mean  
calling ActiveRecord::Base.establish\_connection. My point was that  
unless you go out of your way, your rails app will use one connection  
per instance of mongrel.

Fred
