我为了解决hibernate连mysql数据库的长时间没有处理后数据库访问异常(com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.),引入了c3p0库,但配置重启后问题依旧,不知道是不是哪些地方配置的有问题,请各位大侠指点!
mysql的配置:
  interactive_timeout=20
  wait_timeout=20hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
 <property name= "hibernate.connection.provider_class ">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">20</property> 
<property name="hibernate.c3p0.max_size">50</property> 
<property name="hibernate.c3p0.timeout">20</property> 
<property name="hibernate.c3p0.max_statements">100</property> 
<property name="hibernate.c3p0.idle_test_period">20</property> 
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.automaticTestTable">true</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">20</property>
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="show_sql">true</property>
<mapping resource="entity/config.hbn.xml"/>
</session-factory>
</hibernate-configuration>

解决方案 »

  1.   

    我记得这个问题好像是mysql的问题,而不是hibernate的,记得需要改mysql的一个参数,修改连接时间,默认时间好像是8个小时
      

  2.   

     自己解决了,少了一个参数:validate
      

  3.   

    fireelf000:
    给个关注奖吧!
      

  4.   

    这个问题是mysql4.x前的bug,跟hibernate没有关系,但可以通过配置连接池的属性解决
    <prop key="hibernate.generate_statistics">true</prop>
    <prop key="hibernate.connection.release_mode">auto</prop>
    <prop key="hibernate.autoReconnect">true</prop>
    <prop key="hibernate.use_sql_comments">false</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>