`
什么世道
  • 浏览: 219612 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

No operations allowed after connection closed异常的解决方案

阅读更多

异常详细:ERROR: No operations allowed after connection closed.

异常原因:Caused by: org.hibernate.TransactionException: unable to rollback against JDBC connection

                  .................

                  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 53,775,153 milliseconds ago.  The last packet sent successfully to the server was 53,775,153 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

 

Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。当connection设置为static类型时候,connection pools中的connections如果空闲超过8小时,mysql将其断开,connection提交或者撤销事务时,就会出现上面的异常。

 

解决方案:其实上面的异常原因有提示具体的解决方案。

 

a.增加wait_timeout的时间。
b.减少Connection pools中connection的lifetime。

 

c.测试Connection pools中connection的有效性。
具体操作:修改连接池配置中的 URL,添加一个参数:autoReconnect=true(不推荐)
 如果使用的是Hibernate框架,建议该用c3p0数据库连接池
添加在工程中添加c3p0-0.9.2.1.jar,hibernate-c3p0-4.3.5.Final.jar,mchange-commons-java-0.2.3.4.jar 3个包。
 hibernate.cfg.xml配置文件中添加如下c3p0完整配置信息。
<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> 
<!-- 最小连接数 --> 
<property name="hibernate.c3p0.min_size">5</property>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">300</property>
<!-- 每隔3000秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!--查询的最大结果集 -->
<property name="hibernate.c3p0.max_statements">50</property>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
 
最后也是最重要的是,加入数据库访问不是非常频繁时,每次使用完connection 对象时,应将器关闭,需要事务处理时再创建。 
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics