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.