困扰很久了, 不知道什么原因
使每天早上打开页面系统就会提示类似
The last packet successfully received from the server was86395 milliseconds ago.The last packet sent successfully to the server was 86395 milliseconds ago或者提示
java.sql.SQLException: Operation not allowed after ResultSet closed
hibernate 配置文件如下:
<?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"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
jdbc:mysql://mysql:3306/mydb
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.autocommit">true</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.show_sql">true</property> <!-- c3p0在我们使用的Hibernate版本中自带,不用下载,直接使用 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.idle_test_period">900</property>
<property name="hibernate.c3p0.preferredTestQuery">select 1</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<!-- 以下就全是mapping了,省略 -->
</session-factory></hibernate-configuration>检查了下代码,没有直接关闭session之类的操作
请问各位,到底那里可能出问题啊?

解决方案 »

  1.   

    看看你使用ResultSet的地方是不是使用前已经关闭过了了。特别是while(re.next())这样的地方。
      

  2.   

    看了下mysql的log<property name="hibernate.c3p0.idle_test_period">900</property>
    这个设置是生效的,每900秒hibernate就会使用select 1语句查询数据库,以保持连接所以就很奇怪什么原因了
      

  3.   

    那就是连接未关闭而只是resultset关闭了,就象1楼说的
    java.sql.SQLException: Operation not allowed after ResultSet closed
      

  4.   

    检查过代码了,myeclipse自动生成的自己没有直接使用过connection,或者resultSet
      

  5.   

    那与resultset相关的代码能不能得到并贴出来看下
      

  6.   

    这个错误是因为,数据库空闲时间过长时,释放连接,但连接关闭后,仍然执行数据库操作
    第一个错误是在释放连接后,第一次执行时报的错;以后每次操作就会提示你的第二个错误.
    你的程序里是不是有定时任务?定时任务间隔时间太长了,超过数据库空闲时间了,连接关闭造成的错误.
    把你的wait timeout时间设置长一点.
      

  7.   

    ResultSet  关闭了?  你用了手动操作 hibernate 的方法吗?
      

  8.   

    没有,现在我怀疑是因为session用完后没有close导致的?因为C3P0过900秒执行select来保持连接是可以看到的
      

  9.   

    看一下下面的mysql日志就会发现,C3P0永远都是在刷新612,613,614 3个连接
    程序真正执行过程中用到的是616,而616在使用后根本没有被刷新过,所以8小时后,必然就超时了这里使用hibernate有什么要求吗?
    或者说session每次使用过以后需要什么特殊操作吗?130108 17:28:29   613 Query     select 1
                      612 Query     select 1
                      614 Query     select 1
    130108 17:28:34   613 Query     select 1
                      614 Query     select 1
                      612 Query     select 1
    130108 17:28:39   613 Query     select 1
                      614 Query     select 1
                      612 Query     select 1
    130108 17:28:44   612 Query     select 1
                      614 Query     select 1
                      613 Query     select 1
    130108 17:28:49   614 Query     select 1
                      613 Query     select 1
                      612 Query     select 1
    130108 17:28:54   613 Query     select 1
                      612 Query     select 1
                      614 Query     select 1
    130108 17:28:59   613 Query     select 1
                      614 Query     select 1
                      612 Query     select 1
    130108 17:29:04   614 Query     select 1
                      613 Query     select 1
                      612 Query     select 1
    130108 17:29:09   613 Query     select 1
                      612 Query     select 1
                      614 Query     select 1
    130108 17:29:14   614 Query     select 1
                      613 Query     select 1
                      612 Query     select 1
    130108 17:29:19   614 Query     select 1
                      613 Query     select 1
                      612 Query     select 1
    130108 17:29:21   616 Query     select user0_.id as id61_, user0_.created_on as created2_61_, user0_.disable as disable61_, user0_.password as password61_, user0_.updated_on as updated5_61_, user0_.user_type as user6_61_, user0_.username as username61_ from health_center.user user0_ where user0_.username='11'
    130108 17:29:24   614 Query     select 1
                      612 Query     select 1
                      613 Query     select 1
    130108 17:29:26   616 Query     select user0_.id as id61_, user0_.created_on as created2_61_, user0_.disable as disable61_, user0_.password as password61_, user0_.updated_on as updated5_61_, user0_.user_type as user6_61_, user0_.username as username61_ from health_center.user user0_ where user0_.username='11'
    130108 17:29:29   614 Query     select 1
                      612 Query     select 1
                      613 Query     select 1
    130108 17:29:34   613 Query     select 1
                      614 Query     select 1
                      612 Query     select 1
    130108 17:29:39   614 Query     select 1
                      613 Query     select 1
                      612 Query     select 1
    130108 17:29:44   614 Query     select 1
                      612 Query     select 1
                      613 Query     select 1
    130108 17:29:49   614 Query     select 1
                      612 Query     select 1
                      613 Query     select 1
    130108 17:29:54   613 Query     select 1
                      614 Query     select 1
                      612 Query     select 1
    130108 17:29:59   614 Query     select 1
                      613 Query     select 1
                      612 Query     select 1
    130108 17:30:04   613 Query     select 1
                      612 Query     select 1
                      614 Query     select 1
      

  10.   

    应该还是恰恰相反,用完后close了却还要再用,才导致的java.sql.SQLException: Operation not allowed after ResultSet closed
      

  11.   


    实际证明,session使用完要close,然后这连接就会被返还给连接池了只是每次操作完成都要手工close session还是有点问题啊。
      

  12.   

    还是要确定下异常的出现是不是和这个语句有关或与这个语句的执行有关?select user0_.id as id61_, user0_.created_on as created2_61_, user0_.disable as disable61_, user0_.password as password61_, user0_.updated_on as updated5_61_, user0_.user_type as user6_61_, user0_.username as username61_ from health_center.user user0_ where user0_.username='11'能不能单独用c3p0来测试?等异常出现后利用c3p0来执行此语句java.sql.SQLException: Operation not allowed after ResultSet closed
    还有值得一提的是,异常特别提示了是ResultSet被关闭,我觉得这一点很重要,他既然特别提到了是ResultSet,那就很可能与Connection关闭无关,否则就应该提示Connection closed了,而日志记录中貌似也体现出了连接正常的信息,为了确认,对比一下时间(最后一次成功执行select 1的时间和异常发生后最后一次收发包的时间)。
    130108 17:30:04   613 Query     select 1
                      612 Query     select 1
                      614 Query     select 1
      

  13.   


    hibernate.connection.pool_size 5
    hibernate.c3p0.min_size 50
    hibernate.c3p0.max_size 800
    hibernate.c3p0.max_statements 50
    hibernate.jdbc.batch_size 10
    hibernate.c3p0.timeout 300
    hibernate.c3p0.idle_test_period 3000
    hibernate.c3p0.testConnectionOnCheckout true这是hibernate官网推荐的配置。