Q:
When launching my application, the database frequently times out during data source initialization. Its a server shared by many developers you see, and I would like to give it a few more tries before giving up. How could I accomplish this, I'm using commons-dbcp as connection pool implementation.
or
My web application seems to lose its database connection overnight when its not much used. The first new request seems to fail to database exception but the second request works ok. How could I make the app work from the get-go?
A:
Well, you can by giving up commons-dbcp and switching to c3p0 connection pool implementation. This user friendly library offers retry functionality that is a true time-saver.
The datasource configuration is a bit different, but here's a quick pointer with nice default values. Example uses Maven dependencies and Spring bean configuration.
Before
pom.xml
<dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency>
(i'll bolt on the commons-dbcp version before somebody tells me hey it exists in there too!)
datasource.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="" /> <property name="url" value="" /> <property name="username" value="" /> <property name="password" value="" /> </bean>
After
pom.xml
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
datasource.xml
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="" /> <property name="jdbcUrl" value="" /> <property name="user" value="" /> <property name="password" value="" /> <!-- How many times to retry before giving up --> <property name="acquireRetryAttempts" value="3"/> <!-- Change test query depending on the RDBMS implementation --> <property name="preferredTestQuery" value="SELECT 1"/> </bean>