apache的开源连接池 - BasicDataSource问题

今天在做一个300多M的xml文件加载入库的过程中发现一个严重的问题,经过几番查找后终于得以解决,在此做下记录:

 

问题描述:

加载xml文件的过程中有XML格式校验、解析、数据校验、入库四步操作,经过监控发现在入库之前的三个过程执行的时间较长,以至于在进行入数据库的过程中后台抛出如下异常:

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

 

问题分析:

程序中采用的是Spring+IBatis,数据源配置如下:

<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>

 

经过查找资料和分析,得出该问题主要是由于连接时间过长,失效所致,将以上配置修改如下:

<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"/>
        <property name="validationQuery" value="select * from dual"/>
 </bean>

 修改后经过测试,程序运行正常

 

问题总结:

当数据库连接池中的连接被创建而长时间不使用的情况下,该连接会自动回收并失效,但客户端并不知道,在进行数据库操作时仍然使用的是无效的数据库连接,这样,就导致客户端程序报“ java.sql.SQLException: Io 异常: Connection reset by peer”或“java.sql.SQLException 关闭的连接”异常,加上<property name="validationQuery" value="select * from dual"/>

配置后,客户端在使用一个无效的连接时会先对该连接进行测试,如果发现该连接已经无效,则重新从连接池获取有效数据库连接来使用

 

 

以上是个人的分析和理解,有不对的地方请大家指出共同讨论~~~

你可能感兴趣的:(java,apache,oracle,ibatis,jdbc)