# Making Multiple Database Connections

**URL:** <https://www.ruby-forum.com/t/making-multiple-database-connections/111860>\
**Category:** Rails\
**Created:** [August 12, 2007, 2:29pm UTC](https://www.ruby-forum.com/t/making-multiple-database-connections/111860 "2007-08-12T14:29:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [August 12, 2007, 2:29pm UTC](https://www.ruby-forum.com/t/making-multiple-database-connections/111860/1 "2007-08-12T14:29:39Z")

</div>

Hi,

I have a multi-tenant web application. Each tenant’s data is  
separated into their own database. In rails, I have some code to  
manually change the database connection using  
ActiveRecord::Base.establish\_connection to create a connection to the  
appropriate database.

My question is this: in a single database environment (as I am aware  
Rails is designed for), Rails seems to maintain the connection to the  
database beyond the lifetime of a request. Looking at mysql, the  
connection is placed in sleep mode and obviously reused for the next  
action. However, if I am manually establishing connections, this  
reuse of connection will obviously not function. Are there any  
downsides to using ActiveRecord::Base.establish\_connection to  
establish a connection on each request to potentially a different  
database? Will these connections be gracefully closed once the  
request is over? And finally, is there any way to use connection  
pooling to keep a list of connections to each database and reuse them,  
instead of creating a new one on each request?

Thanks in advance! 🙂

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [August 24, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/making-multiple-database-connections/111860/2 "2007-08-24T23:17:37Z")

</div>

Hello,

If you would find a solution for a connection pool, i would be very  
interested in your approach.  
As i am currently have struggeling with thesame problem.

Every request with ActiveRecord::Base.establish\_connection on every  
request has the following performance hit (about 0.01s, too big in my  
opinion)

With ActiveRecord::Base.establish\_connection on every request  
Processing SiteController#show\_page (for 127.0.0.1 at 2007-08-24  
22:46:25) [GET]  
Parameters: {“action”=\>“show\_page”, “url”=\>[“products”, “m-series”],  
“controller”=\>“site”}  
Completed in 0.01111 (90 reqs/sec) | DB: 0.00019 (1%) | 200 OK [http://  
localdev.be/products/m-series/]

Without ActiveRecord::Base.establish\_connection on every request  
Processing SiteController#show\_page (for 127.0.0.1 at 2007-08-24  
22:42:07) [GET]  
Parameters: {“action”=\>“show\_page”, “url”=\>[“products”, “m-series”],  
“controller”=\>“site”}  
Completed in 0.00169 (590 reqs/sec) | DB: 0.00035 (20%) | 200 OK  
[[http://localdev.be/products/m-series/](http://localdev.be/products/m-series/)]

Thanks,  
Tiele

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [August 25, 2007, 5:25am UTC](https://www.ruby-forum.com/t/making-multiple-database-connections/111860/3 "2007-08-25T05:25:24Z")

</div>

Most rails apps that are multi-tenant use a single database. This is  
covered  
a lot and DHH has commented on how basecamp does it. Might want to  
search  
the list archives for some proposed solutions. They all involve using  
associations to protect data from being exposed to the wrong user.

Of course, that doesn’t answer your question, but I thought I’d bring it  
up.
