Oracle DBMS_PIPE的用法

前言

dbms_pipe是Oracle提供的管道功能,可以让不同session之前通过pipe交互信息。虽然使用也还算方便,但也存在诸多问题,如定位问题困难、管道积压导致数据丢失等,一般也就在遗留系统中存在。
本文主要介绍dbms_pipe的使用方法,以及我所经历的几个问题的解决方法。

使用(Writing and Reading Pipes)

创建管道时,可以选择管道类型是公有还是私有,公有管道标识所有用户均可访问;私有管道表示只有建立管道的用户可以访问。
详细功能可以阅读Oracle官方文档:https://docs.oracle.com/database/121/ARPLS/d_pipe.htm#ARPLS67396
以下引用了一段说明:

Each public pipe works asynchronously. Any number of schema users can write to a public pipe, as long as they have EXECUTE permission on the DBMS_PIPE package, and they know the name of the public pipe. However, once buffered information is read by one user, it is emptied from the buffer, and is not available for other readers of the same pipe.

The sending session builds a message using one or more calls to the PACK_MESSAGE procedure. This procedure adds the message to the session's local message buffer. The information in this buffer is sent by calling the SEND_MESSAGE function, designating the pipe name to be used to send the message. When SEND_MESSAGE is called, all messages that have been stacked in the local buffer are sent.

A process that wants to receive a message calls the RECEIVE_MESSAGE function, designating the pipe name from which to receive the message. The process then calls the UNPACK_MESSAGE procedure to access each of the items in the message.

常用方法:

子程序 描述
create_pipe 创建管道
pack_message 在本地缓冲区中构建消息
purge 清除命名管道的内容
receive_message 将命名管道中的消息复制到本地缓冲区
remove_pipe 删除命名管道
reset_buffer 清除本地缓冲区的内容
send_message 在命令管道上发送消息,如果管道不存在,则隐式创建
unpack_message 访问缓冲区中的下一项

测试

declare
  FLAG NUMBER;
  PIPENAME VARCHAR2(10);
  MESSAGE  VARCHAR2(200);
  MESSAGE2  VARCHAR2(200);
begin
  PIPENAME := 'pipe';
  MESSAGE  := 'I send message';
  FLAG := DBMS_PIPE.CREATE_PIPE(PIPENAME);
  IF FLAG = 0 THEN
    DBMS_PIPE.PACK_MESSAGE(MESSAGE);
    FLAG := DBMS_PIPE.SEND_MESSAGE(PIPENAME);
  END IF;
  
  FLAG := DBMS_PIPE.RECEIVE_MESSAGE(PIPENAME);
  IF FLAG = 0 THEN
    DBMS_PIPE.UNPACK_MESSAGE(MESSAGE2);
    FLAG := DBMS_PIPE.REMOVE_PIPE(PIPENAME);
    dbms_output.put_line(MESSAGE2);
  END IF;
end;

由于本次测试仅仅是验证pipe是否可用,所以在一个会话中写入管道后又重新读取管道,正常是需要在多个会话中处理的。
比如正式场景一般均为由Oracle触发消息,写入管道后,由专门的程序读取管道并处理消息。

问题

1、pipe支持多个消息发布者,但只支持一个消息订阅者,所以如果处理端性能有问题,需要增加中间件做分布式调度使用。
2、pipe缓冲区默认是8192字节,注意是字节,而不是消息的数量,如果管道已经存满,则会阻止之后的消息存放,直到超期或者管道空间释放。
3、使用过程中,如果提示管道不存在(正常会隐式创建),可以尝试删除管道再重新创建。

总结

dbms_pipe优缺点都比较明显,视情况而定吧,个人是不太建议使用的,出问题查错的开销太大。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,916评论 0 23
  • 图、文/野兔丫 看着兔丫每天空余时间涂涂画画,忙得不亦乐乎,胖兔着急了,再也傻乐不下去。 趁兔丫没注意,胖兔背起袋...
    野兔丫阅读 1,483评论 24 32
  • 凌晨4点,值夜班,疲惫、疲倦的我,照例点燃了一根香烟。吞云吐雾间,突然想到一个很有意思的现象。“吸烟有害健康”,我...
    风起雲阅读 192评论 0 0
  • 今天陪着儿子到新华书店进行美文朗诵。一路上儿子非常的高兴,像飞出林子的小鸟, 叽叽喳喳说个不停,从他身上看不...
    6a130fcb7353阅读 138评论 0 0