1

I am developping a Spring MVC web application that use the dbcp database connection pool.

<bean id="datasourceAR_XXX" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@XXX.XXX.com:1500:SERVICE</value></property>
        <property name="maxActive"><value>100</value></property>
        <property name="maxIdle"><value>10</value></property>
        <property name="username"><value>XXX</value></property>        
        <property name="password"><value>XXX</value></property>
</bean>

I recently moved the scope of those beans to singleton because the amount of connection per session started to be a bit too much.

The problem is :

Our database is shutting down every sunday and the spring application seems to act strangely by keeping the socket open and does not refresh the connection as I thought it would do.

Is there a way to refresh the beans scoped as singleton in a way that will refresh the connection everyday and not be obliged to relaunch the application every monday?

4
  • Use a validation query to validate the connection. If validation fails the connection will be dropped and a new one created. That way you don't need to restart anything...
    – M. Deinum
    Jun 8, 2015 at 10:26
  • In fact, we have issues with that because the connector seems to be waiting without any error given at any moment... it's just blocking. What exactly are you talking about when you say Validation query? It's just a simple query on the database or something Spring is providing? Jun 8, 2015 at 13:09
  • Your datasource is providing that.Have you read the documentation?. I would however also suggest a different connection pool something like HikariCP
    – M. Deinum
    Jun 8, 2015 at 13:33
  • Thanks Deinum! I missed the validation query parameter which have a timeout parameter as well so it's perfect! Don't know how to pin your answer though :) Jun 8, 2015 at 15:01

1 Answer 1

1

What you want to do is to configure validation for your connections. When a connection is borrowed from the pool you want to make sure that that connection is valid. For this you can specify the validationQuery property on your datasource.

<bean id="datasourceAR_XXX" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
    <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
    <property name="url"><value>jdbc:oracle:thin:@XXX.XXX.com:1500:SERVICE</value></property>
    <property name="maxActive"><value>100</value></property>
    <property name="maxIdle"><value>10</value></property>
    <property name="username"><value>XXX</value></property>        
    <property name="password"><value>XXX</value></property>
    <property name="validationQuery" value="select 1 from dual" />
</bean>

See DBCP - validationQuery for different Databases for a list of possible validation queries for different databases.

There are some issues with Commons DBCP and it is pretty old (although there is a DBCP 2.x now). I would suggest moving to a different datasource like HikariCP this datasource is also a JDBC 4.x based datasource which allows for easier connection validation (it is part of the JDBC 4 spec).

<bean id="datasourceAR_XXX" class="com.zaxxer.hikari.HikariDataSource">
    <property name="datasourceClassName" value="oracle.jdbc.pool.OracleDataSource"/>
    <property name="maximumPoolSize" value="20" />
    <property name="username" value="XXX" />
    <property name="password" value="XXX" />
    <property name="datasourceProperties">
        <props>
            <prop key="serverName">XXX.XXX.com</prop>
            <prop key="port">1500</prop>
            <prop key="databaseName">SERVICE</prop>             
        </props>
    </property>
</bean>

If your oracle driver is new enough you don't need a validation query anymore as validation is provided by the driver instead of needing to be done with a query. Next to that you probably have better results with this pool.

Also you might have a bit of a large pool size, nice article/presentation about pool sizing can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.