zoukankan      html  css  js  c++  java
  • 三、dbms_pipe(类似UNIX系统的管道)

    1、概述

    说明:Oracle管道类似UNIX系统的管道,但不采用OS机制实现,管道信息被缓存到SGA中,当关闭例程时会丢失管道信息,建立公用管道所有数据库用户都可访问,私有管道只能由建立这访问。
    作用:用于在同一例程程的不同会话之间进行管道通信.注意,如果用户要执行包dbms_pipe中的过程和函数,则必须要为用户授权.
    sql>conn sys/oracle as sysdba;
    sql>grant execute on dbms_pipe to scott;

    2、包的组成

    1)、create_pipe
    作用:该函数用于建立公用管道或私有管道.如果将参数private设置为TRUE,则建立私有管道;如果设置为FALSE,则建立公用管道.
    语法:dbms_pipe.create_pipe(pipename in varchar2,maxpipesize in integer default 8192,private in boolean default true) return integer;
    其中,pepename指定管道名称,maxpipesize指定管道消息的最大尺寸,private指定管道类型,函数返回0则成功,反之失败。

    2)、pack_message
    作用:该过程用于将变量写入到本地消息缓冲区。
    说明:为了个管道发消息,首先使用过程pack_message将消息写入本地消息缓冲区,然后使用send_message将消息发送到管道。
    语法:dbns_pipe.pack_message(item in varchar2/nchar2/number/date);
    dbns_pipe.pack_message_raw(item in raw);
    dbns_pipe.pack_message_rowid(item in rowid);

    3)、send_message
    作用:该函数用于将本地消息缓冲区中的内容发送到管道。
    语法:dbms_pipe.send_message(pipename in varchar2,timeout in integer defalut maxwait,maxpipesize in integer default 8192) return integer;
    其中,timeout指定发送消息的超时时间,0成功1超时3中断。

    4)、receive_message
    说明:该函数用于接收管道消息,并将接收到的消息写入到本地消息缓冲区。当接收完管道信息之后,会删除管道消息,管道消息只能被接收一次。
    语法:
    dbms_pipe.receive_message(pepename in varchar2,timeout in integer default maxwait) return integer;
    其中,返回0接受成功,返回1超时,返回2本地缓冲区不能容纳管道消息,返回3发生中断。

    5)、next_item_type
    说明:该函数用于确定本地消息缓冲区下一项的数据类型。在调用receive_message之后调用。
    语法:dbms_pipe.next_item_type return integer;
    其中,如果该函数返回0,则表示管道没有任何消息;如果返回6,则表示下一项的数据类型为number;如果返回9,则表示下一项的数据类型为varchar2;
    如果返回11,则表示下一项的数据类型为rowid;如果返回12,则表示下一项的数据类型为date;如果返回23,则表示下一项的数据类型为raw.

    6)、unpack_message
    作用:该过程用于将消息缓冲区的内容取出来写入到变量中,每次只能取一条,需要取出多条需要多次调用。
    说明:在使用函数receive_message接收到管道消息之后,应该使用过程unpack_message取得消息缓冲区的消息。
    语法:dbms_pipe.unpack_message(item out varchar2 char umberdate);
    dbms_pipe.unpack_message_raw(item out raw);
    dbms_pipe.unpack_message_rowid(item out rowid);、

    7)、remove_pipe
    作用:该函数用于删除已经建立的管道
    语法:dbms_pipe.remove_pipe(pepename in varchar2) return integer;
    其中,函数返回0表示成功,否则会显示错误信息。

    8)、purge
    说明:该过程用于清除管道中的内容。
    语法:dbms_pipe.purge(pipename in varchar2);

    9)、reset_buffer
    说明:该过程用于复位管道缓冲区,因为所有管道都共享单个管道缓冲区,所以在使用新管道之前应该复位管道缓冲区。
    语法:dbms_pipe.reset_buffer;

    10)、unique_session_name
    说明:该函数用于为特定会话返回惟一的名称,并且名称的最长度为30字节,对同一会话其值不变。
    语法:dbms_pipe.unique_session_name


    3、包的应用


    1)、综合例子1
    declare
    falg int;
    v_ename emp.ename%type;
    v_sal emp.sal%type;
    v_rowid rowid;
    item_no int;
    message varchar2(100);
    v_session varchar2(200);
    begin
    flag := dbms_pipe.create_pipe('public_pipe', 8192, false);
    if flag = 0 then
    dbms_output.put_line('建立公用管道成功');
    end if;
    select ename,sal,rowid into v_ename,v_sal,v_rowid from emp where empno=7788;
    dbns_pipe.pack_message(v_ename||','||v_sal||','||v_rowid);
    flag :dbms_pipe.send_message('PUBLIC_PIPE');
    if flag = 0 then
    dbms_output.put_line('发送成功');
    end if;
    flag := dbms_pipe.receive_message('PUBLIC_PIPE');
    if flag = 0 then
    dbms_output.put_line('成功');
    end if;
    item_no := dbms_pipe.next_item_type;
    dbms_output.put_line(item_no);
    dbms_pipe.unpack_message(message);
    dbms_output.put_line(message);
    --remove_pipe
    flag:=dbms_pipe.remove_pipe('PUBLIC_PIPE');
    if flag = 0 then
    dbms_output.put_line('删除成功');
    end if;
    --unique_session_name
    v_session:=dbms_pipe.unique_session_name;
    dbms_output.put_line(v_session);
    end;

    2)、综合例子2
    使用管道是,一个会话需要将消息发送到管道中,另一个会话则需要接收管道消息。
    发送消息到管道需要先将消息写入本地消息缓冲区,然后再发送到管道;
    接收消息需要先使用本地消息缓冲区接收管道消息,然后从消息缓冲区取得具体消息。
    create or replace procedure send_message(pepename varchar2,message varchar2) is
    flag int;
    begin
    flag:=dbms_pipe.create_pipe(pipename);
    if flag=0 then
    dbms_pipe.pack_message(message);
    flag:=dbms_pipe.send_message(pipename);
    end if;
    end send_message;

    create or replace procedure receive_message(pipename varchar2,message out varchar2) is
    flag int;
    begin
    flag:=dbms_pipe.receive_message(pipename);
    if flag=0 then
    dbms_pipe.unpack_message(message);
    flag:=dbms_pipe.remove_pipe(pipename);
    end if;
    end receive_message;

    会话一:exec send_message('pipe1','你好');
    会话二:
    var message varchar2(100)
    exec scorr.receive_message('pipe1',:message)
    print message

  • 相关阅读:
    软件测试的术语及解释总结
    测试工具
    数据库客户端连接
    应用程序发生错误怎么修改
    软件测试容易忽略的缺陷
    LR中错误解决方法
    LR结果分析2
    LR结果分析
    Hive
    zookeeper
  • 原文地址:https://www.cnblogs.com/champaign/p/9468433.html
Copyright © 2011-2022 走看看