Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a timeout on spring DriverManagerDataSource

We are using DriverManagerDataSource from the Spring framework (version 2.5) to pool connections to Oracle. However, it seems that these connections don't have any timeout defined - yesterday, after emergency database restart, we had a thread hanging on a socket read inside the database connection. How can I set the timeout, to say 10 mins, so that it raises an exception next time?

like image 243
Grzenio Avatar asked Sep 13 '10 13:09

Grzenio


2 Answers

I ended up changing the bean in the Spring context in the following way:

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="no">
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="connectionProperties">
        <props>
            <prop key="oracle.net.READ_TIMEOUT">60000</prop>
        </props>
    </property>
</bean>

I don't know if it works yet.

like image 88
Grzenio Avatar answered Oct 06 '22 00:10

Grzenio


Oracle has a builtin connection pool: oracle.jdbc.pool.OracleDataSource. I recommend you use that directly. The reference for Oracle JDBC (10gR2, other versions will be similar) is in http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/toc.htm. Points of interest:

  • Configuration of data sources
  • Statement caching
  • Implicit connection caching (and specially timeout properties)
like image 24
gpeche Avatar answered Oct 06 '22 00:10

gpeche