我使用的Java库C3PO实现连接MySQL数据库池。我在查询之前和之后记录连接以识别连接泄漏。我发现一个查询不应该使用近20个连接。事实上,当我检查MySQL进程列表时,它创建了50个新进程。 这导致整个Web应用程序失败,因为后端不再能获取到数据库的连接。莫名连接泄漏崩溃的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
任何想法,这可能是造成这种连接泄漏?
,我在我的配置唯一的差异是,不是要求数据源REF:<豆ID =“JdbcTemplate的”类=“org.springframework.jdbc.core.JdbcTemplate”> <构造带参数> constructor-arg> 在一个构造函数中,我在类级别中执行它,然后调用JDBCTemplate另一个比较是池的初始大小是1并且渐进式在5中。 –