1

我的 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>`

并且执行插入语句的代码是:

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

我可以在日志中看到 Jdbc Connection 由 Spring 返回到 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 会按照我在文档中阅读的那样做到这一点。
有人可以在这里提供帮助吗?

谢谢
Gopi
www.allibilli.com

4

0 回答 0