conn sys/sys@oracle as sysdba
grant execute on dbms_pipe to scott;
grant execute on dbms_lock to scott;
打开一个SESSION
conn scott/scott
DECLARE
pipename CONSTANT VARCHAR2(12) := 'signaler';
result INTEGER;
pipebuf VARCHAR2(64);
BEGIN
result := DBMS_PIPE.create_pipe(pipename);
LOOP
DBMS_OUTPUT.PUT_LINE('FF');
DBMS_LOCK.SLEEP(10);
IF DBMS_PIPE.receive_message(pipename, 0) = 0
THEN
DBMS_PIPE.unpack_message(pipebuf);
EXIT WHEN pipebuf = 'stop';
END IF;
END LOOP;
END;
pipename CONSTANT VARCHAR2(12) := 'signaler';
result INTEGER;
pipebuf VARCHAR2(64);
BEGIN
result := DBMS_PIPE.create_pipe(pipename);
LOOP
DBMS_OUTPUT.PUT_LINE('FF');
DBMS_LOCK.SLEEP(10);
IF DBMS_PIPE.receive_message(pipename, 0) = 0
THEN
DBMS_PIPE.unpack_message(pipebuf);
EXIT WHEN pipebuf = 'stop';
END IF;
END LOOP;
END;
/
执行一段时间后
再打开另一个SESSION执行如下代码:
DECLARE
pipename VARCHAR2(12) := 'signaler';
result INTEGER := DBMS_PIPE.create_pipe(pipename);
BEGIN
DBMS_PIPE.pack_message('stop');
result := dbms_pipe.send_message(pipename);
END;
pipename VARCHAR2(12) := 'signaler';
result INTEGER := DBMS_PIPE.create_pipe(pipename);
BEGIN
DBMS_PIPE.pack_message('stop');
result := dbms_pipe.send_message(pipename);
END;
/
再去观察第一个SESSION的代码死循环就会执行完成。