kei*_*h h 5 mysql connection spring tomcat c3p0
我使用 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>
Run Code Online (Sandbox Code Playgroud)
我还使用 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>
Run Code Online (Sandbox Code Playgroud)
这是数据源配置的其余部分:
<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>
Run Code Online (Sandbox Code Playgroud)
最后,这是一种将记录更新到数据库中的方法:
@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);
}
Run Code Online (Sandbox Code Playgroud)
我的配置中是否存在错误,或者我是否缺少需要添加到配置或编码中的内容?
这是抛出的异常:
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"
Run Code Online (Sandbox Code Playgroud)
先感谢您。基思
归档时间: |
|
查看次数: |
3211 次 |
最近记录: |