DBMS_PIPE包的使用

本文详细介绍了Oracle数据库中的DBMS_PIPE包,这是一个用于在同一例程的不同会话间进行通信的工具。DBMS_PIPE允许创建私有或公共管道,并提供了发送、接收、打包和解包消息的功能。通过grant execute权限,用户可以使用这个包。此外,还讨论了如何管理管道,包括删除、清除和复位缓冲区等操作。
摘要由CSDN通过智能技术生成

DBMS_PIPE包的使用

  DBMS_PIPE包用于在同一例程的不同会话之间进行管理通信。
  Oracle管道(PIPE)类似于UNIX系统的管道,但它不是采用操作系统机制实现的,基管道信息被缓存在SGA中,当关闭例程时会丢失管道信息。在建立管道中,既可以建立公用管道,也可以建立私有管道。其中,公用管道是指所有数据库用户都可以的管道,而私有管道只能由建立管道的数据库用户访问。.注意,如果用户要执行包DBMS_PIPE的过程和函数,则必须要为用户授权。

grant excute on dbms_pipe to scott;
  • create_pipe 该函数建立私有或共有管道;
    如果函数返回0,表示管道建立成功;
    语法如下:
    dbms_pipe.create_pipe(
    	pipename in varchar2,
    	maxpipesize in integer default 8192,
     	private in bollean default true
    )retuen integer;
    --pipename用于指定管道的名称
    --maxpipesize用于指定管道消息的最大尺寸
    --private用于指定管道类型,将参数private设置为true,则建立私有管道;如果为false,则建立公用管道
    
  • pack_message 把本地消息写入缓存区,为把消息发送到管道中做准备;
    首先需要使用过pack_message将消息写入到本地消息缓冲区,然后使用过程send_message 将本地消息缓冲区中的消息发送到管道
    dbms_pipe.pack_message(item in varchar2);
    dbms_pipe.pack_message(item in nchar);
    dbms_pipe.pack_message(item in number);
    dbms_pipe.pack_message(item in date);
    dbms_pipe.pack_message_raw(item in raw);
    dbms_pipe.pack_message_rowid(item in rowid);
    
  • send_message 该函数将本地缓冲区的消息发送到管道;
    如果函数返回0,则表示消息发送成功;
    如果函数返回1,则表示发送消息超时;
    如果函数返回3,则表示出现中断;
    dbms_pipe.send_message(
    	pipename in varchar2,
    	timeout in integer default maxwait,
    	maxpipesize in integer default 8192
    )return integer;
    --timeout用于指定发送消息的超时时间
    
  • receive_message 该函数用于接收管道消息,并将接收到的消息写入到本地消息缓冲区,当接收到管道消息之后,会删除管道消息。注意,管道消息只能被接收一次;
    如果函数返回0,则表示接收消息成功;
    如果函数返回1,则表示出现超时;
    如果函数返回2,则表示本地缓冲区不能容纳管道消息;
    如果函数返回3,则表示发生中断;
    dbms_pipe.receive_message(
    	pipename IN varchar2,
        timeout IN integer default maxwait
    )return integer;
    
  • next_item_type 该函数将接收管道消息后,确定本地缓存区的下一条消息的类型;
    如果函数返回0,则表示管道没有任何消息;
    如果返回6,则表示下一项的数据类型为NUMBER;
    如果返回9,则表示下一项的数据类型为VARCHAR2;
    如果返回11,则表示下一项的数据类型为ROWID;
    如果返回12,则表示下一项的数据类型为DATE;
    如果返回23,则表示下一项的数据类型为RAW;
    dbms_pipe.next_item_type() return integer;
    
  • unpack_message 在receive_message把消息放入缓冲区后;unpack_message取得消息缓冲区的消息;
     dbms_pipe.unpack_message(item out varchar2);
     dbms_pipe.unpack_message(item out nchar);
     dbms_pipe.unpack_message(item out number);
     dbms_pipe.unpack_message(item out date);
     dbms_pipe.unpack_message_raw(item out raw);
     dbms_pipe.unpack_message_rowid(item out rowid);
    
  • remove_pipe 该函数用于删除已经建立的管道;返回0表示删除成功;
    dbms_pipe.remove_pipe(pipename in varchar2) return integer;
    
  • purge 用于清除管道里的消息;
    dbms_pipe.purge(pipename in varchar2);
    
  • reset_buffer 复位管道缓冲区,因为所有管道都共享单个管道缓冲区,所以在使用新管道时,要复位管道缓冲区;
    dbms_pipe.reset_buffer();
    
  • unique_session_name 为特定会话返回唯一的名称;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值