I开发者_如何学JAVAf I call ActiveRecord::Base.establish_connection to switch databases for the duration of a Rails request, how global is the effect of this change?
- Does it affect other instances of the Rails app running under Passenger?
- Does it affect the next request by the same instance of Rails?
- Are there any thread race conditions to worry about?
No(*), Yes, No.
I'm not very familiar with Passenger, but I'm assuming it works like other containers that use a process per Rails instance. In that case, each will have its own connection.
The connection is maintained across requests, so if you switch the connection for an ActiveRecord class, it will be used in the next request.
Finally, database connections are shared across threads. You can verify this with:
Thread.new { puts ActiveRecord::Base.connection.object_id; sleep 30; puts ActiveRecord::Base.object_id}
sleep 10
ActiveRecord::Base.establish_connection
and seeing that the object ID output before and after the call to establish connection is different.
So, you may have thread issues if you are expecting all accesses from within a thread to access the same database connection but then you switch it halfway through to a different connection from within another thread..
精彩评论