我在Spring应用程序中有一个已配置的数据源(mySQL),如下所示:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
<property name="defaultAutoCommit">
<value>${db.con.defaultAutoCommit}</value>
</property>
<property name="initialSize" value="12"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="2"/>
</bean>
在我的代码中,我获取jdbcTemplate并执行一些插入语句,如下所示:`
<bean id="doTransaction" class="com.lch.spring.BusinessComponents.DoTransaction">
<property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>
<property name="namedParameterJdbcTemplate"><ref bean="namedParameterJdbcTemplate"/></property>
</bean>`
执行insert语句的代码是:
public int insertLCH_BUSINESS(LCH_BUSINESS lchBusiness) {
int businessId = -1;
try {
log.info("Start Doing Transaction - Business");
KeyHolder keyHolder = new GeneratedKeyHolder();
SqlParameterSource parametersSource = new BeanPropertySqlParameterSource(
lchBusiness);
getNamedParameterJdbcTemplate().update(
SQLQueries.INSERTLCH_BUSINESS, parametersSource, keyHolder);
businessId = keyHolder.getKey().intValue();
} catch (Exception e) {
e.printStackTrace();
}
return businessId;
}
问题:当我多次执行此插入代码时,我遇到了池耗尽错误。
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
...............
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:103)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 38 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:756)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
... 41 more
我可以在日志中看到Spring将Jdbc Connection返回给DataSource,如下所示:
[http-8080-2] DEBUG [JdbcTemplate.java : 570 ][Dec 17 2012 00:43:09.531] Executing prepared SQL statement [INSERT INTO `addressinfo` (`address1`,`address2`,`city`,`state`,`country`,`zipcode`,`landmark`) VALUES (?,?,?,?,?,?,?)]
[http-8080-2] DEBUG [DataSourceUtils.java : 110 ][Dec 17 2012 00:43:09.532] Fetching JDBC Connection from DataSource
[http-8080-2] DEBUG [JdbcTemplate.java : 860 ][Dec 17 2012 00:43:09.564] SQL update affected 1 rows and returned 1 keys
[http-8080-2] DEBUG [DataSourceUtils.java : 332 ][Dec 17 2012 00:43:09.565] Returning JDBC Connection to DataSource
我尝试使用C3P0和Bounre CP没有运气。我是否需要明确关闭连接?我相信jdbcTemplate会在我阅读文档时这样做。
有人可以在这里帮忙吗?
感谢
戈皮
www.allibilli.com