Reputation: 387
I have an application where the DB connection are created by the Spring configuration (then passed on to hibernate). Following is the default parameters in our spring file.
<bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="url">
<value>${dbUrl}</value>
</property>
<property name="username">
<value>${dbUser}</value>
</property>
<property name="password">
<value>${dbPassword}</value>
</property>
</bean>
However, in a particular environment the Oracle DB connections are getting closed by a network setting after certain period of time. We initially thought that the closing of the connections is because of the connections being idle. We added validationQuery to periodically post a query on the server so that the connections are not idle.
<bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="url">
<value>$dbUrl</value>
</property>
<property name="username">
<value>$dbUser</value>
</property>
<property name="password">
<value>$dbPassword</value>
</property>
<property name="validationQuery">
<value>SELECT 1 FROM dual</value>
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<property name="testOnReturn">
<value>true</value>
</property>
<property name="timeBetweenEvictionRunsMillis">
<value>1000</value>
</property>
</bean>
My question is does adding validationQuery guarantees, the connections not being idle? The application fails on first login attempt throwing no DB connection; but works after 2-3 attempts. Not sure what is happening here.
Upvotes: 0
Views: 917
Reputation: 2451
On the side note, C3P0 Connection Pool libraries is much better than DBCP. I had always issues in past with DBCP, ever since switched to C3P0 no issues so far...
http://www.mchange.com/projects/c3p0/
Upvotes: 0
Reputation: 2104
The validationQuery is only used to test a connection when it's retrieved from the connection pool. See: https://commons.apache.org/proper/commons-dbcp/configuration.html
Also, I've seen some odd behavior with org.apache.commons.dbcp.BasicDataSource and Oracle (11 and 12). We've had much better stability with the org.apache.tomcat.dbcp.dbcp.BasicDataSource instead. The config options are pretty close (maybe identical?) to the commons dbcp implementation, so conversion is a snap.
Upvotes: 1