测试连接服务
WebSphere® Application Server 提供了用于验证数据源配置的测试连接服务。testConnection 操作实例化数据源配置、获取连接,然后立即关闭连接。
如果将数据源与 WebSphere 变量相关联,请参阅主题“创建、编辑和删除 WebSphere 变量”以验证它们的配置是否正确。找不到变量异常是由于尝试使用通过未正确定义的变量调用的数据源造成的。
激活测试连接服务
有三种方式可用来激活测试连接服务:通过管理控制台、wsadmin 工具或 Java™ 独立程序。 每个进程都调用同一 MBean 上的同一方法。
管理控制台
WebSphere Application Server 允许您只需按下按钮就可从管理控制台来测试连接:数据源连接、数据源设置、V4 数据源集合和 V4 数据源设置页面都有测试连接按钮。在定义并保存数据源之后,您可以单击此按钮以确保此数据源定义中的参数是正确的。在集合页面上,您可以选择多个数据源并同时对其进行测试。注意,首先必须满足某些条件。有关更多信息,请参阅主题“使用管理控制台测试连接”。
对节点 svtaix24Node01 中服务器 server1 上的数据源 isagent 进行测试连接失败,出现以下异常:java.lang.Exception: java.sql.SQLException: JZ006: 捕获到 IOException: java.net.ConnectException: 远程主机拒绝了一个尝试连接操作。DSRA0010E: SQL 状态 = JZ006,错误代码 = 0
当 Sybase 数据源端口号与在
Sybase 服务器中配置的端口不匹配时,就会出现此异常。缺省端口号是 5000。请在 /<sybase 安装目录> 下的接口文件中检查 Sybase 服务器的端口号。wsadmin 工具
wsadmin 工具为所有 WebSphere Application Server 管理活动提供了脚本编制接口。由于测试连接功能作为 MBean 上的方法实现,并且 wsadmin 可以调用 MBean 方法,因此可以使用 wsadmin 测试到数据源的连接。要用 wsadmin 测试数据源连接,您有两种选择:
wsadmin 的 AdminControl 对象具有一个 testConnection 操作,该操作用于测试数据源对象的配置属性。有关信息,请参阅主题“使用 wsadmin 测试连接”。
您还可以通过调用 MBean 操作测试连接。使用本主题中的示例“示例:使用 wsadmin 测试数据源连接”作为此技术的指南。
Java 独立程序
最后,您可以通过执行 DataSourceCfgHelper MBean 上的 testConnection 方法来测试连接。此方法允许您传递配置数据源的配置标识。Java 程序连接到正在运行的 Java 管理扩展 (JMX) 服务器以访问 MBean。在 Application Server 的基本安装中,您连接到在应用程序服务器中(通常在端口 8880 上)运行的 JMX 服务器。
此调用的返回值将是 0、正数或异常。0 表明操作成功完成,没有警告。正数表明操作成功完成,但有一些警告。异常表明测试连接失败。
以下样本代码创建数据源实例和相关联的连接实例,并对它们进行测试以确保数据库连接。
此程序使用 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);
}
}
}
概念主题