2011-11-15 179 views
4

我使用spring 3.0(jdbcTemplate),Tomcat,MySQL和C3p0来处理我的数据库活动。我正在使用jdbctemplate和simplejdbctemplate,这将负责创建和关闭连接,语句,结果集等。我使用C3p0进行连接池,但是连接保持打开状态,最终应用程序将用完连接。数据库连接不关闭

这里是我的数据源的配置:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" > 
     <property name="driverClass" value="${jdbc.driverClassName}"></property> 
     <property name="jdbcUrl" value="${jdbc.url}"></property> 
     <property name="user" value="${jdbc.username}"></property> 
     <property name="password" value="${jdbc.password}"></property> 
     <property name="initialPoolSize" value="5"></property> 
     <property name="maxPoolSize" value="100"/> 
     <property name="minPoolSize" value="5"/> 
     <property name="maxIdleTime" value="30"/> 
     <property name="maxIdleTimeExcessConnections" value="30"/> 
     <property name="maxConnectionAge" value="30"/> 
     <property name="checkoutTimeout" value="100"/> 
     <property name="maxStatements" value="50"></property> 
     <property name="automaticTestTable" value="C3P0_TEST_TABLE"></property> 
     <property name="testConnectionOnCheckin" value="true"></property> 
     <property name="idleConnectionTestPeriod" value="30"></property> 
    </bean> 

我还使用Spring提供TransactionManagement中 - 这里是该配置:

<!-- enable the configuration of transactional behavior based on annotations --> 
    <tx:annotation-driven transaction-manager="txManager"/> 

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

这里的休息数据源配置:

<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> 
     <constructor-arg><ref bean="dataSource"/></constructor-arg> 
    </bean> 
    <bean id="userDAO" class="com.Test.dao.UserDAOImpl"> 
     <property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property> 
    </bean> 

最后这里是一个方法,我更新记录s转换为数据库:

@Transactional(readOnly=false) 
public void updateBenchMarkCumulative(List<BenchMarkCumulative> bmCumulativeList) 
{ 
    List<Object[]> parameters = new ArrayList<Object[]>(); 
    for(BenchMarkCumulative bmCumulative : bmCumulativeList) 
    { 
     parameters.add(new Object[]{bmCumulative.getCumulativeAmt(), bmCumulative.getPkBenchMarkCumulative()}); 
    } 
    this.simpleJdbcTemplate.batchUpdate(UPDATE_BENCHMARK_CUMULATIVE, parameters); 
} 

有什么我做错了,我的配置还是我失去了一些东西需要被添加到配置或编码?

这里是被抛出的异常:

INFO [http-8080-1] (AbstractPoolBackedDataSource.java510) - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ java.beans.IntrospectionException: java.lang.reflect.InvocationTargetException [numThreadsAwaitingCheckoutDefaultUser] ] 


org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" 

预先感谢您。 Keith

+0

您确定没有使用JDBC模板手动访问数据源吗? – axtavt

+1

当连接在池中时,它可能仍然保持打开状态。首先要做的是确保您的maxPoolSize不超过数据库允许的并发连接数。 – mrembisz

+0

所有数据库活动都是使用jdbc模板完成的,但Spring Security除外(它在登录时处理用户验证并且不会导致连接用完)我将假定正在正确处理这些连接。乍一看我的配置似乎很好? –

回答

0

我会同意axtavt。我处于类似的情况,有几个地方我手动打开连接,但没有关闭它们(存储过程调用iSeries)。请确认你是否有这种点。