# Database connection switching at runtime

**URL:** <https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800>\
**Category:** Rails\
**Created:** [October 13, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800 "2008-10-13T14:23:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 13, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800/1 "2008-10-13T14:23:29Z")

</div>

Hi All:

I want my application to authenticate user through master database and  
accordingly should connect them to their specific database.

I am using the sub-domain as identifier for the user and stores  
domain-database information in master database.

Im using a function responsible for mapping domains to database in  
application.rb using before\_filter :function\_call

Default database will be master and function will identify the user  
specify db to establish a connection at runtime.

The problem is …the next time i tries to logout and use different  
domain… it still uses previous database and tries to find  
domain-database related information there, which actually is in masters.

Plz suggest me some way to it.

Thanks in Advance  
Priya S.

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 13, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800/2 "2008-10-13T17:38:03Z")

</div>

I assume that your initial case is working: New web session hits the  
web server, application sees “new” and uses master database. User logs  
in (verified against master) and is hooked up to the correct database.

Not knowing any of the details of your login/logout or session  
management, it sounds like after logging out, you aren’t clearing enough  
data out of the session so that the same browser session would be  
recognized as “new” (i.e., the same as a totally new browser session)  
when you try to log back in.

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 14, 2008, 7:11am UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800/3 "2008-10-14T07:11:33Z")

</div>

Hi Chron:

Thanks for the reply.  
Yes the initial case is working but there is something to be decided  
there as well.  
In application.rb  
i use before\_filter :switch\_db  
def switch\_db  
@db = Client.find\_by\_domain\_name(request.env[‘HTTP\_HOST’])  
db\_name = @db.database\_name  
ActiveRecord::Base.establish\_connection(  
:adapter =\> “mysql”,  
:host =\> “”,  
:username =\> “”,  
:password =\> “”,  
:database =\> db\_name,  
:port =\> “3333”  
)  
end  
Initially it selects the db well but since application.rb is called  
always the function gets executed each time and tries to find client in  
the current db.  
What condition shall be placed to by pass this function if there already  
exists a database connection [other than with master].

Regards,  
Priya S.

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 14, 2008, 7:47am UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800/4 "2008-10-14T07:47:12Z")

</div>

I think on logout i have to end the current db connection.  
How can i do this??

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 14, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800/5 "2008-10-14T18:45:47Z")

</div>

Not to backtrack too far, but why a separate physical database per user?

If they are all running the same application, could not a single  
database be used for multiple users? You’d just need to scope the  
queries by “owner”, and persist the owner on record creation.

To answer your question, “disconnect!”

---

<div class="post-metadata">

**Author:** ![priyasaini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@priyasaini](https://www.ruby-forum.com/u/priyasaini)\
**Post date:** [October 15, 2008, 6:16am UTC](https://www.ruby-forum.com/t/database-connection-switching-at-runtime/150800/6 "2008-10-15T06:16:07Z")

</div>

Ar Chron wrote:

> Not to backtrack too far, but why a separate physical database per user?
> 
> If they are all running the same application, could not a single  
> database be used for multiple users? You’d just need to scope the  
> queries by “owner”, and persist the owner on record creation.
> 
> To answer your question, “disconnect!”

Hi Chron:  
I am planning to keep seperate db for all my future clients coz keep a  
single db wont be feasible since they may have their individual  
requirements and configuration. Also we have Ferret Search in our  
application… so keep dbs seperate will be a better option.  
Anyways i did it. I maintained a session variable to avoid function call  
each time.  
N thanks for “disconnect!” 🙂  
What i am lookin for now is how to maintain two db connections  
simultaneously and how to hop among them. I dont want to set up  
connection each time i need it.

Thanks Again  
Priya S.
