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\nchar\number\date);
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

三、dbms_pipe(类似UNIX系统的管道)的更多相关文章

  1. UNIX 系统上的文本操作简介

    http://www.oschina.net/question/129540_53561 UNIX 的基本哲学之一就是创建只做一件事并将这一件事做好的程序(或进程).这一哲学要求认真考虑接口以及结合这 ...

  2. 《Linux/Unix系统编程手册》 时间子系统

    Linux下操作系统编程有两本经典APUE即<Advanced Programming in the UNIX Environment>和TLPI<The Linux Program ...

  3. 《Linux/UNIX系统编程手册》第63章 IO多路复用、信号驱动IO以及epoll

    关键词:fasync_helper.kill_async.sigsuspend.sigaction.fcntl.F_SETOWN_EX.F_SETSIG.select().poll().poll_wa ...

  4. Unix及类Unix系统文本编辑器的介绍

    概述 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是纯粹的自由软件. Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌来自Em ...

  5. UNIX系统的显示时间何时会到达尽头

    本文分为三个小块: 一.UNIX系统中时间的存储形式: 二. time_t 的最大值是多少: 三. 将time_t 的最大值转化为真实世界的时间: #---------------------# # ...

  6. (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  7. 《Linux/Unix系统编程手册》读书笔记5

    <Linux/Unix系统编程手册>读书笔记 目录 第8章 本章讲了用户和组,还有记录用户的密码文件/etc/passwd,shadow密码文件/etc/shadow还有组文件/etc/g ...

  8. UNIX系统接口

    UNIX系统接口 8.1 文件描述符 UNIX操作系统中,所有的外围设备(包括键盘和显示器)都被看作是文件系统中的文件.系统通过文件描述符来标识文件:标准输入为0,标准输出为1,标准错误为2. 当程序 ...

  9. Unix系统操作指令汇总

    一.目录及文件操作命令 1.1 ls 语法: ls [-RadCxmlnogrtucpFbqisf1] [目录或文件--] 说明: ls 命令列出指定目录下的文件,缺省目录为当前目录 ./,缺省输出顺 ...

随机推荐

  1. Error: registers may not be the same -- `strexb r3,r2,[r3]'

    tmp\ccFziEge.s:914: Error: registers may not be the same -- `strexb r3,r2,[r3]'tmp\ccFziEge.s:968: E ...

  2. [转]Earth Mover's Distance (EMD)

    转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981 Earth Mover's Distance (EMD)原文: htt ...

  3. AndroidManifest.xml中的注册组件

    界面跳转时Activity的识别方法有两种:第一种,通过name 第二种,通过<intent-filter> 通过配置文件中配置<intent-filter>来实现Activi ...

  4. ls命令输出的文件颜色

    ls的输出颜色不止3种,有以下几种,白色:表示普通文件蓝色:表示目录绿色:表示可执行文件红色:表示压缩文件浅蓝色:链接文件红色闪烁:表示链接的文件有问题 黄色:表示设备文件 灰色:表示其他文件 这是l ...

  5. iis日志时间与本地日期不一样

    iis日志里面的时间是 UTC时间,所以要自已加时区进行转换:即 utc时间+8小时:

  6. 20135320赵瀚青LINUX内核分析第四周学习笔记

    赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 概述 本周的内容主要是讲解系 ...

  7. Spring笔记1——Spring起源及其核心技术

    Spring的作用 当我们使用一种技术时,需要思考为什么要使用这门技术.而我们为什么要使用Spring呢?从表面上面SSH这三大框架中,Struts是负责MVC责任的分离,并且提供为Web层提供诸如控 ...

  8. linux下如何获取sd卡中的mbr

    答:使用dd命令,示例如下: dd if=/dev/mmcblk0 of=mbr.bin bs=512 count=1 解析: bs表示指定输入输出的块大小为512个字节 count表示指定读取输入的 ...

  9. Vjudge - B - 这是测试你会不会排序

    2017-07-14 22:44:11 writer:pprp 题目: 他们提出的比赛规则是这样的: 1.  每方派出n个人参赛: 2.  出赛的顺序必须是从弱到强(主要担心中国人擅长的田忌赛马):  ...

  10. Vim练级攻略(转)

    转自平凡的世界:http://www.ccvita.com/ 前言今天看到这篇文章,共鸣点非常多.它把Vim使用分为4个级别,目前我自己是熟练运用前面三级的命令,在培养习惯使用第四级.完全就是我这一年 ...