Rails community -
1. Does Rails make a new connection to the Database for each request?
2. I am trying to specify which database to connect to for each model.
I have added these lines of code into my config/environment.db file:
Recipe.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "dderaps",
:password => "buzz05",
:database => "derek"
)
Category.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "dderaps",
:password => "buzz05",
:database => "derek2"
)
The following happens: when I go to
http://localhost/recipe/list
or
http://localhost/category/list
the first time, everything works works fine, but, for each subsequent
request, it defaults to using the database defined in config/database.yml.
According to MySQL administrator, those initial two connections to
databases "derek" (for Recipe) and "derek2" (for Category)
remain
connected to the database server.
I have also tried putting the above code in the controller and/or model,
and that works, but that creates a new connection every time and does
not close it, which is a problem because after 25 requests I have 50
connections to the database server.
Any hints, tips, comments, or suggestions on setting up models to use
different databases?
Also any advice on optimizing Rails database access for taking huge
amounts of hits? (such as not making a new connection to the database
server every time, just using a pre-existing connection from the pool)
Thanks guys and gals,
Derek DeRaps