JythonでshowPoolContentsを一定時間おきに実行します
実行環境
下記環境を使用しています。
Jythonで、1分おきにデータソース PdprofDataSource に showPoolContents を実行
/tmp/test.py を作成
import time
# DataSource名
dataSourceName = "PdprofDataSource"
# 結果を保存するファイルのパス
outputFilePath = "/tmp/pool_contents.txt"
# DataSourceのオブジェクト名を取得
objectName = AdminControl.queryNames("WebSphere:name=" + dataSourceName + ",*")
# 無限ループで1分おきに実行
while True:
# プール内容の取得
result = AdminControl.invoke(objectName, "showPoolContents")
# 現在のタイムスタンプを取得
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
# 結果をファイルに追記
with open(outputFilePath, "a") as outputFile:
outputFile.write("Timestamp: " + timestamp + "\n") # タイムスタンプをファイルに書き込み
outputFile.write(result + "\n") # 結果をファイルに書き込み
outputFile.write("-----\n") # 区切りラインを追加
# 1分(60秒)待機
time.sleep(60)
実行結果
$ /opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/wsadmin.sh -lang jython -f /tmp/test.py
$ tail -f /tmp/pool_contents.txt
(中略)
Timestamp: 2025-07-25 01:05:07
PoolManager name:jdbc/derbyEmbedded
PoolManager object:1338453246
Total number of connections: 1 (max/min 10/0, reap/unused/aged 180/1800/0, connectiontimeout/purge 180/EntirePool)
(testConnection/inteval false/0, stuck timer/time/threshold 0/0/0, surge time/connections 0/-1)
(pool paused false, prePopulate alternate false, resourceFailBackEnabled true, isAlternateResourceEnabled false, disableDatasourceFailoverAlarm false, startFailBack false)
(isPartialResourceAdapterFailoverSupportEnabled false, isAlteranteResourcePoolManager false, resourceAvailabilityTestRetryInterval 10, currentInusePool null, currentMode 100, alternate jndiName null)
Shared Connection information (shared partitions 200)
No shared connections
Free Connection information (free distribution table/partitions 5/1)
(1)(0)MCWrapper id 25a9e339 Managed connection WSRdbManagedConnectionImpl@1a60b0d4 State:STATE_ACTIVE_FREE
Total number of connection in free pool: 1
UnShared Connection information
No unshared connections
-----
Timestamp: 2025-07-25 01:06:07
PoolManager name:jdbc/derbyEmbedded
PoolManager object:1338453246
Total number of connections: 10 (max/min 10/0, reap/unused/aged 180/1800/0, connectiontimeout/purge 180/EntirePool)
(testConnection/inteval false/0, stuck timer/time/threshold 0/0/0, surge time/connections 0/-1)
(pool paused false, prePopulate alternate false, resourceFailBackEnabled true, isAlternateResourceEnabled false, disableDatasourceFailoverAlarm false, startFailBack false)
(isPartialResourceAdapterFailoverSupportEnabled false, isAlteranteResourcePoolManager false, resourceAvailabilityTestRetryInterval 10, currentInusePool null, currentMode 100, alternate jndiName null)
Shared Connection information (shared partitions 200)
No shared connections
Free Connection information (free distribution table/partitions 5/1)
(1)(0)MCWrapper id 62950ebb Managed connection WSRdbManagedConnectionImpl@6adcc2d State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 8381cdab Managed connection WSRdbManagedConnectionImpl@5589f579 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 25a9e339 Managed connection WSRdbManagedConnectionImpl@1a60b0d4 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id d490dae2 Managed connection WSRdbManagedConnectionImpl@841a0dff State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 879922b9 Managed connection WSRdbManagedConnectionImpl@c267b992 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 71cf906e Managed connection WSRdbManagedConnectionImpl@7b23aac4 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 618bc39c Managed connection WSRdbManagedConnectionImpl@e0d1de4 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id e1721491 Managed connection WSRdbManagedConnectionImpl@1e23d89e State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id f13d2b3f Managed connection WSRdbManagedConnectionImpl@efe31f9d State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id cd194fc8 Managed connection WSRdbManagedConnectionImpl@1f575a08 State:STATE_ACTIVE_FREE
Total number of connection in free pool: 10
UnShared Connection information
No unshared connections
(以下省略)
補足として、使用中の接続のスタックトレースも出力するには、トレース仕様に ConnLeakLogic=all を設定します。
参考 How to troubleshoot J2CA0045E connection pooling problems
In Trace Specification, enter the following string:
ConnLeakLogic=all
Click OK.
ConnLeakLogic=all を設定している場合は、使用中の接続があると、その接続がallocateされたときのスタックトレースがshowPoolContentsに出力されます。
Timestamp: 2025-07-25 01:58:04
PoolManager name:jdbc/derbyEmbedded
PoolManager object:1338453246
Total number of connections: 9 (max/min 10/0, reap/unused/aged 180/1800/0, connectiontimeout/purge 180/EntirePool)
(testConnection/inteval false/0, stuck timer/time/threshold 0/0/0, surge time/connections 0/-1)
(pool paused false, prePopulate alternate false, resourceFailBackEnabled true, isAlternateResourceEnabled false, disableDatasourceFailoverAlarm false, startFailBack false)
(isPartialResourceAdapterFailoverSupportEnabled false, isAlteranteResourcePoolManager false, resourceAvailabilityTestRetryInterval 10, currentInusePool null, currentMode 100, alternate jndiName null)
Shared Connection information (shared partitions 200)
com.ibm.ws.LocalTransaction.LocalTranCoordImpl@2eee405e;RUNNING; MCWrapper id 897ef86e Managed connection WSRdbManagedConnectionImpl@8eb83dba State:STATE_TRAN_WRAPPER_INUSE Native Thread Id: 206 Thread Id: 000000ce Thread Name: WebContainer : 13 Connections being held 1 Used with transaction com.ibm.ws.LocalTransaction.LocalTranCoordImpl@2eee405e;RUNNING; Start time inuse Fri Jul 25 01:57:04 UTC 2025 Time inuse 59 (seconds) Start time same as last allocation time
Total number of connection in shared pool: 1
Free Connection information (free distribution table/partitions 5/1)
(1)(0)MCWrapper id 8381cdab Managed connection WSRdbManagedConnectionImpl@5589f579 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 25a9e339 Managed connection WSRdbManagedConnectionImpl@1a60b0d4 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id d490dae2 Managed connection WSRdbManagedConnectionImpl@841a0dff State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 879922b9 Managed connection WSRdbManagedConnectionImpl@c267b992 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 71cf906e Managed connection WSRdbManagedConnectionImpl@7b23aac4 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id 618bc39c Managed connection WSRdbManagedConnectionImpl@e0d1de4 State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id f13d2b3f Managed connection WSRdbManagedConnectionImpl@efe31f9d State:STATE_ACTIVE_FREE
(1)(0)MCWrapper id cd194fc8 Managed connection WSRdbManagedConnectionImpl@1f575a08 State:STATE_ACTIVE_FREE
Total number of connection in free pool: 8
UnShared Connection information
No unshared connections
Connection Leak Logic Information: (Note, applications using managed connections in this list may not be following the recommended getConnection(), use connection, close() connection programming model pattern)
1. Stack trace when the thread/transaction requested to allocate a connection from the pool
at com.ibm.ejs.j2c.ConnectionManager.allocateConnection(ConnectionManager.java:1334)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:646)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:613)
at com.ibm._jsp._sleep._jspService(_sleep.java:128)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1235)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:122)
at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:221)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1134)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:82)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:966)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:382)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:532)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:318)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:289)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1909)
Below connections share above stack trace:
MCWrapper id 897ef86e Managed connection WSRdbManagedConnectionImpl@8eb83dba State:STATE_TRAN_WRAPPER_INUSE Native Thread Id: 206 Thread Id: 000000ce Thread Name: WebContainer : 13 Connections being held 1 Used with transaction com.ibm.ws.LocalTransaction.LocalTranCoordImpl@2eee405e;RUNNING;
Start time inuse Fri Jul 25 01:57:04 UTC 2025 Time inuse 59 (seconds)
Start time same as last allocation time
なお、上記は、以下のJSPを実行中の showPoolContents の結果です。接続を取得して1分間sleepしています。このJSPはアプリに含まれておらず、作成が必要です。
$cat /opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/DefaultCell01/db_connections.ear/db.connections.war/sleep.jsp
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.util.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8" session="false" %>
<%
// データソース名
String dataSourceName = "java:/comp/env/jdbc/derbyEmbedded";
Connection conn = null;
try {
// JNDIからデータソースを取得
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(dataSourceName);
// 接続の確立
conn = ds.getConnection();
// 1分間スリープ
Thread.sleep(60000); // 60000ミリ秒 = 1分
out.println("1分間のスリープが完了しました。");
} catch (Exception e) {
out.println("エラー: " + e.getMessage());
} finally {
// 接続を閉じる
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
out.println("接続のクローズ中にエラーが発生しました: " + e.getMessage());
}
}
}
%>