jdbc Archive

Sample JMX Code to retrieve JDBC Connection Pool Parameter

import java.io.IOException;
import java.util.*;

import javax.management.*;
import javax.management.remote.*;

import javax.naming.Context;

import weblogic.jndi.Environment;
import weblogic.management.*;

public class JDBCConnectionPoolProperties {

	static MBeanServerConnection connection;
	static JMXConnector connector;
	static ObjectName service;

	public static void main(String[] args) {

		try {

			JMXServiceURL serviceURL = new JMXServiceURL("t3", "localhost",
					80,
					"/jndi/weblogic.management.mbeanservers.domainruntime");
			service = new ObjectName(
					"com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");

			Hashtable h = new Hashtable();
			h.put(Context.SECURITY_PRINCIPAL, "weblogic");
			h.put(Context.SECURITY_CREDENTIALS, "weblogic123");
			h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
					"weblogic.management.remote");
			connector = JMXConnectorFactory.connect(serviceURL, h);
			connection = connector.getMBeanServerConnection();

			JDBCConnectionPoolProperties jdbcConnPP = new JDBCConnectionPoolProperties();
			jdbcConnPP.getJDBCRuntime();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getJDBCRuntime() throws Exception {

		ObjectName[] serverRuntime = (ObjectName[]) connection.getAttribute(service,
				"ServerRuntimes");

		int length = (int) serverRuntime.length;

		for (int i = 0; i < length; i++) {
			String name = (String) connection.getAttribute(serverRuntime[i], "Name");
			ObjectName[] jdbcRuntime = (ObjectName[]) connection.getAttribute(new ObjectName("com.bea:Name="+name+",ServerRuntime="+name+",Location="+name+",Type=JDBCServiceRuntime"),"JDBCDataSourceRuntimeMBeans");
			int jdbcLength = (int) jdbcRuntime.length;

			for (int x = 0; x < jdbcLength; x++) {
				System.out.println("\nServer Name   " + name
						+ "   Datasource Name: "
						+ (String) connection.getAttribute(jdbcRuntime[x], "Name"));
				System.out.println("ActiveConnectionsCurrentCount    : "
						+ connection.getAttribute(jdbcRuntime[x],
								"ActiveConnectionsCurrentCount"));
				System.out.println("ActiveConnectionsAverageCount    : "
						+ connection.getAttribute(jdbcRuntime[x],
								"ActiveConnectionsAverageCount"));
				System.out.println("ActiveConnectionsAverageCount    : "
						+ connection.getAttribute(jdbcRuntime[x],
								"ActiveConnectionsAverageCount"));
				System.out.println("ConnectionsTotalCount            : "
						+ connection.getAttribute(jdbcRuntime[x],
								"ConnectionsTotalCount"));
				System.out.println("CurrCapacity                    : "
						+ connection.getAttribute(jdbcRuntime[x], "CurrCapacity"));
				System.out.println("CurrCapacityHighCount            : "
						+ connection.getAttribute(jdbcRuntime[x],
								"CurrCapacityHighCount"));
				System.out.println("HighestNumAvailable              : "
						+ connection.getAttribute(jdbcRuntime[x],
								"HighestNumAvailable"));
				System.out.println("HighestNumAvailable              : "
						+ connection.getAttribute(jdbcRuntime[x],
								"HighestNumAvailable"));
				System.out.println("LeakedConnectionCount            : "
						+ connection.getAttribute(jdbcRuntime[x],
								"LeakedConnectionCount"));
				System.out.println("WaitSecondsHighCount             : "
						+ connection.getAttribute(jdbcRuntime[x],
								"WaitSecondsHighCount"));
				System.out.println("WaitingForConnectionCurrentCount: "
						+ connection.getAttribute(jdbcRuntime[x],
								"WaitingForConnectionCurrentCount"));
				System.out.println("WaitingForConnectionFailureTotal: "
						+ connection.getAttribute(jdbcRuntime[x],
								"WaitingForConnectionFailureTotal"));
				System.out.println("WaitingForConnectionTotal        : "
						+ connection.getAttribute(jdbcRuntime[x],
								"WaitingForConnectionTotal"));
				System.out.println("WaitingForConnectionHighCount    : "
						+ connection.getAttribute(jdbcRuntime[x],
								"WaitingForConnectionHighCount"));
				String poolName = (String) connection.getAttribute(jdbcRuntime[x],
						"Name");
				ObjectName jdbcSystemResource = new ObjectName("com.bea:Name="
						+ poolName + ",Type=JDBCSystemResource");
				ObjectName jdbcDataSourceBean = (ObjectName) getObjectName(
						jdbcSystemResource, "JDBCResource");

				System.out.println("\n Connection Pool Parameters");
				ObjectName jdbcConnectionPoolParams = (ObjectName) getObjectName(
						jdbcDataSourceBean, "JDBCConnectionPoolParams");
				Integer CapacityIncrement = (Integer) getObjectName(
						jdbcConnectionPoolParams, "CapacityIncrement");
				System.out
						.println(" CapacityIncrement                      :"
								+ CapacityIncrement);

				Integer ConnectionCreationRetryFrequencySeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams,
						"ConnectionCreationRetryFrequencySeconds");
				System.out
						.println(" ConnectionCreationRetryFrequencySeconds:"
								+ ConnectionCreationRetryFrequencySeconds);

				Integer ConnectionReserveTimeoutSeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams,
						"ConnectionReserveTimeoutSeconds");
				System.out
						.println(" ConnectionReserveTimeoutSeconds        :"
								+ ConnectionReserveTimeoutSeconds);

				Integer HighestNumWaiters = (Integer) getObjectName(
						jdbcConnectionPoolParams, "HighestNumWaiters");
				System.out.println(" HighestNumWaiters                     :"
						+ HighestNumWaiters);

				Integer InactiveConnectionTimeoutSeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams,
						"InactiveConnectionTimeoutSeconds");
				System.out.println(" InactiveConnectionTimeoutSeconds      :"
						+ CapacityIncrement);

				Integer InitialCapacity = (Integer) getObjectName(
						jdbcConnectionPoolParams, "InitialCapacity");
				System.out.println(" InitialCapacity                       :"
						+ InitialCapacity);

				Integer JDBCXADebugLevel = (Integer) getObjectName(
						jdbcConnectionPoolParams, "JDBCXADebugLevel");
				System.out.println(" JDBCXADebugLevel                      :"
						+ JDBCXADebugLevel);

				Integer LoginDelaySeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams, "LoginDelaySeconds");
				System.out.println(" LoginDelaySeconds                     :"
						+ LoginDelaySeconds);

