Select statement for update --- reprint

Source: Internet
Author: User

Purpose:

Select... The syntax of the For Update statement is the same as that of the select statement, but the for update [NOWAIT] clause is added after the select statement.

This statement is used to lock specific rows (if a where clause exists, it is the rows that meet the where condition ). When these rows are locked, you can select
These rows, but cannot be changed or deleted until the transaction of the statement ends with a commit or rollback statement.

From: http://blog.163.com/yiyun_8/blog/static/100336422201031505832337/

The meaning of nowait is often interpreted as "Don't wait, execute now ". But in fact, this keyword indicates "Don't wait, return immediately"
If the requested resource is locked by another session, it will be blocked. nowait can avoid this blocking because
If another user is in the process of modifying that row, we will get an ORA ‐00054
Resource Busy error. We are blocked and must wait for the other user to finish
It.
In this experiment, I used pl/SQL developer to lock the table game.
SQL> select * from game where game_id = 1;
Returns a record.
 

SQL> select * from game where game_id = 1 for update nowait;
Select * from game where game_id = 1 for update nowait
             *
ERROR is located in row 1st:
ORA-00054: resource busy, requirement to specify NOWAIT
When the NOWAIT keyword is used, the error "ORA-00054" is reported.
 

How can I check what resources cause such a situation? How can this problem be solved?
View locked objects, users, and Sessions
SQL> select lo. oracle_username, do. object_name, s. logon_time, lo. process, s. sid as s
Ession_id
2 from v $ locked_object lo, v $ session s, dba_objects do
3 where lo. session_id = s. sid and do. object_id = lo. OBJECT_ID
4/
ORACLE_USERNAME
------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
 

LOGON_TIME PROCESS SESSION_ID
-----------------------------------------
NBA --- user name
GAME --- operation object
10:55:15 --- logon Time: 840: 5176 10
 

Use as sysdba
View specific SQL statements based on sid
Selectsql_textfromv $ session a, v $ sqltext_with_newlines B
WhereDECODE (a. SQL _hash_value, 0, prev_hash_value, SQL _hash_value) = B. hash_value
Anda. sid = 10;
Begin: id: = sys. dbms_transaction.local_transaction_id; end;
 

Kill session
SQL> select sid, serial # from v $ session where sid = 10;
 

Sid serial #
--------------------
10 23
SQL> alter system kill session '10, 23 ';
 

The system has been changed.
 

Select * from game where game_id = 1 for update nowait;
Data returned

When two users update the same record at the same time, the use of select for update will be blocked after the executor, and the use of select for update nowait will throw: ORA-00054 resource busy and acquire with NOWAIT specified exception, telling the user this line is locked.

Select statement for update --- reprint

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.