(在Oracle9.2下,link:http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96612/u_tcp.htm#ARPLS075)
With the UTL_TCP package and its procedures and functions, PL/SQL applications can communicate with external TCP/IP-based servers using TCP/IP. Because many Internet application protocols are based on TCP/IP, this package is useful to PL/SQL applications that use Internet protocols and e-mail.
(通过使用UTL_TCP包和他的过程与函数,PL/SQL程序能够使用TCP/IP协议与其他的基于TCP/IP的服务器进行通信。这个包对于使用网络和email服务的PL/SQL程序非常有用,因为许多网络服务程序协议都是基于TCP/IP协议的。)
Oracle的UTL_TCP包提供了一种额外的方法来主动与应用程序来进行通信。比如我们可以通过一个触发器,当某张表的数据被更改时,即时通知应用程序基础的通知应用程序服务,当然,这个应用程序服务需要开监听。写了个小测试东西来测试了下UTL_TCP:
1)以下是一个PL/SQL过程,使用UTL_TCP包,与ip为xxx.xxx.xxx.xxx:1234进行通信。只是简单的发送"Result has changed":
1: CREATE OR REPLACE procedure USERID.P_SendNotice
2: IS
3: conn utl_tcp.connection;
4: ret_val pls_integer;
6: conn := utl_tcp.open_connection(remote_host => 'xxx.xxx.xxx.xxx',
7: remote_port => 1234,
8: charset => 'US7ASCII');
9:
10: ret_val:=utl_tcp.write_line(conn, 'Result has changed');
11: dbms_output.put_line(to_char(ret_val));
13: when utl_tcp.NETWORK_ERROR then
14: dbms_output.put_line('network error');
15: End;
2)在一张表上建个触发器,以便对该表数据进行更改时候,发送通知消息给外部服务器:
1: CREATE OR REPLACE TRIGGER USERID.noticesend_trg
3: ON USERID.TableName REFERENCING NEW AS NEW OLD AS OLD
5: P_SendNotice ();
6: END;
3)在应用程序中,应该监听我在上面包中所写ip为xxx.xxx.xxx.xxx的1234端口,以完成对相应消息的接收
至此,完成一个简单的Oracle UTL_TCP的小应用。当表数据中发生改变的时候,通过触发器来调用过程,发送通知给应用服务器,应用服务器再做相应的相应。
UTL_TCP的限制:The UTL_TCP package provides TCP/IP client-side access functionality in PL/SQL. The API provided in the package only allows connections to be initiated by the PL/SQL program.It does not allow the PL/SQL program to accept connections initiated outside the program.