				Integer MaxCapacity = (Integer) getObjectName(
						jdbcConnectionPoolParams, "MaxCapacity");
				System.out.println(" MaxCapacity                           :"
						+ MaxCapacity);

				Integer ProfileHarvestFrequencySeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams,
						"ProfileHarvestFrequencySeconds");
				System.out.println(" ProfileHarvestFrequencySeconds        :"
						+ ProfileHarvestFrequencySeconds);

				Integer ProfileType = (Integer) getObjectName(
						jdbcConnectionPoolParams, "ProfileType");
				System.out.println(" ProfileType                           :"
						+ ProfileType);

				Integer SecondsToTrustAnIdlePoolConnection = (Integer) getObjectName(
						jdbcConnectionPoolParams,
						"SecondsToTrustAnIdlePoolConnection");
				System.out.println(" SecondsToTrustAnIdlePoolConnection    :"
						+ SecondsToTrustAnIdlePoolConnection);

				Integer ShrinkFrequencySeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams, "ShrinkFrequencySeconds");
				System.out.println(" ShrinkFrequencySeconds                :"
						+ ShrinkFrequencySeconds);

				Integer StatementTimeout = (Integer) getObjectName(
						jdbcConnectionPoolParams, "StatementTimeout");
				System.out.println(" StatementTimeout                      :"
						+ StatementTimeout);

				Integer TestFrequencySeconds = (Integer) getObjectName(
						jdbcConnectionPoolParams, "TestFrequencySeconds");
				System.out.println(" TestFrequencySeconds                  :"
						+ TestFrequencySeconds);

				String TestTableName = (String) getObjectName(
						jdbcConnectionPoolParams, "TestTableName");
				System.out.println(" TestTableName                         :"
						+ TestTableName);

				String StatementCacheType = (String) getObjectName(
						jdbcConnectionPoolParams, "StatementCacheType");
				System.out.println(" StatementCacheType                    :"
						+ StatementCacheType);

				String InitSql = (String) getObjectName(
						jdbcConnectionPoolParams, "InitSql");
				System.out.println(" InitSql                               :"
						+ InitSql);

				Boolean CredentialMappingEnabled = (Boolean) getObjectName(
						jdbcConnectionPoolParams, "CredentialMappingEnabled");
				System.out.println(" CredentialMappingEnabled              :"
						+ CredentialMappingEnabled);

				Boolean IdentityBasedConnectionPoolingEnabled = (Boolean) getObjectName(
						jdbcConnectionPoolParams,
						"IdentityBasedConnectionPoolingEnabled");
				System.out.println(" IdentityBasedConnectionPoolingEnabled :"
						+ IdentityBasedConnectionPoolingEnabled);

				Boolean IgnoreInUseConnectionsEnabled = (Boolean) getObjectName(
						jdbcConnectionPoolParams,
						"IgnoreInUseConnectionsEnabled");
				System.out.println(" IgnoreInUseConnectionsEnabled         :"
						+ IgnoreInUseConnectionsEnabled);

				Boolean PinnedToThread = (Boolean) getObjectName(
						jdbcConnectionPoolParams, "PinnedToThread");
				System.out.println(" PinnedToThread                        :"
						+ PinnedToThread);

				Boolean RemoveInfectedConnections = (Boolean) getObjectName(
						jdbcConnectionPoolParams, "RemoveInfectedConnections");
				System.out.println(" RemoveInfectedConnections             :"
						+ RemoveInfectedConnections);

				ObjectName jdbcDriverParams = (ObjectName) getObjectName(
						jdbcDataSourceBean, "JDBCDriverParams");
				ObjectName jdbcProperties = (ObjectName) getObjectName(
						jdbcDriverParams, "Properties");
				ObjectName[] jdbcPropertyBeans = (ObjectName[]) getObjectName(
						jdbcProperties, "Properties");

				for (int j = 0; j < jdbcPropertyBeans.length; j++) {
					ObjectName jdbcPropertyBean = null;
					jdbcPropertyBean = jdbcPropertyBeans[j];
					String jdbcPropertyName = (String) getObjectName(
							jdbcPropertyBean, "Name");
					String jdbcPropertyValue = (String) getObjectName(
							jdbcPropertyBean, "Value");
					System.out.println("\n Name & Value  "+ jdbcPropertyName + " " + jdbcPropertyValue);
				}

			}
		}
	}

	private Object getObjectName(ObjectName objectName, String attributeName)
			throws AttributeNotFoundException, InstanceNotFoundException,
			MBeanException, ReflectionException, IOException {
		return connection.getAttribute(objectName, attributeName);
	}
}

