DBCP连接池泄漏:Connection.close()不会将连接返回到池并且连接已用尽

时间:2012-06-11 08:03:41

标签: mysql jdbc connection connection-pooling

DBCP连接池泄漏。

当程序返回使用的连接时,不返回连接资源  游泳池,筋疲力尽!!!

因此,在maxActive时间Connection.close()中,当getConnection()时,池会挂起。

请让我知道如何解决这个问题!!!

图书馆:

公地DBCP-1.4.jar commons-pool的-1.6.jar MySQL的连接器的Java-5.1.12.jar

Mysql版本:5.5.9

来源:

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;



class ConnectionManager {

    static {

        java.sql.DriverManager.registerDriver((java.sql.Driver) 
                  Class.forName("org.apache.commons.dbcp.PoolingDriver").newInstance()) ;
        java.sql.DriverManager.registerDriver((java.sql.Driver) 
                  Class.forName("com.mysql.jdbc.Driver").newInstance()) ;
   }

   public Connection getConnection(String dataSourceName) {

       return DriverManager.getConnection("jdbc:apache:commons:dbcp:/"+dataSourceName);
   }

}

查询源::

String dsName = "pool" ;

ConnectionManager pool = new ConnectionManager();

java.sql.Connection conn = pool.getConnection(dsName);


conn.setAutoCommit(true);
if( m_sql.trim().toUpperCase().startsWith("INSERT") )
    pstmt = conn.prepareStatement(m_sql, Statement.RETURN_GENERATED_KEYS);
else
    pstmt = conn.prepareStatement(m_sql);

pstmt.setFetchSize(100);

boolean hasRS = pstmt.execute();

ResultSet resSet = null ;

if (hasRS) {
   resSet = pstmt.getResultSet();

} else {
   resSet = null;
   return ;
}

while(resSet.next()) {

     //resultSet processing ....
     ::::::
}


resSet.close() ;
pstmt.close() ;
conn.close() ;  --> don't return the connection to the pool and exhaust the connection...;;;

:::

poo.jocl ::这位于$ CLASSPATH

<object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
    <string value="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;traceProtocol=true&amp;characterEncoding=UTF-8"/>
    <string value="usera"/>
    <string value="passa"/>
</object>

<object class="org.apache.commons.pool.impl.GenericObjectPool">
    <object class="org.apache.commons.pool.PoolableObjectFactory" null="true" />
    <int value="100" /> <!-- maxActive -->
            <byte value="1" /> <!-- whenExhaustedAction -->
    <long value="10000" /> <!-- maxWait -->
    <int value="30" /> <!-- maxIdle -->
    <int value="3" />  <!-- minIdle -->
    <boolean value="true" /> <!-- testOnBorrow -->
    <boolean value="true" />  <!--testOnReturn -->
    <long value="600000" />  <!-- timeBetweenEvictionRunsMillis -->
    <int value="5" />  <!-- numTestsPerEvictionRun -->
    <long value="3600000" /> <!-- minEvictableIdleTimeMillis -->
    <boolean value="true" /> <!-- testWhileIdle -->
</object>

<object class="org.apache.commons.pool.KeyedObjectPoolFactory" null="true"/>    
<string null="true"/>   
<boolean value="false"/>
<boolean value="true"/>

1 个答案:

答案 0 :(得分:0)

由于注册了MySQL驱动程序,您可能没有使用Apache连接池。尝试删除它。从JDBC 4.0开始,您根本不需要注册MySQL驱动程序。事实上,由于所有的XML配置,我认为你根本不需要那个静态块。尝试删除它。