今天部署到新浪上的一个应用,抛出了这个异常:
Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: rollback failed
MySQLNonTransientConnectionException: No operations allowed after statement closed
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 10,789 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
看异常信息应给是:连接断开,但是提交事务时,找不到连接,所以抛异常。
原因:开启事务与提交事务的时长超出了,连接等待的时长。
于是看了下mysql的设置:SHOWVARIABLES LIKE '%timeout%';
interactive_timeout | 10 |
wait_timeout | 10 |
居然设置的是10秒,可是我用的是连接池,从创建连接到执行操作难道要在10秒内完成吗,那我的连接池还有意义吗?
而mysql的默认设置应给是28800秒,即8小时。
解决办法有两种:
一、修改数据库设置
1. 修改配置文件,my.cnf(windows下my.ini),在[mysqld]下加两行interactive_timeout=设置值 wait_timeout=设置的值 然后重启mysql服务
2. 直接在sql命令行里设置 set global interactive_timeout=设置值 set global wait_timeout=设置值 这样的设置好像是只要一重启mysql服务 就会还原的
二、修改应用配置
将事务的操作时间控制在配置范围内。连接池闲置连接关掉设置为0 maxIdle=0。(保证每次获得的连接是最新的,已便有足够的时间).
优化自己的单个事务的处理时长,以便在合理范围内!