OUTPUT

Server Name AdminServer Datasource Name: PegaRULES
ActiveConnectionsCurrentCount : 0
ActiveConnectionsAverageCount : 0
ActiveConnectionsAverageCount : 0
ConnectionsTotalCount : 10
CurrCapacity : 2
CurrCapacityHighCount : 4
HighestNumAvailable : 5
HighestNumAvailable : 5
LeakedConnectionCount : 0
WaitSecondsHighCount : 0
WaitingForConnectionCurrentCount: 0
WaitingForConnectionFailureTotal: 0
WaitingForConnectionTotal : 0
WaitingForConnectionHighCount : 0

Connection Pool Parameters
CapacityIncrement :1
ConnectionCreationRetryFrequencySeconds:0
ConnectionReserveTimeoutSeconds :10
HighestNumWaiters :2147483647
InactiveConnectionTimeoutSeconds :1
InitialCapacity :1
JDBCXADebugLevel :10
LoginDelaySeconds :0
MaxCapacity :15
ProfileHarvestFrequencySeconds :300
ProfileType :0
SecondsToTrustAnIdlePoolConnection :10
ShrinkFrequencySeconds :900
StatementTimeout :-1
TestFrequencySeconds :120
TestTableName :SQL SELECT 1 FROM DUAL
StatementCacheType :LRU
InitSql :null
CredentialMappingEnabled :false
IdentityBasedConnectionPoolingEnabled :false
IgnoreInUseConnectionsEnabled :true
PinnedToThread :false
RemoveInfectedConnections :true

