Previously, there were several ways to connect multiple databases in a rails application, as follows:
class Cookies < activerecord::base establish_connection:typo
This way, when using operations such as Cookie.find, you will be connected to operations on the Monitor_spider configured database in DATABASE.YML. I used to use it all the time and found nothing wrong. Most recent project, because of the process of starting more, always encounter database connection pool link get time-out error.
via MySQL client with command: show Processlist; The number of database connections has been found to be high, and the connection to the 2k+ is easily loosened.
By reading the connection_pool.rb file code of the rails framework, it is found that connecting the database with Establish_connection in each model can cause great problems.
The Establish_connection method code for Class Connectionhandler in the file is as follows:
def establish_connection (name, spec) =
Corresponding to the configuration of the cookie model above, the value of name here is a cookie.
That is, the establish_connection configuration is used in 10 models, and 10 connectionpool are initialized in the @connection_pools .
If the pool size of each connectionpool is 20, then the application will run with a 10x20 database connection.
In my application there are two types of models (connected to two databases), and the total number of models is 40 +. By setting the database connection as establish_connection, the MySQL connection is easily exhausted.
How do you deal with that?
1, Configuration Database.yml
Development: adapter:mysql encoding:utf8 database:myapp username:root Password: Host:localhost test: adapter:sqlite3 database:db/test.sqlite3 5 5000 production: adapter:mysql encoding:utf8 database:myapp username:root Password: host:localhost typo: adapter:mysql database:typo username:root Password:
Description
1, typo is an external database, this is a rails installed after the existence of an instance of the application of the library;
2, the other development, test, production are the default master database, which is the default configuration;
3, Yml file writing method is very strict, typo description part of the following parameters must be tab-aligned, in NetBeans can recognize this format, if the format is not correct, will prompt an error.
Second, write an external library of the Basic model class
Create EXTERNAL_DATA.RB this model class
class Externaldata < activerecord::base = true
Description
1, Self.abstract_class = True, indicates that the model class is not related to any tables in the library, that is, an abstract class.
2, Establish_connection:typo, is to configure the connection
Third, write a specific model class
Create a file cookie.rb with the following content:
Description
1, the key is that the cookie class is inherited from the abstract class externaldata just defined, there is a benefit of this abstract class derived from the various model classes can share the connection pool, reduce the consumption of database connections .
2, this adoption of inheritance is very convenient to create other tables corresponding to the class, do not need to repeat in the model establish_connection:typo such statements. As with the model in the default library, the difference is that the parent class is different.
Go to Rails to link multiple databases