Description of the problem:
A project involves two data sources, each using two different database connection pools, one of which is the Poxool connection pool, and the problem is that spring starts with only initializing the database connection in one of the database connection pools, and the Poxool configured database connection pool, at startup No initialization, a database connection is not initialized, so the first time you need to access the database, the temporary need to initialize the database connection, resulting in the first click of the page button, if it is remote access, up to 15 seconds or so.
At first thought can be used in DataSource this bean to use the property Lazy-init = "false" to fix, in fact, the default is Lazy-init= "false", in fact, from the spring start log, poxool corresponding DataSource has been instantiated, so it is not the DataSource delay initialization problem, but the datasoruce instantiation, but the database connection is not initialized, so this is the root cause of the problem.
Find the reason, then it is easy to solve, we know that the bean has a property: Init-method = "SomeMethod", indicates that the bean's SomeMethod method is called at the later stage of instantiating the bean.
So, we look at the source of the Org.logicalcobwebs.proxool.ProxoolDataSource, find his initial database connection method, the assignment of the Init-method property can be done. The source code is as follows:
/** * @see javax.sql.datasource#getconnection () */public Connection getconnection () throws SQLException { ConnectionPool CP = NULL; try { if (! Connectionpoolmanager.getinstance (). Ispoolexists (alias)) { registerpool (); } CP = Connectionpoolmanager.getinstance (). Getconnectionpool (alias); return cp.getconnection (); } catch (Proxoolexception e) { log.error ("Problem getting Connection", e); throw new SQLException (e.tostring ()); } }
Obviously, getconnection will trigger the Org.logicalcobwebs.proxool.ProxoolDataSource to establish a database connection.
So the bean:
<bean id= "DataSource" class= "Org.logicalcobwebs.proxool.ProxoolDataSource" >
Switch
<bean id= "DataSource" class= "Org.logicalcobwebs.proxool.ProxoolDataSource" init-method= "getconnection" >
OK, it will force Org.logicalcobwebs.proxool.ProxoolDataSource to call the Getconnection method immediately when instantiating, initialize the database connection immediately.
Avoid temporarily initializing the database connection pool until the first time you access the database, resulting in super-slow first-time access.
This configuration can lead to another small problem, when it starts, it slows down a bit because it requires a database connection in two database connection pools to start.
On the principle of init-method, you can refer to my other blog: http://www.cnblogs.com/digdeep/p/4518571.html
Take advantage of the Spring Bean's property init-method to resolve a problem where the first click of the page is slow because the database connection is not initialized