Hibernate用Mysql数据库时链接关闭异常的解决



Mysql在经过8小时不使用后会自动关闭已打开的连接,摘录原文如下:
5.4.  I have a servlet/application that works fine for a day, and then stops working overnight
MySQL closes connections after 8 hours of inactivity. You either need to use a connection pool that handles stale connections or use the "autoReconnect" parameter (see "Developing Applications with MySQL Connector/J").
Also, you should be catching SQLExceptions in your application and dealing with them, rather than propagating them all the way until your application exits, this is just good programming practice. MySQL Connector/J will set the SQLState (see java.sql.SQLException.getSQLState() in your APIDOCS) to "08S01" when it encounters network-connectivity issues during the processing of a query. Your application code should then attempt to re-connect to MySQL at this point.

为 此试验三种方法:
1、在数据库的url中加入autoReconnect=true;
2、在每次调用getSession()方法时判断 session.isClosed()是否为真,若为真则调用session.reconnect();
3、不使用Hibernate内置的连接池(Hibernate强烈推荐不使用但我以前一直在用),改用C3P0连接池,这个连接池会自动 处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.8.3.jar复制到项目的lib目录中,再在 hibernate.properties里去掉hibernate.c3p0开头的那些属性的注释(使用缺省值或自己需要的数值),这样 Hibernate就会自动使用C3P0代替内置的连接池了。到目前为止前面的问题没有再出现过。

以前对Hibernate警告不要使用内置连接池作产品用途没有太放在心上,其实不论从稳定还是性能的考虑,都应该选择相对更加成熟的连接池。

C3P0的具体配置方方法, 其他连接池(如DBCP等)大同小异。一下配置主要只针对连接池部分。
1.1 hibernate.properties中的配置
hibernate.show_sql = true 
hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect 
hibernate.connection.driver_class = com.mysql.jdbc.Driver 
hibernate.connection.url = jdbc:mysql://localhost/HibernateTest 
hibernate.connection.username = root 
hibernate.connection.password = 
hibernate.c3p0.min_size=5 
hibernate.c3p0.max_size=20 
hibernate.c3p0.timeout=1800 
hibernate.c3p0.max_statements=50

1.2在hibernate.cfg.xml中直接配置C3P0连接池
<property name="c3p0.min_size">5</property><!--连接池的最小连接数-->
    <property name="c3p0.max_size">20</property><!--最大连接数-->
    <property name="c3p0.timeout">100</property><!--连接超时时间-->
    <!每隔100秒检测连接是否可正常使用 -->
     <property name="c3p0.idle_test_period">100</property>
    <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3-->
<property name="hibernate.c3p0.acquire_increment">3</property>
<!--statemnets缓存大小-->
<property name="c3p0.max_statements">150</property>

2.在Spring配置文件中配置C3P0连接池

<!--数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--驱动类-->
<property name="driverClassName">
<value> oracle.jdbc.driver.OracleDriver</value>
</property>
<!--连接字符串-->
<property name="url">
    <value> jdbc:oracle:thin:@210.51.173.22:1521:orcl</value>
</property>
</bean>

<!--数据库属性配置 -->

<bean id="hibernateProperties"   class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
   <props>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop>
    <prop key="hibernate.query.substitutions">true 'T', false 'F'</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.c3p0.minPoolSize">5</prop>
    <prop key="hibernate.c3p0.maxPoolSize">20</prop>
    <prop key="hibernate.c3p0.timeout">600</prop>
    <prop key="hibernate.c3p0.max_statement">50</prop>
</property>
</bean>

<!--配置Hibernate的SessionFactory-->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
      <ref local="dataSource"/>
</property>
<property name="hibernateProperties">
      <ref bean="hibernateProperties" />
</property>
<!--   添加po映射文件 -->
<property name="mappingResources">
     <list>
        <value>Customer.hbm.xml</value>
        <value>Account.hbm.xml</value>
     </list>
</property>
</bean>

你可能感兴趣的:(spring,oracle,mysql,Hibernate,jdbc)