2

我正在使用 Java 库 C3PO 来实现与 MySQL 数据库的连接池。我在查询之前和之后记录连接以识别连接泄漏。我发现一个查询使用了将近 20 个不应该使用的连接。事实上,当我检查 MySQL 进程列表时,它会创建 50 个新进程。这会导致整个 webapp 失败,因为后端无法再获得与数据库的连接。

这是导致泄漏的方法的一些伪代码。

public List<Interaction> getInteractions() {
     // Log the # of connections before the query
     logNumConnections();
     --> stdout: Total (7) Idle: (2) Busy: (5) Orphan: (0)

     // Here's the query that's causing the leak
     String sql="select distinct ... from A left join B on A.x=B.y "
            + "where A.x in (?,?,...)"
     List<Interaction> results = jdbcTemplate.query(sql, args, rowMapper);

     // Log the # connections after the query
     logNumConnections();
     --> stdout: Total (24) Idle: (0) Busy: (24) Orphan: (0)

     return results;
}

JdbcTemplate 应该关闭连接并释放资源。一个查询不应该使用 20 个连接!这些连接在查询后很长时间仍然存在。这是我的 JdbcTemplate 和 DataSource 的配置。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
     <property name="driverClass" value="${database.driver}" />
     <property name="jdbcUrl" value="${database.url}"/>
     <property name="user" value="${database.username}"/>
     <property name="password" value="${database.password}"/>
     <property name="initialPoolSize" value="5" />
     <property name="minPoolSize" value="5" />
     <property name="maxPoolSize" value="50" />
     <property name="acquireIncrement" value="1" />
     <property name="maxStatements" value="1000" />
     <property name="maxStatementsPerConnection" value="1000"/>
     <property name="maxIdleTime" value="10000"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <constructor-arg>
          <ref bean="dataSource" />
     </constructor-arg>
</bean>

最后,这是explain声明

id|select_type|table|type  |possible_keys|key    |key_len|rows  | Extra  
 1|SIMPLE     |A    |ALL   |NULL         |NULL   |NULL   |437750| Using where; Using temporary
 1|SIMPLE     |B    |eq_ref|PRIMARY      |PRIMARY|4      |1  

知道什么可能导致此连接泄漏吗?

4

1 回答 1

0

发现了问题。导致连接泄漏的不是上面的查询。这是对单独方法的单独 AJAX 调用,该方法与上述查询在同一时间范围内执行。上面的查询/方法毕竟没有引起任何问题。

于 2013-08-02T21:17:26.103 回答