天天看點

Weblogic常見故障常:JDBC Connection Pools

weblogic server中資料庫連接配接池是一個經常出問題的地方,總結一下出問題的原因和解決辦法。

一、資料庫連接配接洩漏

此類問題一般都是由于開發人員沒有正确關閉資料庫連接配接造成的。比如使用完connection後,沒有調用connection.close()方法。

1、診斷方法

在console中,找到connection pools tab 和diagnostics,設定以下屬性(不同版本可能略有差別)

enable connection leak profiling 啟用連接配接池洩漏的監控。

enable connection profiling 啟用連接配接池監控。

inactive connection timeout 100 表示100秒後強制回收無效連接配接。預設0,表示使用完才釋放回連接配接池。

無需重新開機,檢視server的log,查找“a jdbc pool connection leak was detected”,如果有,看看是哪個類引起的。下面是一個資料庫連接配接洩漏的例子:

a jdbc pool connection leak was detected.

a connection leak occurs when a connection obtained from the pool was not closed explicitly by 

calling close() and then was disposed by the garbage collector and returned to the connection pool.

the following stack trace at create shows where the leaked connection was created.

stack trace at connection create:

at weblogic.jdbc.wrapper.jtaconnection.init(jtaconnection.java:90)

at weblogic.jdbc.jta.datasource.getconnection(datasource.java:468)

at weblogic.jdbc.jta.datasource.connect(datasource.java:410)

at weblogic.jdbc.common.internal.rmidatasource.getconnection(rmidatasource.java:344)

at troubleshooting.servlets.jdbcconnections.service(jdbcconnections.java:97)

at javax.servlet.http.httpservlet.service(httpservlet.java:853)

at weblogic.servlet.internal.servletstubimpl$servletinvocationaction.run(servletstubimpl.java:1077)

at weblogic.servlet.internal.servletstubimpl.invokeservlet(servletstubimpl.java:465)

at weblogic.servlet.internal.servletstubimpl.invokeservlet(servletstubimpl.java:348)

at weblogic.servlet.internal.webappservletcontext$servletinvocationaction.run(webappservletcontext.java:7047)

at weblogic.security.acl.internal.authenticatedsubject.doas(authenticatedsubject.java:321)

at weblogic.security.service.securitymanager.runas(securitymanager.java:121)

at weblogic.servlet.internal.webappservletcontext.invokeservlet(webappservletcontext.java:3902)

at weblogic.servlet.internal.servletrequestimpl.execute(servletrequestimpl.java:2773)

at weblogic.kernel.executethread.execute(executethread.java:224)

at weblogic.kernel.executethread.run(executethread.java:183)

問題解決後,把三個屬性設定回先前的值。

2、解決方法

正确的關閉資料庫連接配接。具體代碼如下:

connection conn = null;

resultset rs = null;

preparedstatement pss = null;

try {

conn = datasource.getconnection(userid,password);

pss = conn.preparestatement("select saveserialzeddata from session.pingsession3data where 

sessionkey = ?");

pss.setstring(1,sessionkey);

rs = pss.executequery();

pss.close();

}

catch (throwable t) {} 

finally {

try 

{

if (conn != null) conn.close();

catch (exception e){}

二、資料庫連接配接不夠用

導緻資料庫連接配接不夠用的原因主要有:

①某些程式占用connection時間過長,如果多個使用者同時使用這些程式,則會導緻連接配接不夠用。

②線程死鎖,無法釋放connection。

①監控參數:waiting for connection high count

[domain_name]-> enviroment -> servers -> [server] -> monitoring -> jdbc檢視參數:waiting for connection high count

如果沒有此參數,手工添加進來,該參數表示在沒有可用連接配接的情況下,應用程式等待連接配接的最大個數。調整後的連接配接池最大值 = 調整前的連接配接池最大值 + waiting for connection high count。一般來說,資料庫連接配接池的大小與最佳并發使用者數相當。

②在server log中,明确抛出下列異常:

java.sql.sqlexception: internal error: cannot obtain xaconnection 

weblogic.common.resourcepool.resourcelimitexception: no resources currently available in pool 

bankconnectionpool to allocate to applications, please increase the size of the pool and retry..

at weblogic.jdbc.jta.datasource.refreshxaconnandenlist(datasource.java:1493)

at weblogic.jdbc.jta.datasource.getconnection(datasource.java:455)

at troubleshooting.servlets.jdbcconnections.service(jdbcconnections.java:80)

如果此時觀察connection的監控,會發現所有connection 都是active,而且還有大量請求等待connection。

①提高maximum capacity數量,該值一般略大于峰值情況下的資料庫連接配接數。services > jdbc > connection pools > bankconnectionpool > configuration > connections

②重點檢查synchronize代碼段和涉及資料庫鎖的代碼。如果有必要,可以檢視thread dump,看看線程在忙什麼和等什麼。

三、資料庫連接配接使用逾時

此類問題一般是由于某些資料庫操作時間比較長,超過了inactive connection timeout的設定。

在server log中,明确有下列提示,并且在提示後抛出應用異常:

forcibly releasing inactive resource "weblogic.jdbc.common.internal.connectionenv@132967d" back into the pool bankconnectionpool".這裡無法列出應用異常,因為每個應用都不一樣,不過很有可能會抛出空指針異常,因為connection被強制放回池中了,繼續使用一個空對象會抛出該異常。

在進階參數中,提高inactive connection timeout數量。

services > jdbc > connection pools > bankconnectionpool > configuration > connections

四、事務逾時

此類問題一般是由于某些資料庫操作時間比較長,超過了jta timeout seconds的設定。

在server log中,明确抛出異常:

weblogic.transaction.internal.timedoutexception: transaction timed out after 300 seconds

提高services > jta configuration > timeout seconds數量。

注意這個參數應該小于inactive connection timeout的值,因為事務必須在連接配接逾時前完成。如果想分析究竟是哪些sql語句導緻事務逾時,可以打開日志adminserver > logging > jdbc,選中enable jdbc logging,并設定jdbc log file name。

原帖位址:http://maping930883.blogspot.com/2009/03/wls040jdbc-connection.html