java.sql.SQLException: 關閉的串連 解決辦法

來源:互聯網
上載者:User

標籤:

程式如果長時間不進行資料庫操作,那麼資料來源中的 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

java.sql.SQLException: 關閉的串連 解決辦法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.