2

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.

Here's my hibernate config :

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
    <ref bean="dataSource"/>
  </property>
  <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
         <prop key="hibernate.connection.release_mode">auto</prop>
     </props>
  </property>
  <property name="mappingResources">
    <list>
      <value>detection/model/conf/rules.hbm.xml</value>
</list>
  </property>
</bean>

And my jdbc datasource config :

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">

    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="initialSize" value="10" />
    <property name="maxActive" value="100" />
    <property name="maxIdle" value="50" />
    <property name="minIdle" value="10" />
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="maxWait" value="2000"/>
    <property name="validationQuery" value="/*ping*/ SELECT 1"/>
    <property name="timeBetweenEvictionRunsMillis" value="2000"/>
    <property name="removeAbandonedTimeout" value="2000"/>
    <property name="removeAbandoned" value="true"/>
    <property name="validationInterval" value="5000"/>

 </bean>

Is there any way to tell hibernate to timeout waiting for a connection after a certain time ?

UPDATE :

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.

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
4

2 回答 2

1

终于用 c3p0 得到了我想要的行为。这是我使用的配置:

    <property name="acquireRetryDelay" value="5000"/>
    <property name="breakAfterAcquireFailure" value="true"/>
    <property name="checkoutTimeout" value="1"/>
    <property name="testConnectionOnCheckin" value="1" />

我认为关键属性是 breakAfterAcquireFailure ,它将正确地从池中删除断开的连接,而 idleCheck 测试在数据库关闭时不会关闭连接并无限期挂起。这使得 checkoutTimeout 正常工作,因为池得到了正确管理。恢复数据库后,连接提供程序也会正确重新连接。

于 2012-12-07T16:29:09.340 回答
0

SO Post描述了如何配置 C3PO 以在无法获取连接时引发异常。

于 2012-12-07T15:13:03.867 回答