Name & Value user SYSTEM

Monitoring JDBC Datasource Runtime Attributes using WLST

Step 1 :- Set the environment

C:\bea103\user_projects\domains\prpc612\bin>setDomainEnv.bat

Step 2:- Connect to server and browse to the JDBCDataSourceRuntime MBean

C:\bea103\user_projects\domains\prpc612>java weblogic.WLST

Initializing WebLogic Scripting Tool (WLST) …

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

wls:/offline> connect()
Please enter your username [weblogic] :
Please enter your password [weblogic] :
Please enter your server URL [t3://localhost:7001] :t3://localhost:7003
Connecting to t3://localhost:7003 with userid weblogic …
Successfully connected to managed Server ‘MS0’ that belongs to domain ‘prpc612’.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

wls:/prpc612/serverConfig> serverRuntime()
Location changed to serverRuntime tree. This is a read-only tree with ServerRunt
imeMBean as the root.
For more help, use help(serverRuntime)
wls:/prpc612/serverRuntime> cd(‘JDBCServiceRuntime’)
wls:/prpc612/serverRuntime/JDBCServiceRuntime> cd(‘MS0’)
wls:/prpc612/serverRuntime/JDBCServiceRuntime/MS0> cd(‘JDBCDataSourceRuntimeMBeans’)
wls:/prpc612/serverRuntime/JDBCServiceRuntime/MS0/JDBCDataSourceRuntimeMBeans> cd(‘MyDs’)
gaRules> cmo.getActiveConnectionsCurrentCount()
1

The other JDBC Datasource Runtime attributes that can be monitored are listed below.

wls:/prpc612/serverRuntime/JDBCServiceRuntime/MS0/JDBCDataSourceRuntimeMBeans/PegaRules> ls()
dr– JDBCDriverRuntime
dr– LastTask
dr– WorkManagerRuntimes

-r– ActiveConnectionsAverageCount 1
-r– ActiveConnectionsCurrentCount 0
-r– ActiveConnectionsHighCount 5
-r– ConnectionDelayTime 627
-r– ConnectionsTotalCount 5
-r– CurrCapacity 5
-r– CurrCapacityHighCount 4
-r– DeploymentState 2
-r– Enabled true
-r– FailedReserveRequestCount 0
-r– FailuresToReconnectCount 0
-r– HighestNumAvailable 5
-r– HighestNumUnavailable 0
-r– LastTask null
-r– LeakedConnectionCount 0
-r– ModuleId PegaRules
-r– Name PegaRules
-r– NumAvailable 5
-r– NumUnavailable 0
-r– PrepStmtCacheAccessCount 11852
-r– PrepStmtCacheAddCount 420
-r– PrepStmtCacheCurrentSize 50
-r– PrepStmtCacheDeleteCount 370
-r– PrepStmtCacheHitCount 11432
-r– PrepStmtCacheMissCount 420
-r– Properties {serverName=localhost, SID=prpc612, portNumber=1521, user=SYSTEM}
-r– ReserveRequestCount 11844
-r– State Running
-r– Type JDBCDataSourceRuntime
-r– VersionJDBCDriver oracle.jdbc.OracleDriver
-r– WaitSecondsHighCount 0
-r– WaitingForConnectionCurrentCount 0
-r– WaitingForConnectionFailureTotal 0
-r– WaitingForConnectionHighCount 0
-r– WaitingForConnectionSuccessTotal 4
-r– WaitingForConnectionTotal 0
-r– WorkManagerRuntimes null

-r-x clearStatementCache Void :
-r-x dumpPool Void :
-r-x dumpPoolProfile Void :
-r-x forceShutdown Void :
-r-x forceSuspend Void :
-r-x poolExists Boolean : String(name)
-r-x preDeregister Void :
-r-x reset Void :
-r-x resume Void :
-r-x shrink Void :
-r-x shutdown Void :
-r-x start Void :
-r-x suspend Void :
-r-x testPool String :