0

DBCP 连接池泄漏。

当程序返回使用的连接时,连接资源没有返回到池中,被耗尽!!!

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

请告诉我如何解决这个问题!!!

图书馆 :

commons-dbcp-1.4.jar commons-pool-1.6.jar mysql-connector-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"/>

4

1 回答 1

0

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

于 2012-06-11T08:15:26.033 回答