ORACLE-009: locking stored procedures

Source: Internet
Author: User

ORACLE-009: locking stored procedures

Recently, we have encountered a situation where only one process can be executed in a stored procedure, and the last process is executed and then executed. This is similar to thread synchronization in program development.

You can use the following method to summarize the data.

1. Set a variable, or a field in the table as the identification space, set it to a value during execution, and then set it to the original value after execution.

This method requires that the interval between setting the location of the flag and determining the location of the flag cannot be too long; otherwise, the requirement still cannot be met.

2. Use row locks.

For example, to create a table t_test, insert several data records. At the beginning of the stored procedure

Select s. idinto v_snfrom t_test s for update;


Commit or rollback at the end of the stored procedure. Of course, to be more secure, you can use exception capture.

Of course, to prevent deadlocks, you can add the wait time after for update. In this way, the stored procedure will report an exception and end at this time to prevent lock for a long time.

The specific script is as follows:

Create or replace procedure test_pro is v_sn varchar2; begin select s. id into v_sn from t_test s for update wait 10; -- 10 s -- self-executed script commit; -- or rollback; exception when others then begin rollback; END; end test_pro;

In this way, you can implement general blocking stored procedures.

Related Article

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.