dbms_pipe

/*
dbms_pipe 用於同一例程在不同會話之間進行管道通信;
共用管道是所有數據庫用戶都可以訪問;
私有管道只能由建立者數據庫用戶訪問;
授權 – grant excute on dbms_pipe to scott;
*/

– 1,create_pipe
/*
該函數建立私有或共有管道;
若返回0,表示管道建立成功;
*/
declare
flag int;
begin
flag:= dbms_pipe.create_pipe(‘public_pipe’,2561,false);
if flag=0 then
dbms_output.put_line(‘管道建立成功……….’);
end if;
end;

– 2,pack_message
/*
把本地消息寫入緩存區,
爲把消息發送到管道中做準備;
*/

declare
name emp.empname%type;
v_sal emp.sal%type;
v_rowid rowid;
begin
select empname,sal ,rowid into name ,v_sal,v_rowid from emp where empno = 8;
dbms_pipe.pack_message(name);
dbms_pipe.pack_message(v_sal);
dbms_pipe.pack_message(v_rowid);
end;

– 3,send_message
/*
該函數將本地緩衝區的消息發送到管道;
0 —-成功
1—–超時
3—–中斷
*/
declare
flag int ;
begin
flag:=dbms_pipe.send_message(‘public_pipe’);
if flag = 0 then
dbms_output.put_line(‘向管道發送消息成功……………’);
end if;
end;

–4,recieve_message
/*
該函數將接收管道消息,把他放置本地緩存區
0 —-接收成功
1—–接收超時
2——本地緩存區不能容納管道消息
3—–中斷
*/

declare
flag int ;
begin
flag:=dbms_pipe.receive_message(‘public_pipe’);
if flag = 0 then
dbms_output.put_line(‘接受管道消息成功……………’);
end if;
end;

–5,next_item_type
/*
該函數將接收管道消息後,確定本地緩存區的下一條消息的類型
o—該管道沒有任何消息
6—number
9—-varchar2
11—-rowid
12—-date
23—raw
*/
declare
no int;
begin
no:=dbms_pipe.next_item_type;
dbms_output.put_line(‘管道消息類型編號爲:’ || no);
end;

—6,unpack_masage
/*
recieve_message把消息放入緩衝區後;
unpack_message 該過程用於將消息寫入變量中;
*/

declare
mass number(20);
begin
dbms_pipe.unpack_message(mass);
dbms_output.put_line(mass);
end;

—7,remove_masage
/*
該函數用於刪除管道
0 —刪除成功
*/

declare
no int;
begin
no := dbms_pipe.remove_pipe(‘paublic_pipe’);
if no =0 then
dbms_output.put_line(‘管道刪除成功…….’);
end if;
end;

–8.purge
/*
用於清除管道里的消息
*/

begin
dbms_pipe.purge(‘public_pipe’);
end;

– 9,reset_buffer
/*
復位管道緩衝區
因爲所有管道都共享單個管道緩衝區。所以在使用新管道時,要復位管道緩衝區;

*/

begin
dbms_pipe.reset_buffer;
end;

– 10,unique_session_name
/*
爲特定會話返回唯一的名稱;
*/

declare
name varchar2(100);
begin
name := dbms_pipe.unique_session_name;
dbms_output.put_line(name);
end;


/*
管道使用特例
*/

–建立過程send_message

grant execute on dbms_pipe to system;
create or replace procedure send_message(
pipename 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;

 --建立過程recieve_message

 create or replace procedure recieve_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);
       dbms_output.put_line(message);
       flag:= dbms_pipe.remove_pipe(pipename);
    end if;    
end;

–使用過程
begin
send_message(‘public_pipe’,’how are you ‘);
end;

declare
message varchar2(100);
begin
recieve_message(‘public_pipe’,message);
dbms_output.put_line(message);
end;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章