为什么要在Oracle中访问WebService?在系统实现中,有时会有直接在数据库端利用触发器、存储过程等方式进行数据传递、分发的业务,而其中可能会涉及一些业务逻辑,为了处理这些业务逻辑,并简单起见,可以直接在触发器或存储过程中调用一些java类或WebService。
2. 准备工作1、首先保证Oracle为完全安装(本文均以10.2g);
2、下载对应版本的UTL_DBWS(一个工具包dbws-callout-utility-10131.zip),解压后放到oracle安装目录下的<oracle_install_dir>/sqlj/lib中;
3、在命令行中利用loadjava命令(一般安装完jdk或oracle之后就会有)将jar包导入oracle对应的用户中:
loadjava -u user/pwd@dbinstance -r -v -f -genmissing dbwsclientws.jar dbwsclientdb102.jar
若为SYS用户,命令为:
loadjava -u sys/change_on_install -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb102.jar
3. 步骤说明
前面准备工作做好之后,如果未发现UTL_DBWS(可以在plsql中敲入sys.若未有utl_dbws显示,则需要进行初始化),需要运行之前下载的包中dbws-callout-utility-10131\sqlj\lib\ utl_dbws_body.sql及utl_dbws_decl.sql,这样准备工作基本就绪,下面是调用的步骤:
1、 编写sql函数并发布。
create or replace function FUNC_GENERATE_LOGINNAME(xmlstring VARCHAR2)
return VARCHAR2
AS
l_service utl_dbws.SERVICE;
l_call utl_dbws.call;
l_result ANYDATA;
l_wsdl_url VARCHAR2(1024);
l_service_name VARCHAR2(200);
l_operation_name VARCHAR2(200);
l_input_params UTL_DBWS.anydata_list;
BEGIN
l_wsdl_url := 'http://192.168.21.85:9080/xgxt/services/wfWebService?wsdl';
l_service_name := 'WorkflowWebServiceService';
l_operation_name := 'saveFormData';
l_service := UTL_DBWS.create_service(wsdl_document_location=>URIFACTORY.getURI(l_wsdl_url),service_name=>l_service_name);
l_call := UTL_DBWS.create_call(service_handle=>l_service,port_name=>NULL,operation_name=>l_operation_name);
l_input_params(1) := ANYDATA.ConvertVarchar2(xmlstring);
l_result := UTL_DBWS.invoke(call_handle=>l_call,input_params=>l_input_params);
UTL_DBWS.release_call (call_handle=>l_call);
UTL_DBWS.release_service (service_handle=>l_service);
RETURN ANYDATA.AccessVarchar2(l_result);
EXCEPTION
WHEN OTHERS THEN
RETURN substr(sqlerrm,0,2000);
END FUNC_GENERATE_LOGINNAME;
2、 查看结果(select func_generate_loginname(var) from dual)
对于其中出现的异常,最可能的就是出现权限不足,可以利用如下语句解决:
begin
dbms_java.grant_permission( 'XGXT', 'SYS:java.lang.RuntimePermission', 'getClassLoader', '' );
dbms_java.grant_permission( 'XGXT','SYS:java.lang.IllegalAccessException','getClassLoader', '' );
dbms_java.grant_permission( 'XGXT', 'SYS:java.lang.RuntimePermission', 'accessClassInPackage.sun.util.calendar', '' );
dbms_java.grant_permission( 'XGXT', 'SYS:java.lang.RuntimePermission', 'setFactory', '' );
dbms_java.grant_permission( 'XGXT', 'SYS:java.util.PropertyPermission', 'HTTPClient.socket.idleTimeout', 'write' );
dbms_java.grant_permission( 'XGXT', 'SYS:java.net.SocketPermission', 'localhost', 'resolve' );
dbms_java.grant_permission( 'XGXT', 'SYS:java.net.SocketPermission', '192.168.21.203:80', 'connect,resolve' );
dbms_java.grant_permission( 'XGXT', 'SYS:java.lang.RuntimePermission', 'createClassLoader', '' );
end;
1、以上还只是针对windows服务器,对于unix/linux,主要是一些环境变量需要配置,需要查文档解决。
2、有帮助的语句
SELECT owner, status, count(*) FROM DBA_OBJECTS
WHERE OBJECT_TYPE='JAVA CLASS'
GROUP BY owner, status;
select * from User_Objects t where t.object_type like '%JAVA_CLASS%' and object_name like '%DbwsProxy%';
SELECT TEXT FROM ALL_SOURCE
WHERE TYPE = 'PACKAGE' and owner like '%XGXT%'