今天在作一個300多M的xml文件加載入庫的過程當中發現一個嚴重的問題,通過幾番查找後終於得以解決,在此作下記錄:java
問題描述: spring
加載xml文件的過程當中有XML格式校驗、解析、數據校驗、入庫四步操做,通過監控發如今入庫以前的三個過程執行的時間較長,以致於在進行入數據庫的過程當中後臺拋出以下異常:sql
2009-04-03 09:10:46,953 - ERROR TransactionInterceptor - Application exception overridden by rollback exception
org.springframework.dao.DataAccessResourceFailureException: SqlMapClient operation; SQL [];
--- The error occurred in net/htjs/rkxx/dao/sqlmaps/CZRK_BDXX_ZB.xml.
--- The error occurred while applying a parameter map.
--- Check the getSEQ_BDXX_ID-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: Io 異常: Connection reset by peer: socket write error; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in net/htjs/rkxx/dao/sqlmaps/CZRK_BDXX_ZB.xml.
--- The error occurred while applying a parameter map.
--- Check the getSEQ_BDXX_ID-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: Io 異常: Connection reset by peer: socket write error
Caused by:
com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in net/htjs/rkxx/dao/sqlmaps/CZRK_BDXX_ZB.xml.
--- The error occurred while applying a parameter map.
--- Check the getSEQ_BDXX_ID-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: Io 異常: Connection reset by peer: socket write error
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:185)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForObject(GeneralStatement.java:104)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:566)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:541)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:106)
at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:243)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193)
at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:241)
at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:235)
at net.htjs.rkxx.dao.impl.IDaoCZRK_BDXX_ZBImpl.getSEQ_BDXX_ID(IDaoCZRK_BDXX_ZBImpl.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:177)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy3.getSEQ_BDXX_ID(Unknown Source)
at net.htjs.rkxx.service.rk.BDXXRKService.doSaveData(BDXXRKService.java:152)
at net.htjs.rkxx.service.rk.BDXXRKService.doService(BDXXRKService.java:58)
at net.htjs.listener.FileScanner$Processor.run(FileScanner.java:98)
Caused by: java.sql.SQLException: Io 異常: Connection reset by peer: socket write error
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:812)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1031)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:836)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1124)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3390)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:256)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.ibatis.common.jdbc.logging.PreparedStatementLogProxy.invoke(PreparedStatementLogProxy.java:62)
at $Proxy6.execute(Unknown Source)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:186)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteQuery(GeneralStatement.java:205)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
... 23 more數據庫
問題分析:apache
程序中採用的是Spring+IBatis,數據源配置以下:oracle
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="maxActive" value="100"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="defaultAutoCommit" value="true"/> </bean>
通過查找資料和分析,得出該問題主要是因爲鏈接時間過長,失效所致,將以上配置修改以下:app
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="maxActive" value="100"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="defaultAutoCommit" value="true"/> <span style="background-color: rgb(255, 0, 0);"><property name="validationQuery" value="select * from dual"/></span> </bean>
修改後通過測試,程序運行正常socket
問題總結:測試
當數據庫鏈接池中的鏈接被建立而長時間不使用的狀況下,該鏈接會自動回收並失效,但客戶端並不知道,在進行數據庫操做時仍然使用的是無效的數據庫鏈接,這樣,就致使客戶端程序報「 java.sql.SQLException: Io 異常: Connection reset by peer」或「java.sql.SQLException 關閉的鏈接」異常,加上<property name="validationQuery" value="select * from dual"/>url
配置後,客戶端在使用一個無效的鏈接時會先對該鏈接進行測試,若是發現該鏈接已經無效,則從新從鏈接池獲取有效數據庫鏈接來使用
以上是我的的分析和理解,有不對的地方請你們指出共同討論~~~