Home  >  Article  >  Database  >  一次c3p0连接池连接异常错误的排查

一次c3p0连接池连接异常错误的排查

WBOY
WBOYOriginal
2016-06-07 15:57:021305browse

最近写了一个数据库采集程序,大概过程是将SQLSERVER数据库的数据定时采集到Oracle数据库。1小时出一次数据,每次数据量在2W左右。环境采用Sping3+hibernate4,数据库连接池采用C3p0 奇怪的时候每隔一段时间都会报:c3p0 connection is already closed 我开始

最近写了一个数据库采集程序,大概过程是将SQLSERVER数据库的数据定时采集到Oracle数据库。1小时出一次数据,每次数据量在2W左右。环境采用Sping3+hibernate4,数据库连接池采用C3p0

奇怪的时候每隔一段时间都会报:“c3p0 connection is already closed”

我开始的数据库连接池配置如下:oracle数据库开启事务,而采集的sqlserver数据库没有开启事务

jdbc.driverClass=oracle.jdbc.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=10.12.18.240)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))
jdbc.user=appds
jdbc.password=appds
c3p0.acquireIncrement=5
c3p0.maxIdleTime=60
c3p0.maxPoolSize=80
c3p0.minPoolSize=10
c3p0.initialPoolSize=10
c3p0.maxStatements=0
c3p0.idleConnectionTestPeriod=60
c3p0.acquireRetryAttempts=30
c3p0.acquireRetryDelay=1000
c3p0.breakAfterAcquireFailure=false
c3p0.testConnectionOnCheckout=false

jdbcdata.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcdata.jdbcUrl=jdbc:sqlserver://10.12.18.241:1433;databaseName=data;
jdbcdata.user=cawasdatauser
jdbcdata.password=cawas606
c3p0ObsDataDB.maxPoolSize=30
发现了问题,我首先在c3p0上加上调试信息的配置:

c3p0.debugUnreturnedConnectionStackTraces=true
c3p0.unreturnedConnectionTimeout=90 (我的连接超时时间是60s,所以这设置了90s)

applicationContext数据源配置增加响应配置


果然发现很多未回收的连接,正常情况下超时未回收的连接会有一些,但是不会这么多啊。

经查资料在hibernate sessionFacory中增加配置(http://hi.baidu.com/austincao/item/fc9907da3d854e44fa576861)

Spring3.1去掉了HibernateDaoSupport类。hibernate4需要通过getCurrentSession()获取session。并且设置
org.springframework.orm.hibernate4.SpringSessionContext

在Spring @Transactional声明式事务管理,”currentSession”的定义为: 当前被 Spring事务管理器 管理的Session,此时应配置:
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext。

另外在hibernate中使用sessionFactory.getCurrentSession()获取session时,需要为方法声明事务,为此将sqlserver

采集的代码也加上事务

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn