A problem has recently occurred in a project:
The system uses the SPRING+C3P0 to manage the connection pool for the database, with a total of 4 data sources in the project. When you start a project, if one or more of the data sources are not connected, the background attempts to connect indefinitely, causing the entire project to fail to start.
In fact, the result we want is that if a data source cannot be connected, skip the connection to the data source and continue loading the following item. So I checked the C3P0 connection pool configuration, found that after the following configuration can achieve the desired results.
When the data source is configured, add the following three items:
<property name= "Breakafteracquirefailure" value= "true"/> <property name=
"checkouttimeout" value= "100" />
<property name= "acquireretryattempts" value= "0"/>
which
Checkouttimeout: When the connection pool is exhausted, the client invokes getconnection () and waits for the new connection to be fetched, and the SQLException is thrown after the timeout, if set to 0 and waits indefinitely. Unit milliseconds. Default:0 Breakafteracquirefailure:true indicates that the pool is marked as block and close after a failure to request a connection to the database, and that the client's request for pool is rejected even if the back-end database resumes normal and does not reconnect. False indicates that pool is not labeled as block, and new requests attempt to connection the database request. The default is False. Therefore, if you want to restore the database and network failures, pool can continue to request normal resources must set this configuration to False acquireretryattempts: Defines the number of repeated attempts after a new connection has been fetched from the database. Default:30
Finally, a complete data source configuration is as follows:
<bean id= "Iamsdb" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "close" > <property nam
E= "Driverclass" value= "${iamsdb.driverclass}"/> <property name= "Jdbcurl" value= "${iamsdb.url}"/> <property name= "user" value= "${iamsdb.user}"/> <property name= "password" value= "${iamsdb.password}"/&
Gt <property name= "maxpoolsize" value= "${iamsdb.maxpoolsize}"/> <property name= "MinPoolSize" value= B.minpoolsize} "/> <property name=" maxidletime "value=" ${iamsdb.maxidletime} "/> <property nam E= "Initialpoolsize" value= "${iamsdb.initialpoolsize}"/> <property name= "Acquireincrement" value= Cquireincrement} "/> <property name= maxstatements" value= "${iamsdb.maxstatements}"/> Ty name= "Idleconnectiontestperiod" value= "${iamsdb.idleconnectiontestperiod}"/> <property name= "AcquireRetr" Yattempts "VAlue= "${iamsdb.acquireretryattempts}"/> <property name= "Breakafteracquirefailure" value= "true"/>
<property name= "Checkouttimeout value="/> <property name= "acquireretryattempts" value= "0"/> </bean>