ORACLE FOR UPDATE与FOR UPDATE OF区别

This switched: http: //blog.sina.com.cn/s/blog_61cd89f60102e7di.html

In the production database, we have to ensure that the data reading and writing unique, often encounter the following five kinds of locks statement, the difference in that? Let's use an example to explain:

1、FOR UPDATE

2、FOR UPDATE OF COLUMN

3、FOR UPDATE WAIT

4、FOR UPDATE NOTWAIT

5、FOR UPDATE NO WAIT SKIP LOCK

Let's look at a few examples below:

1, SELECT * FROM EMP FOR UPDATE; - locking all rows of the table, only to be read but not write

Copy the code
DECLARE. 1 
2 Cursor emp_cur 
. 3 IS 
. 4 SELECT * from EMP for Update; - lock all the rows, only read but not written 
. 5 the begin 
. 6 for X in emp_cur Loop 
. 7 = 9999 Update EMP WHERE SAL SET Current of emp_cur; 
. 8 End Loop ; 
9 End;
Copy the code

2, SELECT * FROM EMP WHERE DEPTNO = 10 FOR UPDATE; - only 10 rows of locking DEPTNO = 

Copy the code
1 declare
2     cursor emp_cur 
3     is
4     SELECT * FROM EMP WHERE DEPTNO = 10 FOR UPDATE ;    --只锁定DEPTNO = 10 的行 
5 begin
6     for x in emp_cur loop 
7         update emp set sal = 9999 where current of emp_cur ;
8     end loop ;
9 end ;
Copy the code

3, SELECT * FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO FOR UPDATE - locks all records two tables

4, SELECT * FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.DEPTNO = 10 FOR UPDATE; - locking two rows of the table satisfy the condition

5, SELECT * FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.DEPTNO = 10 FOR UPDATE OF E.DEPTNO; - EMP table row lock will satisfy the condition

FOR UPDATE you can see all the tables are locked, while the FOR UPDATE OF locked behind corresponding table according to the conditions OF

 

The first point:

For the single-table operation terms, FOR UPDATE and FOR UPDATE OF is the same . Without conditions is to lock the whole table, plus the condition of row-level locking . For chestnut:

1, without the WHERE condition is a full table lock

SELECT * FROM EMP FOR UPDATE; - to lock the whole table

2, plus WHERE conditions for row-level locking

SELECT * FROM EMP WHERE DEPTNO = 10 FOR UPDATE; - only 10 departments to meet the conditions of the lock

Remind FOR UPDATE and FOR UPDATE OF again for single-table operation is the same.

So FOR UPDATE and FOR UPDATE OF Where does the difference? Conducting multi-table queries FOR UPDATE OF column of the table where the only lock to lock, but the table-level locking, FOR UPDATE lock is more than one table, do not know look at an example:

1、SELECT * FROM EMP E ,DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.DEPTNO = 10 FOR UPDATE OF E.DEPTNO ;

- Lock table E.DEPTNO only where the locking DEPTNO and only 10 rows =

2、SELECT * FROM EMP E , DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.DEPTNO = 10 FOR UPDATE ;  

- lock multiple tables

The difference between 1 and 2 is that an EMP lock Table 2 and Table EMP and DEPT To lock the table, this is the real difference between the two. FOR UPDATE i.e., all the tables are locked, while the corresponding locking FOR UPDATE OF table is based on a condition 

 

The second point: About NOWAIT (If you must use FOR UPDATE, more recommended NOWAIT)

1, when there is an error and LOCK conflict rather than ending STATEMENT there waiting (for example: to check the line has been locked by other transactions, the current lock transaction with conflict, with nowait, the current transaction will be ended an error and an immediate end STATEMENT no longer wait).

2, WAIT clause specifies the wait for another user to release the lock of seconds, to prevent an indefinite wait.

"Use FOR UPDATE WAIT" clause of the following advantages:
  1. Prevention wait indefinitely locked row;
  2. To allow the application of more control wait time locks.
  3. Very useful for interactive applications, because users can not wait for these uncertain
  4. If you use a skip locked, the lock can cross the line, does not report triggered by the wait n 'resource busy' exception reporting

Guess you like

Origin www.cnblogs.com/turnip/p/11639358.html