Hibernate连接数据库超时设置autoReconnect=true

如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat. 

    不用hibernate的话, connection url加参数: autoReconnect=true 

 


    用hibernate的话, 加如下属性: 

        <property name="connection.autoReconnect">true</property> 
        <property name="connection.autoReconnectForPools">true</property> 
        <property name="connection.is-connection-validation-required">true</property> 

 


    要是还用c3p0连接池: 

        <property name="hibernate.c3p0.acquire_increment">1</property> 
        <property name="hibernate.c3p0.idle_test_period">0</property> 
        <property name="hibernate.c3p0.timeout">0</property> 
        <property name="hibernate.c3p0.validate">true</property>

 

<!-- 指定Hibernate的配置文件 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">    
<props>    
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>    
<prop key="hibernate.show_sql">true</prop>    
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>    
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.autoReconnectForPools">true</prop>
<prop key="hibernate.is-connection-validation-required">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.min_size">8</prop>
<prop key="hibernate.c3p0.max_size">200</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statements">0</prop>
<prop key="hibernate.c3p0.idle_test_period">60</prop>
<prop key="hibernate.c3p0.acquire_increment">2</prop>
<prop key="hibernate.c3p0.validate">true</prop>
</props>    
</property>
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>

 

posted @ 2016-05-23 11:39  243573295  阅读(4064)  评论(0编辑  收藏  举报