【问题标题】:Too many busy connections using JDBCTemplate and c3p0使用 JDBCTemplate 和 c3p0 的繁忙连接过多
【发布时间】:2011-10-14 06:11:09
【问题描述】:

我正在开发一个使用 Spring、JDBCTemplate 和 c3p0 访问数据库的 Web 应用程序。

我经常遇到服务器冻结,我很确定它来自繁忙的数据库连接数。如果我使用 jconsole 观察应用程序行为,我可以看到 ComboPooledDataSource 的 maxPoolSize 已达到,并且服务器不再加载页面。

这里是有用的代码:

数据源定义:

<Resource auth="Container" description="GDLWeb DB Connection"
    driverClass="org.postgresql.Driver"
    maxPoolSize="16"
    minPoolSize="1"
    acquireIncrement="1"
    maxIdleTime="60"
    maxStatements="0"
    idleConnectionTestPeriod="1800"
    acquireRetryAttempts="30"
    breakAfterAcquireFailure="true"
    name="jdbc/gdlweb"
    user="gdlweb"
    password=""
    factory="org.apache.naming.factory.BeanFactory"
    type="com.mchange.v2.c3p0.ComboPooledDataSource"
    jdbcUrl="jdbc:postgresql://localhost:5432/postgres"
/>

典型访问方法(在 DAO 类中):

protected T getPersistentObject(
        final String tableName,
        final List<WhereClause> whereParams,
        final RowMapper<T> rowMapper) {

    try {
        log.debug(this, "get " + tableName + " " + whereParams);
        return (T) getTemplate().queryForObject(
                generateSelectStar(tableName, whereParams),
                extractValueMap(whereParams),
                rowMapper);
    } catch (final EmptyResultDataAccessException e) {
        log.warning(this, "No " + tableName + " found with " + whereParams + " in the DB!");
        return null;
    }
}

我尝试将 maxPoolSize 增加到 100,这是我的 postgresql 服务器中定义的 maxConnections。这样,我可以看到当前打开了 43 个繁忙的连接,就在 postgresql 服务器崩溃之前。

我可能用错了 JDBCTemplate,但我不知道在哪里。

谢谢。

【问题讨论】:

    标签: c3p0 connection jdbctemplate


    【解决方案1】:

    问题可能与您使用的 Mysql Connector/J 版本有关。

    我遇到了同样的问题,更新到新的 Mysql Connector v5.1.15 为我解决了这个问题。 v5.1.13 有一个错误导致您看到的问题

    修复错误的版本的更改日志:http://dev.mysql.com/doc/refman/5.1/en/cj-news-5-1-14.html

    谢谢

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 2012-11-03
      • 2014-09-28
      • 2015-01-31
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      相关资源
      最近更新 更多