现在需要将访问数据库的JDBC接口发布到Axis2搭建的Tomcat7.0服务器上。
遇到的问题是,JDBC接口在本地测试正常,但是发布到服务器后,建立Client工程使用PRC方式调用则报错。
Exception in thread "main" org.apache.axis2.AxisFault: Exception occurred while trying to invoke service method login
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531)
at org.apache.axis2.rpc.client.RPCServiceClient.invokeBlocking(RPCServiceClient.java:102)
at yerasel.Axis2LoginTest.main(Axis2LoginTest.java:29)
错误原因是Tomcat7.0未配置数据源。
配置过程如下:(每个人的具体工程可能不同,但是配置Tomcat过程是相同的,是最重要的,这是本文重点)
1. 将Oracle数据库的JDBC驱动ojdbc14.jar放入TOMCAT_HOME//lib下。
2. Tomcat7.0\conf\context.xml的<Context></Context>标签之间中添加信息
<Resource
name="jdbc/fcwf"
auth="Container"
type="javax.sql.DataSource"
password="Psw"
username="Usrname"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@10.23.117.134:1521:ABC"
maxActive="100"
maxIdle="30"
maxWait="5000"
/>
如图

有的文章说Tomcat7.0安装目录下的localhost也要写东西:
在conf/Catalina/localhost下面建立了的web描述文件比如我的web叫myapps然后我建立myapps.xml在里面写入 <?xml version= "1.0 " encoding= "ISO-8859-1 "?>
<Context path= "/myapps " docBase= "路径"
debug= "0 " privileged= "true ">
<ResourceLink name= "jdbc/mysql " global= "mysql " type= "javax.sql.DataSource " />
</Context>
<Context path= "/myapps " docBase= "路径"
debug= "0 " privileged= "true ">
<ResourceLink name= "jdbc/mysql " global= "mysql " type= "javax.sql.DataSource " />
</Context>
经验证,没有必要。
3. 配置工程的web.xml文件。
工程目录下bin下建立WEB-INF,其中建立web.xml,内容为
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/fcwf</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
请注意,jdbc/fcwf名字一定要统一。
4. 发布webservice,具体可以参见上一篇日志,
http://blog.csdn.net/ozwarld/article/details/7740169
工程目录框图为:

5. 在client端编写测试代码,使用RPC等方式调用Webservice的接口即可。
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
/* 用户登录 */
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://10.24.28.139:8080/axis2/services/User");
options.setTo(targetEPR);
options.setManageSession(true);
// 指定方法的参数值
Object[] opAddEntryArgs = new Object[] { "Yerasel", "123456" };
// 指定方法返回值的数据类型的Class对象
Class[] classes = new Class[] { Integer.class };
// 指定要调用的getGreeting方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://axis2.demo", "login");
// 调用方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0]);
// 调用方法并输出该方法的返回值
// 1表示jdbc连接错误;2表示查询不到usrName, psw;
// 0表示完成设置key-value对
int iRes = -1;
iRes = (
(Integer)((serviceClient.invokeBlocking(
opAddEntry, opAddEntryArgs, classes))[0])
).intValue();
switch (iRes) {
case 1:
System.out.println("JDBC连接错误");
break;
case 2:
System.out.println("用户验证无效");
break;
case 0:
System.out.println("用户验证完成");
break;
default:
System.out.println("客户端调用Login未定义行为");
break;
}
客户端运行结果如下:
