0
DBCP連接池泄漏。DBCP連接池泄漏:Connection.close()不會將連接返回到池並且連接耗盡
當程序返回使用的連接時,連接資源不會返回到池中的 並已耗盡!
因此,在maxActive時間Connection.close()中,池在getConnection()時掛起。
請讓我知道如何解決這個問題!
庫:
公地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&traceProtocol=true&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"/>
是的,你是對的。 MySQL驅動程序不需要註冊!但是,除去這個,該程序也掛起... ;;;; –
我不知道爲什麼程序直接使用MySQL驅動程序而不是Apache連接池,儘管刪除了註冊MySQL驅動程序。 –
Ooooops !!!。我在我的項目源中犯了一個錯誤... –