# Per Session Database Connection

**URL:** <https://www.ruby-forum.com/t/per-session-database-connection/98449>\
**Category:** Rails\
**Created:** [July 14, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/per-session-database-connection/98449 "2006-07-14T20:57:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 14, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/per-session-database-connection/98449/1 "2006-07-14T20:57:02Z")

</div>

Hi,

I am creating a Rails front-end for a legacy application. For this  
application the username and password for the database must be provided  
by the user. i.e. each session needs a separate connection to the  
database using the credentials of the user logged in to that session.

Does anyone have any idea where to even start? It seems like Rails won’t  
boot without a database configured (it fails in initializer.rb if you  
don’t specify a DB). The problem I have is that until the user actually  
enters their name and password I have no way to log in to the DB.

Thanks,

Chris.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 14, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/per-session-database-connection/98449/2 "2006-07-14T21:26:26Z")

</div>

On 7/14/06, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> enters their name and password I have no way to log in to the DB.

You might want to post what database you are using, some solutions are  
database specific. For example with postgres you could use SET  
SESSION AUTHORIZATION and just use one connection.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [July 14, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/per-session-database-connection/98449/3 "2006-07-14T22:47:06Z")

</div>

snacktime wrote:

> You might want to post what database you are using, some solutions are  
> database specific. For example with postgres you could use SET  
> SESSION AUTHORIZATION and just use one connection.

At the moment I am using Firebird, but ideally I would like a DB  
independent solution.

At the moment I am experimenting with the per-model connection technique  
from here:

[http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases](http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases)

but I am bit nervous about the impact of connection pooling and caching.  
Since I am doing this for security reasons, having a cached connection  
be used by another session would be catastrophic.

Thanks,

Chris.
