Could not open Hibernate Session for transaction . or java.net.SocketException: Broken pipe

Could not open Hibernate Session for transaction  

最近做一个项目,布暑后出现"Could not open Hibernate Session for transaction",大意是数据库连接超时,需要设置重连.在这里转一个网上找的解决方案:

 

spring2.x+hibernate3+struts2 在首次登陆系统时抛出的异常,再登陆就正常。

 

异常:

javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:

 

root cause

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:

 

 

解决方案:

1.

给jdbc url 增加 autoReconnect=true 一定能解决你的问题,可以定期观察一下 show processlist
改进方法如下:
<property name="url" value="jdbc:mysql://localhost/数据库实例名称?&useUnicode=true&characterEncoding=utf-8&autoReconnect=true"/>

 

2.

寻找支持重连的连接池。
           注意:c3p0连接池支持重连;重连参数是:
               idleConnectionTestPeriod   设置空闲连接测试周期
               preferredTestQuery : 设置一查询语句,用于重连测试
              testConnectionOnCheckin设置为true
              testConnectionOnCheckout设置为true

 

在sessionFactory里配置:

<property name="hibernateProperties">
   <props>

        <prop key="hibernate.autoReconnect">true</prop>

  </props>
</property>

上文转自:http://blog.csdn.net/jife123/article/details/4486266

解决java.net.SocketException: Broken pipe异常 
出现如下异常 解决的方式加上粗体到URL jdbc:mysql://localhost:3306/mydatabase? autoReconnect=true %26amp;amp; useUnicode=true%26amp;amp;characterEncoding=gb2312 Last packet sent to the server was 1 ms ago. STACKTRACE: com
  出现如下异常
解决的方式加上粗体到URL
jdbc:mysql://localhost:3306/mydatabase? autoReconnect=true%26amp;amp;
useUnicode=true%26amp;amp;characterEncoding=gb2312
Last packet sent to the server was 1 ms ago.



STACKTRACE:



com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 



** BEGIN NESTED EXCEPTION ** 



java.net.SocketException

MESSAGE: Broken pipe



STACKTRACE:



java.net.SocketException: Broken pipe

 at java.net.SocketOutputStream.socketWrite0(Native Method)

 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)

 at java.net.SocketOutputStream.write(SocketOutputStream.java:136)

 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)

 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)

 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2692)

 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2621)

 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1552)

 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)

 at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)

 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)

 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030)

 at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)

 at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)

 at org.hibernate.loader.Loader.doQuery(Loader.java:662)

 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)

 at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
 
  
 
  
项目中:/etc/my.cnf中增加:wait_timeout=864000
上文转自:http://sumdwang.blog.163.com/blog/static/79296950200911162150831/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值