无法切换自动提交

时间:2011-01-21 17:16:36

标签: java mysql hibernate jdbc

我有一个在Java Layer中运行了几个小时的事务,当Hibernate尝试持有整理的数据时,抛出如下所示的异常堆栈跟踪。

注意:我也尝试了在..

中指定的配置

http://forums.mysql.com/read.php?39,52805,205216#msg-205216http://forums.mysql.com/read.php?39,52805,273371#msg-273371

++++++

ERROR org.hibernate.transaction.JDBCTransaction (JDBCTransaction.java:232) - Could not toggle autocommit 
java.sql.SQLException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error: 


** BEGIN NESTED EXCEPTION ** 

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

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException 

STACKTRACE: 

java.io.EOFException 
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1903) 
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2349) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2860) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666) 
    at com.mysql.jdbc.Connection.execSQL(Connection.java:2972) 
    at com.mysql.jdbc.Connection.commit(Connection.java:2147) 
    at org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:301) 
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.commit(PoolingDataSource.java:200) 
    at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170) 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146) 
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656) 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625) 

3 个答案:

答案 0 :(得分:3)

MySQL有一个默认的超时时间,之后它会终止空闲连接。默认情况下,时间为8小时。

然而,DBCP并不知道MySQL服务器何时终止连接,因此这些过时的连接仍然按需发送给应用程序。因此,你会得到一个例外。

修复方法是将以下属性添加到dbcp config:

  • validationQuery =“SELECT 1”
  • testOnBorrow =”真”

这告诉DBCP验证与该查询的连接。

答案 1 :(得分:0)

数据库很可能删除了连接。在DBCP上指定验证查询以确保池中的连接仍处于活动状态。

http://commons.apache.org/dbcp/configuration.html

答案 2 :(得分:0)

当我们在Spring应用程序中使用Lucene尝试索引数据时,我们正在使用非常相似的堆栈跟踪。问题是wait_timeout中的my.cnf设置。我们刚刚升级了我们的MySQL安装,我们的DBA忘记将wait_timeout从默认设置更改为之前的3600。