Difference between ORACLE FOR UPDATE and FOR UPDATE OF (reproduced)

Original address: http://blog.sina.com.cn/s/blog_61cd89f60102e7di.html

 

In the production database, in order to ensure the uniqueness of data reading and writing, we often encounter the following five lock statements. What is the difference? I believe that for novices, there is not much to understand. Let's illustrate with an example:

    1.FOR UPDATE、

    2.FOR UPDATE OF COLUMN、

    3.FOR UPDATE WAIT 3、

    4.FOR UPDATE NOWAIT、

    5.FOR UPDATE NO WAIT SKIP LOCK

 

Let's look at a few examples:

  1.select * from Table for update --lock all rows of the table, only read but not write
  2.select * from Table where pkid = 1 for update --lock only rows with pkid=1
  3.select * from Table a join Table b on a.pkid=b.pkid for update-- lock all records of both tables
  4.select * from Table a join Table b on a.pkid=b.pkid where a.pkid = 10 for update--lock two

      --The rows in the table that meet the conditions
  5. select * from Table1 a join Table b on a.pkid=b.pkid where a.pkid = 10 for update of a.pkid Only lock the rows that meet the conditions in Table1


  for update is to lock all tables for update of lock the corresponding table according to the conditions of the table after of

 

The first point:

    For a single table read and write operations, FOR UPDATE and FOR UPDATE OF COLUMN are the same two statements. Without conditions, the whole table is locked, and with conditions, it is a row-level lock . Let's take an example.

    SELECT * FROM TABLE FOR UPDATE

    SELECT * FROM TABLE FOR UPDATE OF TABLE_COLUMN

    The above two do not add the WHERE condition, it is to lock the whole table

    SELECT * FROM TABLE WHERE TABLE_COLUMN='条件' FOR UPDATE[FOR UPDATE OF COLUMN]

    At this time, query conditions are added to the query, which is a row-level lock on the records that meet the conditions in the table.

 

    Again, FOR UPDATE and FOR UPDATE OF are the same for single table operations

 

    So what is the difference between FOR UPDATE OF and FOR UPDATE? When operating on multiple tables, FOR UPDATE OF only locks the table where the column is located, and it is a table-level lock. FOR UPDATE locks multiple tables, not understand examples

   1.select * from Table1 a join Table b on a.pkid=b.pkid where a.pkid = 10 for update of a.pkid

   2.select * from Table1 a join Table b on a.pkid=b.pkid where a.pkid = 10 for update

   The difference between 1 and 2 is that 1 only locks table a, while 2 locks both tables. This is the real difference between the two, that is, for update is

   Lock all tables for update of lock the corresponding table according to the conditions of the table after of

 

The second point: about NOWAIT (if you must use FOR UPDATE, I recommend adding NOWAIT)


    When there is a LOCK conflict, it will prompt an error and end the STATEMENT instead of waiting there (for example: the row to be checked has been locked by other transactions, the current lock transaction conflicts with it, plus nowait, the current transaction will end and prompt an error And end the STATEMENT immediately without waiting. The
    WAIT clause specifies the number of seconds to wait for other users to release the lock, preventing an indefinite wait.
  The advantages of the "use FOR UPDATE WAIT" clause are as follows:
  1. Prevents waiting for locked rows indefinitely;
  2. Allows the application to have more control over the waiting time of the lock.
  3. Very useful for interactive applications, as these users cannot wait indefinitely
  4. If skip locked is used, it is possible to skip over locked rows and will not report the 'resource busy' exception reported by wait n

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326496770&siteId=291194637