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

程序如果长时间不进行数据库操作,那么数据源中的 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

时间: 2024-10-12 19:02:52

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

java.sql.SQLException: 数字溢出 的解决办法

java.sql.SQLException: 数字溢出 的解决办法 今天碰到了 Java.sql.SQLException: 数字溢出.............的错误. 原因可能有以下两种,一:使用的jdbc驱动与数据库版本不匹配引起的,把版本改为一致的,测试看看: 二:可能是数据库的某个NUMBER类型的字段超出对应实体类属性的接收范围(如数据库字段是11位的手机号,而对应实体类使用Integer接收Phone这个属性,必然溢出), 解决方法:将实体类该属性的数据类改为Long即可.

java.sql.SQLException: 关闭的 Resultset: next

根据异常信息判断是数据库查询出来的结果集被关闭了,所以就了next 我的代码是一个Impl方法(假设为A方法)中调用另一个Impl方法(假设为B方法),我在执行完B方法后,调用了一下关闭数据库连接的方法,结果把A方法连接数据库给关闭了; 解决方法是把B方法中 关闭数据库连接的方法(即:super.close(con, pstmt, null, rs);)给删除了! 下面是我的出错误的代码: /** * 查询延期的任务 */ @Override public List<Extension> fi

使用Hibarnate: 出现 java.sql.SQLException: ORA-00911: 无效字符, 解决思路

1. 查看到: Hibernat自动生成的sql查询语句 Hibernate: select * from ( select module0_.MODULE_ID as MODULE_ID1_1_, module0_.CREATE_BY as CREATE_BY2_1_, module0_.CREATE_DEPT as CREATE_DEPT3_1_, module0_.CREATE_TIME as CREATE_TIME4_1_, module0_.UPDATE_BY as UPDATE_BY

连接mysql报错java.sql.SQLException: The server time zone value &#39;?й???????&#39; is unrecognized...解决方法

报错内容: java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time

出现java.sql.SQLException: No suitable driver的几种解决办法

今天在用C3p0连接MySql数据库 这个时候出现了上面的错误, 一般来说这种错误有3个方向解决 一般都是db的配置的问题 ,一定要小心DB 的配置 八月 19, 2017 8:30:46 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run警告: [email protected]95 -- Acquisition Attempt Failed!!! Clearing pending acquires. While t

java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1解决办法

java.sql.SQLException: 索引中丢失  IN 或 OUT 参数:: 1 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162) at oracle.jdbc.driver.OraclePreparedStatement.pr

java.sql.SQLException: Unknown type &#39;246 in column 0 of 1 in binary-encoded result set的解决办法

最近在大学毕设项目基础上新增一些功能,基于Struts2开源框架,数据库采用的是MySQL.写完后台的新功能代码,单元测试也顺利通过,去整合前台,但是当使用JSP页面操作的时候,没想到报异常了.截图如下: java.sql.SQLException: Unknown type '246 in column 2 of 4 in binary-encoded result set. 纳闷了,为什么能够通过JUnit呢? 几经查找,都说是MySQL的bug,更换了MySQL新版本的驱动包,比如mysq

MySQL java连接被拒绝:java.sql.SQLException: Access denied for user &#39;root&#39;@&#39;****&#39; (using password: YES)

java.sql.SQLException: Access denied for user 'root'@'***.**.**.**' (using password: YES) MySQL服务器部署在Linux上,错误中被我隐掉的是我的公网IP. 这是由于"IP为***.**.**.**,名字为root"的用户 没有权限访问数据库 解决办法: 通过Navicat连接数据库,执行下面内容: grant all privileges on *.* to [email protected]

连接mysql时提示java.sql.SQLException: Access denied for user &#39;root&#39;@&#39;DESKTOP-N2B2D9A&#39; (using password: YES)

用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着..  然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者127.0.0.1)进行连接 解决方法:新开一个权限GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jeffw' WITH GRANT OPTION; 然后查询一下:select * from user; 新开的root的已经开启权限,