`
什么世道
  • 浏览: 223136 次
  • 性别: 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
分享到:
评论

相关推荐

    cp30报错No operations allowed after connection closed

    使用mysql+cp30连接池时,报错No operations allowed after connection closed。从报错信息来看,是connection断开导致的错误。在网上搜索后发现,较新版本的mysql配置了connection的默认时间,默认时间一般为8个...

    BURNINTEST--硬件检测工具

    - Video playback, Hard disk and CD/DVD test 'no operations' error reporting changed. - When BurnInTest crashes, it will not generate a "minidump" file. Minidumps will need to be sent to Microsoft ...

    微软内部资料-SQL性能优化3

    If no rows satisfy the WHERE condition the first time the range is scanned, no rows should be returned on any subsequent scans. Key range locks are similar to row locks on index keys (whether ...

    a project model for the FreeBSD Project.7z

    After providing definitions of terms used, this document will outline the organisational structure (including role descriptions and communication lines), discuss the methodology model and after ...

    微软内部资料-SQL性能优化2

    To reserve or commit memory and unintentionally not release it when it is no longer being used. A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: After deleting the selected points in the TFlexPanel.DeleteSelectedPoints the all figures with one point also deleted. - FIX: When the curve contain more then one figure and they were all ...

Global site tag (gtag.js) - Google Analytics