[Version 5.0.1 and later]Example: Test a connection using testConnection(ConfigID)

You can pass the configuration ID of a configured data source, rather than the properties of the data source. This program uses JMX to connect to a running server and invoke the testConnection method on the DataSourceCfgHelper MBean.

The acronym ND in a comment line indicates that the following code applies to WebSphere Application Server Network Deployment. The word Base in a comment line indicates that the following code applies to WebSphere Application Server.

import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.RuntimeMBeanException;
import javax.management.RuntimeOperationsException;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;

/**
 * Tests a connection to a DataSource when WebSphere
 * Security is disabled
 *
 * To run this example, the following must be done:
 *
 * 1)  Set the WAS_HOME environment variable to the location of
 *     your WebSphere Application Server for z/OS Configuration
 *     directory
 *
 *     Example:  export WAS_HOME=/WebSphereV5R1M0/AppServer
 *
 * 2)  Set the following environment variables:
 *
 *     export WAS_LIB=$WAS_HOME/lib
 *     export WAS_CLASSPATH=[DIRECTORY_CONTAINING_THIS_FILE]
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/jmxc.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/wsexception.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/admin.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/wasjmx.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_HOME/java/jre/lib/ext/mail.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/ibmjlog.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/utils.jar
 *
 * 3)  Execute the following commands:
 *
 *     javac -classpath $WAS_CLASSPATH TestDSGUI.java
 *     java -classpath $WAS_CLASSPATH TestDSGUI
 */
public class TestDSGUI {

    //Use port 8880 for Base installation or port 8879 for ND installation
    String port = "8880";
    String host = "localhost";
    final static boolean verbose = true;

    // eg a configuration ID for 5.1 DataSource declared at the node level
    private static final String resURI = "cells/SY1/nodes/SY1:resources.xml#DataSource_1080685343915";

    // eg a configuration ID for 5.1 DataSource declared at the server level
    // private static final String resURI = "cells/SY1/nodes/SY1/servers/server1/resources.xml#DataSource_6";


    public static void main(String[] args) {
        TestDSGUI cds = new TestDSGUI();
        cds.run(args);
    }

    /**
     * This method tests the ResourceMbean.
     */
    public void run(String[] args) {
        try {

            System.out.println("Connecting to the application server.......");

            /*************************************************************************/
            /**    Initialize the AdminClient                                        */
            /*************************************************************************/

            Properties adminProps = new Properties();
            adminProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
            adminProps.setProperty(AdminClient.CONNECTOR_HOST, host);
            adminProps.setProperty(AdminClient.CONNECTOR_PORT, port);
            AdminClient adminClient = null;
            try {
                adminClient = AdminClientFactory.createAdminClient(adminProps);
            } catch (com.ibm.websphere.management.exception.ConnectorException ce) {
                System.out.println("NLS: Cannot make a connection to the application server\n");
                ce.printStackTrace();
                System.exit(1);
            }

            /*************************************************************************/
            /**    Locate the Mbean                                                  */
            /*************************************************************************/

            ObjectName handle = null;
            try {
                // Send in a locator string
                ObjectName queryName = new ObjectName("WebSphere:type=DataSourceCfgHelper,*");
                Set s = adminClient.queryNames(queryName, null);
                Iterator iter = s.iterator();
                while (iter.hasNext()) {
                    // use the first MBean that is found
                    handle = (ObjectName) iter.next();
                    System.out.println("Found this ->" + handle);
                }
                if (handle == null) {
                    System.out.println("NLS: Did not find this MBean>>" + queryName);
                    System.exit(1);
                }
            } catch (MalformedObjectNameException mone) {
                System.out.println("Check the program variable queryName" + mone);
            } catch (com.ibm.websphere.management.exception.ConnectorException ce) {
                System.out.println("Cannot connect to the application server" + ce);
            }

            /*************************************************************************/
            /**           Build parameters to pass to Mbean                          */
            /*************************************************************************/

            String[] signature = { "java.lang.String" };
            Object[] params = { resURI };
            Object result = null;

            if (verbose) {
                System.out.println("\nTesting connection to the database using " + handle);
            }

            try {
                /*************************************************************************/
                /**  Start to test the connection to the database                        */
                /*************************************************************************/
                result = adminClient.invoke(handle, "testConnection", params, signature);
            } catch (MBeanException mbe) {
                // ****** all user exceptions come in here
                if (verbose) {
                    Exception ex = mbe.getTargetException(); // this is the real exception from the Mbean
                    System.out.println("\nNLS:Mbean Exception was received contains " + ex);
                    ex.printStackTrace();
                    System.exit(1);
                }
            } catch (InstanceNotFoundException infe) {
                System.out.println("Cannot find " + infe);
            } catch (RuntimeMBeanException rme) {
                Exception ex = rme.getTargetException();
                ex.printStackTrace(System.out);
                throw ex;
            } catch (Exception ex) {
                System.out.println("\nUnexpected Exception occurred: " + ex);
                ex.printStackTrace();
            }

            /*************************************************************************/
            /**  Process the result.  The result will be the number of warnings      */
            /**  issued.  A result of 0 indicates a successful connection with       */
            /**  no warnings.                                                        */
            /*************************************************************************/

            //A result of 0 indicates a successful connection with no warnings.
            System.out.println("Result (number of warnings) = " + result);

        } catch (RuntimeOperationsException roe) {
            Exception ex = roe.getTargetException();
            ex.printStackTrace(System.out);
        } catch (Exception ex) {
            System.out.println("General exception occurred");
            ex.printStackTrace(System.out);
        }
    }
}

Related reference
Data access : Resources for learning



Searchable topic ID:   testjsap1
Last updated: Jun 21, 2007 9:56:50 PM CDT    WebSphere Application Server for z/OS, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.websphere.zseries.doc/info/zseries/ae/rdat_testjsap1.html

Library | Support | Terms of Use | Feedback