java.sql.SQLException: 关闭的连接 解决办法

转载 2015年07月07日 14:27:43

程序如果长时间不进行数据库操作,那么数据源中的 Connection 很可能已经断开。其原因有可能是防火墙,或者连接的数据库设置的超时时间。这里使用的是 C3P0 连接 oracle 数据库,引起的异常信息为:

 

 

org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: rollback failed

at org.springframework.orm.hibernate4.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:503)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822)

at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:411)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:114)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)

at com.platform.dao.TasksDAO$$EnhancerByCGLIB$$abcb28f5.getTaskById(<generated>)

 

at com.platform.manager.TasksManager.getTaskById(TasksManager.java:287)

at com.platform.manager.TasksManager.checkResult(TasksManager.java:300)

at com.platform.timer.TimerBase.tasksListener(TimerBase.java:149)

at com.platform.timer.TimerBase.run(TimerBase.java:81)

at java.util.TimerThread.mainLoop(Timer.java:555)

at java.util.TimerThread.run(Timer.java:505)

Caused by: org.hibernate.TransactionException: rollback failed

at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:215)

at org.springframework.orm.hibernate4.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:500)

... 15 more

Caused by: org.hibernate.TransactionException: unable to rollback against JDBC connection

at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:167)

at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:209)

... 16 more

Caused by: java.sql.SQLException: 关闭的连接

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:207)

at oracle.jdbc.driver.PhysicalConnection.rollback(PhysicalConnection.java:1160)

at com.alibaba.china.jdbc.proxy.simple.ConnectionProxy.rollback(ConnectionProxy.java:228)

at com.mchange.v2.c3p0.impl.NewProxyConnection.rollback(NewProxyConnection.java:855)

at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:163)

... 17 more

 

异常抛出的地方为 Spring 的事务管理 transactionManager,而并非程序代码。即便捕捉到异常,使用 sessionFactory.openSession 也会抛出如下异常:

 

 

org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: rollback failed

at org.springframework.orm.hibernate4.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:503)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822)

at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:411)

 

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:114)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)

 

at com.platform.dao.TasksDAO$$EnhancerByCGLIB$$abcb28f5.getTaskByNewSession(<generated>)

at com.platform.manager.TasksManager.checkResult(TasksManager.java:303)

at com.platform.timer.TimerBase.tasksListener(TimerBase.java:149)

at com.platform.timer.TimerBase.run(TimerBase.java:81)

at java.util.TimerThread.mainLoop(Timer.java:555)

at java.util.TimerThread.run(Timer.java:505)

Caused by: org.hibernate.TransactionException: rollback failed

at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:215)

 

at org.springframework.orm.hibernate4.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:500)

... 14 more

Caused by: org.hibernate.TransactionException: unable to rollback against JDBC connection

at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:167)

at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:209)

... 15 more

Caused by: java.sql.SQLException: Io 异常: Software caused connection abort: recv failed

 

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:254)

at oracle.jdbc.driver.T4CConnection.doRollback(T4CConnection.java:577)

at oracle.jdbc.driver.PhysicalConnection.rollback(PhysicalConnection.java:1163)

at com.alibaba.china.jdbc.proxy.simple.ConnectionProxy.rollback(ConnectionProxy.java:228)

at com.mchange.v2.c3p0.impl.NewProxyConnection.rollback(NewProxyConnection.java:855)

at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:163)

... 16 more

 

解决的办法是:

 

c3p0 自定义配置:

 

 

 

<!-- 数据库连接声明 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="com.alibaba.china.jdbc.SimpleDriver" />

<!-- <property name="driverClass" value="oracle.jdbc.OracleDriver" /> -->

<property name="jdbcUrl" value="jdbc:oracle:thin:@202.116.70.180:61166:orcl" />

<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->

<property name="idleConnectionTestPeriod" value="60" />

  <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->

<property name="testConnectionOnCheckin" value="true" />

<!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试 

使用。Default: null -->

<property name="automaticTestTable" value="Test" />

 

<property name="properties">

<props>

<prop key="clientEncoding">GBK</prop>

<prop key="serverEncoding">CP1252</prop>

<prop key="user">WDS</prop>

 <prop key="password">HelloOracle</prop> 

</props>

</property>

</bean>

 

配置项一定要作为 <bean> 的子元素,而不能做为 <properties> 的子元素,否则不起作用。

 

 

参考:

http://my.oschina.net/tianzimensheng/blog/65225

http://www.cnblogs.com/younes/archive/2012/06/01/2529483.html

http://www.cnblogs.com/safeking/archive/2007/02/01/637067.html

相关文章推荐

使用Spring JDBC时遇到的Software caused connection abort: recv failed问题

在使用Spring jdbc连接数据库时遇到一个头疼的问题:程序启动以后,如果长时间不访问调用,当再次调用时会报错:  引用 org.springframework.dao.DataA...

java.sql.SQLException: Io 异常: Software caused connection abort: socket write error

java.sql.SQLException: Io 异常: Software caused connection abort: socket write error     今天写了一个广播程序...
  • jingj6
  • jingj6
  • 2012-08-17 14:12
  • 2006

解决Caused by: java.sql.SQLRecoverableException: IO Error: Connection reset相关问题

java.sql.SQLException: Io 异常: Connection reset 当数据库连接池中的连接被创建而长时间不使用的情况下,该连接会自动回收并失效,但客户端并不知道,在进行...

使用Spring JDBC时遇到的Software caused connection abort: recv failed问题

在使用Spring jdbc连接数据库时遇到一个头疼的问题:程序启动以后,如果长时间不访问调用,当再次调用时会报错:  引用 org.springframework.dao.DataA...

java.sql.SQLException: 关闭的连接

最近做一个统计数据的小工具,发现在自己机器上运行正常,移到其他机器上就报: java.sql.SQLException: 关闭的连接 的错误,开始以为是防火墙的问题,经过测试,发现和防火墙无关。 ...

Hibernate开发中常见错误总结

org.hibernate.MappingException: Unknown entity: cn.itcast.hibernate717.helloworld.Person hibernate框架...

java.sql.SQLRecoverableException: 关闭的连接 解决办法

程序如果长时间不进行数据库操作,那么数据源中的 Connection 很可能已经断开。其原因有可能是防火墙,或者连接的数据库设置的超时时间。 问题总结: 当数据库连接池中的连接被创建而长时间不使用...

Java进阶(二十五)Java连接mysql数据库(底层实现)

Java进阶(二十五)Java连接mysql数据库(底层实现) 前言       很长时间没有系统的使用java做项目了。现在需要使用java完成一个实验,其中涉及到java连接数据库。让自己来写...

java.sql.SQLException: 关闭的连接

原因:试图使用已经关闭的Connection,Statement,ResultSet。 如果在rs.next()之前关闭了Statement或PreparedStatement,会导致下面的异常: ...

java.sql.SQLException: Access denied for user 'admin'@'localhost' (using password)解决办法

java.sql.SQLException: Access denied for user 'admin'@'localhost' (using password) 然而事实我并没有admin的mys...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)