WAS v8.5 > Administer applications and their environment > Welcome to administering Data access resources > Administer data access applications > Configure a JDBC provider and data source

Test connection service

The test connection service for validating data source configurations...

  1. Uses a administrative consoletestConnection operation to instantiate the data source configuration.
  2. Gets a connection
  3. Immediately closes the connection

After creating new data sources under cluster scope, restart nodes using stopNode.sh/startNode.sh.


Data source scope

If associate data sources with WebSphere variables, verify they are configured correctly. A "variable cannot be found exception" results from attempted use of a data source that is invoked through an incorrectly defined variable.


Activating the test connection service

There are three ways to activate the test connection service: through the dmgr console, wsadmin, or a Java stand-alone program. Each process invokes the same methods on the same MBean.

Administrative console

WAS allows you to test a connection from the dmgr console by simply pushing a button: the Data source collection, Data source settings, Version 4 data source collection, and Version 4 data source settings pages all have Test Connection buttons. After you define and save a data source, we can click this button to ensure the parameters in the data source definition are correct. On the collection page, we can select several data sources and test them all at once. Note there are certain conditions that must be met first. For more information, see the topic, Testing a connection with the dmgr console.

The following exception occurs when we click Test Connection to connect a Sybase data source from the dmgr console.

Test connection failed for data source isagent on server server1 at node 
svtaix24Node01 with the following exception: java.lang.Exception: 
java.sql.SQLException: JZ006: Caught IOException: java.net.ConnectException: A 
remote host refused an attempted connect operation.DSRA0010E: SQL State = JZ006, 
Error Code = 0
This exception occurs when the Sybase data source port number is not matched to the port configured in Sybase server. The default port number is 5000. Check the port number of your Sybase server in the interfaces file under /<sybase install directory>.

WsAdmin tool

The wsadmin tool provides a scripting interface to a full range of WAS administration activities. Because the Test Connection functionality is implemented as a method on an MBean, and wsadmin can invoke MBean methods, wsadmin can be utilized to test connections to data sources. You have two options for testing a data source connection through wsadmin:

The AdminControl object of wsadmin has a testConnection operation that tests the configuration properties of a data source object. For information, see the topic, Testing a connection using wsadmin.

We can also test a connection by invoking the MBean operation. Use the example in the topic, Example: Testing data source connection using wsadmin, as a guide for this technique.

Java stand-alone program

Finally, we can test a connection by executing the testConnection method on the DataSourceCfgHelper MBean. This method allows you to pass the configuration ID of the configured data source. The Java program connects to a running JMX server to access the MBean. In a base installation of Application Server, you connect to the JMX server running in the application server, usually on port 8880.

The return value from this invocation is either 0, a positive number, or an exception. 0 indicates the operation completed successfully, with no warnings. A positive number indicates the operation completed successfully, with the number of warnings. An exception indicates the test of the connection failed.

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

/**
 * Description
 * Resource adapter test program to verify 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("Connect 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 
                // 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   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 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=" + 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);
  }
 }}

Ensure that you run the test connection service at the same level as an existing data source. For example, do not run the test connection service at the node level if your data source is configured on the server level. If the test connection service and the data source configuration do not exist on the same level, a failure to load exception might result. In this situation, source the db2profile script on the machine and ensure the environment contains pointers to the DB2 native libraries. The db2profile script exists in the root directory of the DB2 user ID.


Related concepts:

WebSphere variables


Related


Configure WebSphere variables
Testing a connection with the dmgr console
Testing a connection using wsadmin
Testing data source connections using wsadmin.sh


Reference:

Administrative console scope settings


+

Search Tips   |   Advanced Search