テスト接続サービス

WebSphere® Application Server では、データ・ソース構成の検証を行うための、テスト接続サービスが提供されています。 testConnection 命令によって、データ・ソース構成をインスタンス化し、接続を取得して、すぐに接続を閉じます。

データ・ソースと WebSphere 変数を関連付ける場合は、『WebSphere 変数の作成、編集、および削除』トピックを参照して、これらが正しく構成されていることを確認してください。 定義に誤りのある変数を介して呼び出されたデータ・ソースを使用しようとすると、 variable cannot be found 例外が発生します。

テスト接続サービスの活動化

テスト接続サービスを活動化するには、管理コンソール、wsadmin ツール、または Java™ スタンドアロン・プログラムを使用する 3 つの方法があります。各プロセスとも、同じ MBean 上で同じメソッドが起動します。

管理コンソール

WebSphere Application Server では、管理コンソールからボタン操作のみで接続をテストできます。「データ・ソース・コレクション (Data source collection) 」、「データ・ソース設定 (Data source settings) 」、「バージョン 4 データ・ソース・コレクション (Version 4 data source collection) 」、および「バージョン 4 データ・ソース設定 (Version 4 data source settings) 」のすべてのページに「テスト接続 (Test Connection) 」ボタンがあります。 データ・ソースを定義および保存したら、このボタンをクリックして、データ・ソース定義のパラメーターが正しいことを確認できます。 コレクション・ページでは、複数のデータ・ソースを選択し、 それらをすべて一度にテストできます。 最初に一定の条件が満たされていなければならないことに注意してください。 詳しくは、『管理コンソールを使用した接続のテスト』トピックを参照してください。

注: 「テスト接続」をクリックして、 管理コンソールから Sybase データ・ソースに接続すると、 次の例外が発生します。
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
Sybase データ・ソースのポート番号が、Sybase サーバーで 構成されたポートと一致しない場合に、この例外が発生します。 デフォルトのポート番号は 5000 です。 /<sybase install directory> の下にある インターフェース・ファイルで、 ご使用の Sybase サーバーのポート番号を確認してください。

WsAdmin ツール

wsadmin ツールは、すべての WebSphere Application Server 管理アクティビティーに対してスクリプト・インターフェースを提供します。 このテスト接続機能は、MBean のメソッドとして実装され、MBean メソッドは wsadmin により呼び出せるため、wsadmin を使用して、データ・ソースとの接続をテストできます。 wsadmin からのデータ・ソース接続のテストには、2 つのオプションがあります。

wsadmin の AdminControl オブジェクトは、 データ・ソース・オブジェクトの構成プロパティーをテストする testConnection 操作を備えています。 詳しくは、『wsadmin を使用した接続のテスト』トピックを参照してください。

また、MBean 操作を起動して接続をテストできます。 この技法のガイドとして、『例: wsadmin を用いたデータ・ソース接続のテスト』トピックを参照してください。

Java スタンドアロン・プログラム

最後に、DataSourceCfgHelper MBean で testConnection メソッドを実行して、接続をテストする方法があります。この方法では、構成されたデータ・ソースの構成 ID を渡すことができます。この Java プログラムは、稼働中の Java Management Extensions (JMX) サーバーに接続して、MBean にアクセスします。Application Server の基本インストールでは、アプリケーション・サーバーで稼働中の JMX サーバーへの接続に、通常はポート 8880 を使用します。

この起動からの戻り値は、0、正数または例外のいずれかです。 0 は、操作が正常に完了し、警告が戻されなかったことを意味します。 正数は、操作が完了したこと、および警告の数を意味します。 例外は、接続のテストが失敗したことを意味します。

注: 例: testConnection(ConfigID) を使用した接続のテスト

以下のコード例ではデータ・ソース・インスタンスと関連接続インスタンスを作成し、それらをテストしてデータベースの接続を確認します。

このプログラムは、JMX を使用して、 稼働中のサーバーとの接続および DataSourceCfgHelper MBean における testConnection メソッドの起動を行ないます。コメント行の頭字語 ND は、以下のコードが WebSphere Application Server WebSphere Application Server Network Deployment に適用されていることを示しています。 コメント行の単語 Base は、以下のコードが WebSphere Application Server に適用されていることを示しています。

/**
 * 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 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 you need to 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 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= " + 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);
  }
 }
}
ヒント: テスト接続サービスは必ず既存のデータ・ソースと同じレベルで実行してください。例えば、データ・ソースがサーバー・レベルで構成されている場合は、テスト接続サービスをノード・レベルで実行しないでください。 テスト接続サービスとデータ・ソース構成が同じレベルに存在しない場合、ロード失敗例外が発生する可能性があります。 この場合、db2profile スクリプトをマシンに調達し、環境が DB2® ネイティブ・ライブラリーへのポインターを含むことを確認してください。db2profile スクリプトは、DB2 ユーザー ID のルート・ディレクトリーにあります。

トピックのタイプを示すアイコン 概念トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-base-dist&topic=cdat_testcon
ファイル名:cdat_testcon.html