Introduction of multi-process communication technology in Pl/sql

Source: Internet
Author: User
Tags commit copy end functions implement integer sql sessions
Process


Pl/sql is a mainstream application programming language based on Oracle, which is characterized by a combination of SQL statements and procedural development languages to achieve more complex business logic. This article mainly discusses the multiple process communication.
Obviously, multi-process technology is used to improve the concurrency of the application, and thus improve the execution efficiency of the whole system, so how to implement the Pl/sql communication in the process? In fact, Pl/sql's design was designed primarily to enhance the functionality of SQL statements, without taking into account the advanced functionality of other programming languages, so that pl/sql communication can only be achieved with the help of Oracle's two development packages: Dbms_pipe and Dbms_alert.

1. Dbms_pipe

The package provides methods for pipeline communication between multiple processes, such as the ability to communicate through pipelines between two separate sessions connected to the same database, and also to communicate between stored procedures and pro*c, which greatly enhances the processing power of the pl/sql. The package mainly provides two pairs of functions:

Pack_message (v_msg varchar2)-----Packaging v_msg information into the buffer, ready to send;

Send_message (v_pipename varchar2)-----Send a buffer of the pipe named V_pipename;

Unpack_message (v_msg varchar2)-----Parsing the information into v_msg;

Receive_message (v_pipename varchar2)----receive a buffer of a pipe named V_pipename;

The principle of its implementation is: first to establish a well-known pipeline (this is familiar with Unix very clear), the pipeline's sender and receiver have a corresponding buffer to receive and send processing, it should be noted that the text information must be packaged to send, through parsing to read information.

To understand the previous description, the following is an example of a communication between two sessions:

Send process:

Declare
V_pipename varchar2: = ' pipe1 ';
V_status integer;
Begin
Dbms_pipe.pack_message (' hello,this is sending process! ');
V_status:=dbms_pipe.send_message (V_pipename);
If V_status!=0 Then
Dbms_output.put_line (' error! ');
End If;
End
/

Accept process:

Declare
V_pipename varchar2: = ' pipe1 ';
V_status integer;
V_msg varchar2 (20);
Begin
V_status:=dbms_pipe.receive_message (V_pipename);
If V_status!=0 Then
Dbms_output.put_line (' error ');
End If;
Dbms_pipe.unpack_message (V_MSG);
Dbms_output.put_line (V_MSG);
End
/

2. Dbms_alert

Like Dbms_pipe, Dbms_alert can implement communication between multiple processes (sessions). Its basic implementation process is: first, establish an alarm channel, and then through the alarm channel to send the alarm signal, at the receiving end need to register the alarm channel, the channel to monitor, and then wait for the arrival of the alarm signal. The following is a description of the main functions in the package:

Dbms_alert.signal (Alarm pipe name, message to be sent)---send alarm information;
Dbms_alert.register (Alarm pipe name)------registered Alarm pipe;
Dbms_alert.waitone (Alarm pipe name, accept message value, return status value)-------to monitor the alarm pipeline, waiting for the arrival of the message;

Also, an example of using Dbms_alert is given here:

Send process:

Declare
V_alertname varchar2: = ' alert1 ';
Begin
Dbms_alert.signal (V_alertname, ' hello,this is sending process! ');
Commit
End
/

Accept process:

Declare
V_alertname varchar2: = ' alert1 ';
V_status integer;
V_msg varchar2 (20);
Begin
Dbms_alert.register (V_alertname);
Dbms_alert.waitone (V_alertname,v_msg,v_status);
If V_status!=0 Then
Dbms_output.put_line (' error ');
End If;
Dbms_output.put_line (V_MSG);
End
/

3. Some notes

Both Dbms_pipe and Dbms_alert can communicate between multiple processes, but there are some differences between the two:

1) The alarm signal is synchronized. The alarm signal is not emitted until the session issues a commit, and if the transaction it is in is rolled back, the signal is not sent. However, the pipeline signal is asynchronous and its communication process is not affected by transaction commits and rollbacks.

2 The message passed along the pipeline can only be processed by a process, the message received by the copy after the deletion, but the alarm signal can be obtained by multiple processes, that is, the message received only copy.

3 Pipeline message can not only pass the text information, but also can pass other information, such as objects, such as the benefit of its packaging of messages to preprocessing, and alert messages can only be text, cannot be other types.

4. Summary

In recent years, although many process applications have been gradually replaced by multithreading, but many processes still have their application space, I hope that the reader through the introduction of this article, the Pl/sql based on the process of communication technology has a preliminary understanding, so as to further the development of a certain foundation.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.