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提交或者撤销事务时,就会出现上面的异常。
解决方案:其实上面的异常原因有提示具体的解决方案。
<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>