title: Hibernate JDBC connection blocking
body: <p>I'm using spring + hibernate + tomcat jdbc in my java application.  I want to be able to handle connection problem when , for exemple, a database crash occur.  The problem I got is that hibernate block on trying to get a jdbc connection (when for exemple the mysql is down) and never timeout making the http request hanging indefinitly.</p>
  
  <p>Here's my hibernate config : </p>
  
  <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
    &lt;property name="dataSource"&gt;
      &lt;ref bean="dataSource"/&gt;
    &lt;/property&gt;
    &lt;property name="hibernateProperties"&gt;
         &lt;props&gt;
           &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
           &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
           &lt;prop key="hibernate.connection.release_mode"&gt;auto&lt;/prop&gt;
       &lt;/props&gt;
    &lt;/property&gt;
    &lt;property name="mappingResources"&gt;
      &lt;list&gt;
        &lt;value&gt;detection/model/conf/rules.hbm.xml&lt;/value&gt;
  &lt;/list&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>And my jdbc datasource config :</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"&gt;
  
      &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;
      &lt;property name="url" value="${jdbc.url}"/&gt;
      &lt;property name="username" value="${jdbc.username}"/&gt;
      &lt;property name="password" value="${jdbc.password}"/&gt;
      &lt;property name="initialSize" value="10" /&gt;
      &lt;property name="maxActive" value="100" /&gt;
      &lt;property name="maxIdle" value="50" /&gt;
      &lt;property name="minIdle" value="10" /&gt;
      &lt;property name="testOnBorrow" value="true"/&gt;
      &lt;property name="testOnReturn" value="true"/&gt;
      &lt;property name="testWhileIdle" value="true"/&gt;
      &lt;property name="maxWait" value="2000"/&gt;
      &lt;property name="validationQuery" value="/*ping*/ SELECT 1"/&gt;
      &lt;property name="timeBetweenEvictionRunsMillis" value="2000"/&gt;
      &lt;property name="removeAbandonedTimeout" value="2000"/&gt;
      &lt;property name="removeAbandoned" value="true"/&gt;
      &lt;property name="validationInterval" value="5000"/&gt;
  
   &lt;/bean&gt;
  </code></pre>
  
  <p>Is there any way to tell hibernate to timeout waiting for a connection after a certain time ?</p>
  
  <p><strong>UPDATE :</strong> </p>
  
  <p>I switch to c3p0 but i got the same behavior, but i got more debug info with c3p0.  I can see that an exception is thrown but c3p0 seem to catch it and do nothing with it so the http request still hang till i restart the mysql server.</p>
  
  <pre><code>DEBUG 2012-12-07 09:07:46,994 : Opening Hibernate Session
  DEBUG 2012-12-07 09:07:46,994 : opened session at timestamp: 13548892669
  DEBUG 2012-12-07 09:07:46,994 : about to open PreparedStatement (open         PreparedStatements: 0, globally: 2)
  DEBUG 2012-12-07 09:07:46,994 : opening JDBC connection
  DEBUG 2012-12-07 09:07:46,994 : trace com.mchange.v2.resourcepool.BasicResourcePool@195b6aad [managed: 10, unused: 7, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@6b0ede6)
  DEBUG 2012-12-07 09:07:46,994 : select rules0_.product as product2_, rules0_.rules as   rules2_, rules0_.type as type2_ from rules rules0_ where rules0_.product=? and rules0_.type=?
  DEBUG 2012-12-07 09:07:46,995 : com.mchange.v2.c3p0.impl.NewPooledConnection@60487c5f handling a throwable.
  com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  </code></pre>