+

Search Tips   |   Advanced Search

Example: Testing a connection using testConnection(ConfigID)


The following sample code creates a data source instance and an associated connection instance, and tests them to ensure database connectivity.

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 WAS ND. The word Base in a comment line indicates that the following code applies to WAS.

/**
 * Description
 * Resource adapter test program to make sure that the MBean interfaces work.
 * Following interfaces are tested
 * 
 *  ---  testConnection()
 * 
 * 
 * We need following to run
 * C:\src>java -Djava.ext.dirs=C:\WebSphere\AppServer\lib;C:\WebSphere\AppServer\java\jre\lib\ext testDSGUI
 * must include jre for log.jar and mail.jar, else get class not found exception
 * 
 * 
 */
 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;
 import com.ibm.ws.rsadapter.exceptions.DataStoreAdapterException;
 public class testDSGUI {


//Use port 8880 for a Base installation or port 8879 for ND installation String port = "8880";

// String port = "8879";
 String host = "localhost";
 final static boolean verbose = true;


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


// eg a 4.0 DataSource declared at the node level for Base

// private static final String resURI = "cells/cat/nodes/cat:resources.xml#WAS40DataSource_1";


// eg Apache Derby DataSource declared at the server level for Base

//private static final String resURI = "cells/cat/nodes/cat/servers/server1/resources.xml#DataSource_6";


// eg node level DataSource for ND

//private static final String resURI = "cells/catNetwork/nodes/cat:resources.xml#DataSource_1";


// eg server level DataSource for ND

//private static final String resURI = "cells/catNetwork/nodes/cat/servers/server1:resources.xml#DataSource_4";


// eg cell level DataSource for ND

//private static final String resURI = "cells/catNetwork:resources.xml#DataSource_1";

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

/**
 * This method tests the ResourceMbean.
 * 
 * @param args
 * @exception Exception
 */
 public void run(String[] args) {

  try {

  System.out.println("Connecting to the appserver.......");

  /*************************************************************************/
  /** 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 appserver\n");
   ce.printStackTrace();
   System.exit(1);
  }

    /*************************************************************************/
    /** Locate the Mbean              */
    /*************************************************************************/
  ObjectName handle = null;
  try {
    
// Send in a locator string 
    
// eg for a Base installation this is enough
    ObjectName queryName = new ObjectName("WebSphere:type=DataSourceCfgHelper,*");

    
// for ND specify which node/process you would like to test from
    
// eg run in the server

//ND: ObjectName queryName = new OjectName
  ("WebSphere:cell=catNetwork,node=cat,process=server1,type=DataSourceCfgHelper,*");
    
// eg run in the node agent

//ND: ObjectName queryName = new ObjectName
  ("WebSphere:cell=catNetwork,node=cat,process=nodeagent,type=DataSourceCfgHelper,*");

//ND: eg run in the Deployment Manager

//ND: ObjectName 
queryName = new ObjectName
   ("WebSphere:cell=catNetwork,node=catManager,process=dmgr,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 appserver" + 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= " + 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


Data access: Links