Database connection switching at runtime

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.

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.

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.

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

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!”

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!” :slight_smile:
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.