HTTP Status 500 - Could not open Hibernate Session for transaction; JDBC begin transaction failed

最近这几天写了一个可以后台查看洗衣机消费数据的简单系统,前端是用的easyUI,后台用的ssh框架。
数据库用的是mysql。
我在本地项目没问题,但是当放到服务器上的tomcat后,早晨上班访问了一下网站,发现竟然报500。

问题 :每天的第一次访问数据库报500,刷新一下就好了。

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

org.hibernate.TransactionException: JDBC begin transaction failed:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was84531 milliseconds ago.The last packet sent successfully to the server was 84531 milliseconds ago, which 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.

java.net.SocketException: Broken pipe (Write failed)

我看了一下这个错误的输出,说是:最后成功发送的数据到服务器的时间比服务器设置的wait_timeout时间长。

我自己查了一下资料,发现了经典的问题:mysql的空闲8小时,MySQL 就会断开该连接。

经本人亲自测试:
将数据源配置成如下即可解决问题

这里写图片描述

问题解决参考 https://stackoverflow.com/questions/24053684/hibernate-session-could-not-open-for-transaction

以下方式,我测试了一遍都不好使

1.我在hibernate配置中,如下所示 不好使

这里写图片描述

2.我看网上还有一种解决方案 不好使
给jdbc url 增加 autoReconnect=true
如下:

<property name="url" value="jdbc:mysql://localhost/数据库实例名
称?&useUnicode=true&characterEncoding=utf-8&autoReconnect=true"/>

3.最暴力就是直接去修改msyql的配置文件,将这两个值设置为-1(-1表示永不超时)或者很大,此方法从开发的角度讲是不合理的. 这种方式我没有试

猜你喜欢

转载自blog.csdn.net/qdh186/article/details/81301027
今日推荐