The Difference and use of for update and for update nowait, updatenowait

Source: Internet
Author: User

The Difference and use of for update and for update nowait, updatenowait

First, for update and for update nowait are used to lock the operated data rows and prevent other operations from modifying the data before the transaction is committed.

The main difference between for update and for update nowait is whether to wait. If nowait is not added, an error will be reported when the select statement is executed. If nowait is added, the select statement will wait, the lock is released.

First, we use two SQL statements:

1. select * from HH t where id = '1' for update

2. select * from HH t where id = '1' for update nowait

Sql1 is executed in pl/SQL, and sql2 is executed in ob12 (two windows in pl/SQL or ob12 cannot be executed, and I don't know why ):

After sql1 is executed, the correct information is queried. When sql2 is executed, the error message "ORA-00054: the resource is busy, but it specifies that the resource is obtained in NOWAIT mode, or timeout is invalid ". This is because the data to be modified is locked when sql1 is executed, and other operations cannot be accessed. After SQL 1 is executed, SQL 2 displays the correct data.

Replace sql2 with for update and perform the preceding steps. sql2 will wait until the lock is released until after sql1 is commit and sql2 will be able to query the data;

Here there is also for update wait n (n is time, unit: seconds), that is, waiting for n seconds, if the data is locked after n seconds, the above error will be reported;

In fact, for update is to prevent data modification when querying data. for example, there are two SQL statements:

Sql1: select * from HH t where id = '1' for update

Sql2: update HH set name = 'zhang san' where id = '1'

After sql1 is executed, sql2 and sql2 will wait until sql1 releases the lock before execution. In this way, no data changes will be made during the query and commit will be executed after sql1, sql2 is automatically executed.

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.