问题
In some of my Rails applications, my ActiveRecord models seem to establish db connections on initialization (e.g., when I do rails console), while in others connections seem to be established only when I reference the model class or instantiate a model object.
For example, I just went to one application, opened Rails console and wrote:
SomeModel.connected?
and it returned false. I went to another application, entered the same command (for a different model), and it returned true. I went to a third application and entered the same command. This time, it waited a moment and then returned true, which made me think that the connected? method triggered the connection for some reason.
This difference in behavior doesn't seem related to Rails versions or the contents of the models. It could be something weird I've done in my initializers, but I don't think so.
So when does Rails establish connections? Or what is the expected behavior?
ADDITIONAL INFO
I'll add that it doesn't seem like connected? returns false because Rails cannot connect to the database.
For example, in my first application I do:
SomeModel.connected?
# => false
SomeModel.table_exists? # or any other command that makes Rails look at db
# => true
SomeModel.connected?
# => true
回答1:
Answering my own question:
Whether a database connection is actually initialized during the Rails initialization process depends basically on whether ActiveRecord::Base.connection (not establish_connection) is called during the initialization process.
This can be related to the Rails version: for example, in Rails 3.2.13, the "active_record.validate_explain_support" initializer makes a call to connection:
!ActiveRecord::Base.connection.supports_explain?
In Rails 3.2.14, this call is not made.
However, Rails 3.2.14 may make a call to connection in the "active_record.set_reloader_hooks" initializer. This call may occur with the command
ActiveRecord::Base.clear_cache!
although the prepare callback runner doesn't always seem to call this...
I also found that some gems (e.g., ActiveAdmin) have an initialization process that will call connection at some point.
回答2:
I cannot offer you exact solutions because there could be several reasons why there are some discrepancies.
Assuming you haven't rewrote connected? method, it returns true when a Rails model can establish connection to its backend. Note that when I say backend, it can be local database in your machine, remote database in your VPN at your work, or remote database far away provided by Amazon RDS. It can differ based on your Rails environments, database config, and Rails app. So the time it takes to check its connection can differ from case to case.
It will return false when it cannot establish connection to its backend. Again, this could be due to many reasons. Your database config might be incorrect. Your remote database might be down. It might be expected by design also. For example, if you have a model using activemodel, but use json to communicate with some API instead of conventional database, it'll return false because the model table is not in the database.
I hope this helps.
来源:https://stackoverflow.com/questions/18154429/when-does-activerecord-establish-connections