# How does Rails / Apache handle database connections?

**URL:** <https://www.ruby-forum.com/t/how-does-rails-apache-handle-database-connections/186414>\
**Category:** Rails\
**Created:** [April 6, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/how-does-rails-apache-handle-database-connections/186414 "2010-04-06T19:48:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 6, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/how-does-rails-apache-handle-database-connections/186414/1 "2010-04-06T19:48:21Z")

</div>

Hi all,

I’m trying to figure out how Rails handles database connections. The  
business problem is:

- My users have access to multiple, structurally identical schemas
- So, for each request, Rails needs to be able to talk to a different  
schema
- It’s essential that users not pull data from the wrong schema

A couple more rules:

- During the request, the schema used will not change
- All the schemas can be accessed from one database server using one  
set of login credentials

The solution I’m contemplating is to execute a “use” statement for  
each request, so that all subsequent queries use the correct table.

My questions are:

- How many database connections does each Rails process create?
- Does this vary by adapter? (I’m using MS SQL Server)
- Does apache pool database connections in any way?
- Are database connections independent of Rails processes on apache?

Thanks!  
Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 6, 2010, 7:54pm UTC](https://www.ruby-forum.com/t/how-does-rails-apache-handle-database-connections/186414/2 "2010-04-06T19:54:12Z")

</div>

On Apr 6, 5:19 pm, Daniel H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - During the request, the schema used will not change
> - Are database connections independent of Rails processes on apache?

Each rails process has a pool of connections (by default up to 5). I  
believe you can add a hook that runs after a connection is checked  
out (eg to set the current database). This layer of abstraction is  
independent of the various adapters. If you are using a multithreaded  
environment (of which the only one of note are the jruby ones, which  
your questions about apache imply you are not using) then you might  
have to be extra specially careful

Apache doesn’t come into this at all.

Fred

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 7, 2010, 1:22pm UTC](https://www.ruby-forum.com/t/how-does-rails-apache-handle-database-connections/186414/3 "2010-04-07T13:22:37Z")

</div>

This helped - thank you!

On Apr 6, 1:52 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
