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/
阅读更多
个人分类: 服务器
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