Oracle neutral select for update

Under normal circumstances, the select statement will not lock the data and will not interfere with other DML and DDL operations. With the for update clause, we can manually implement data lock protection operations at the application level. for update is useful when only one session is allowed to update.

After select...for update , you can use the of clause to select and lock the specific data table of the select. By default, no of clause is used to lock all data tables in the select .

 

select * from test for update; will lock the table test. At this time, only the current session is allowed to update the existing data. But other sessions can still perform the insert operation.

 

select * from Table1 a join Table2 b on a.pkid=b.pkid where a.pkid = 10 for update of a.pkid only locks the rows in Table1 that meet the conditions, which is the role of using the of clause. It is more commonly used in multiple operation of a table.

 

 

After adding for update , Oracle requires to start a new transaction to try to lock the data. If it is currently locked, the default behavior must be block wait. The function of using the nowait clause is to avoid waiting. When it is found that the requested lock resource is locked and not released, it will report an error and return directly. If the nowait or wait clause is not used, the new lock request will hang until the original commit or rollback.

 

 

select * from test where a=2 for update nowait;
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

 

Or
select * from test where a=2 for update wait 3; If the lock cannot be locked within 3 seconds, an error will be returned.

 

 

In a session, you can select XX for update multiple times, and then you only need to commit or rollback once to release.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711037&siteId=291194637
Recommended