table lock mode的分类为5种:
s, x, rx, rs, srx
s:share;
x;exclusive;
r:row.
ROW SHARE 行共享,允许其他用户同时更新其他行,允许其他用户同时加共享锁,不允许有独占(排他性质)的锁
ROW EXCLUSIVE
行排他,允许其他用户同时更新其他行,不允许其他用户同时加行共享锁或者行排他锁
SHARE 共享,不允许其他用户同时更新任何行,只允许其他用户同时加共享锁或者行共享锁
SHARE ROW EXCLUSIVE
共享行排他,允许其他用户同时更新其他行,只允许其他用户同时加行共享锁
EXCLUSIVE 排他,其他用户禁止更新任何行,禁止其他用户同时加任何锁
总结:名称中带ROW的锁的共性是允许其他用户同时更新不同行,
根据是否允许其他用户同时加锁的种类不同而分不同类型
如果不希望别的session lock/insert/update/delete表中任意一行,只允许查询,
可以用lock table table_name in exclusive mode。(X)这个锁定模式级别最高,并发度最小。
如果允许别的session查询或用select for update锁定记录,不允许insert/update/delete,
可以用lock table table_name in share row exclusive mode。(SRX)
如果允许别的session查询或select for update以及lock table table_name in share mode,
只是不允许insert/update/delete,可以用lock table table_name in share mode。
(share mode和share row exclusive mode的区别在于一个是非抢占式的而另一个是抢占式的。
进入share row exclusive mode后其他session不能阻止你insert/update/delete,
而进入share mode后其他session也同样可以进入share mode,进而阻止你对表的修改。(S)
还有两种锁定模式,row share(RS)和row exclusive(RX)。他们允许的并发操作更多,
一般直接用DML语句自动获得,而不用lock语句。
以下来自Oracle Concepts:
A table lock can be held in any of several modes: row share (RS), row exclusive (RX), share (S), share row exclusive (SRX), and exclusive (X). The restrictiveness of a table lock's mode determines the modes in which other table locks on the same table can be obtained and held.
Lock Modes Permitted?
SQL Statement | Mode of Table Lock | RS | RX | S | SRX | X |
SELECT...FROM table... | none | Y | Y | Y | Y | Y |
INSERT INTO table ... | RX | Y | Y | N | N | N |
UPDATE table ... | RX | Y* | Y* | N | N | N |
DELETE FROM table ... | RX | Y* | Y* | N | N | N |
SELECT ... FROM table FOR UPDATE OF ... | RS | Y* | Y* | Y* | Y* | N |
LOCK TABLE table IN ROW SHARE MODE | RS | Y | Y | Y | Y | N |
LOCK TABLE table IN ROW EXCLUSIVE MODE | RX | Y | Y | N | N | N |
LOCK TABLE table IN SHARE MODE | S | Y | N | Y | N | N |
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE | SRX | Y | N | N | N | N |
LOCK TABLE table IN EXCLUSIVE MODE | X | N | N | N | N |
+++++++++++++++++++++++++++++++++
OCP试题
+++++++++++++++++++++++++++++++++
User SCOTT executes the following command on the EMP table but has not issued COMMIT,
ROLLBACK, or any data definition language (DDL) command:
SQL> SELECT job FROM emp
2 WHERE job='CLERK' FOR UPDATE OF empno;
SCOTT has opened another session to work with the database. Which three operations would wait when
issued in SCOTT's second session? (Choose three.)
A. LOCK TABLE emp IN SHARE MODE;
B. LOCK TABLE emp IN EXCLUSIVE MODE;
C. DELETE FROM emp WHERE job='MANAGER';
D. INSERT INTO emp(empno,ename) VALUES (1289,'Dick');
E. SELECT job FROM emp WHERE job='CLERK' FOR UPDATE OF empno;
Answer: ABE
User Scott has updated the salary of one of the employees in the EMPLOYEES table and has not
committed the transaction. What are the two types of locks that this scenario would lead to? (Choose
two.)
A. null lock on the row being updated
B. null lock on the table containing the row
C. ROW SHARE lock for the row being updated
D. ROW EXCLUSIVE lock for the row being updated
E. shared rowexclusive lock for the row being updated
F. a shareable table lock for the table containing the row
G. exclusive tablelevel lock for the table containing the row
Answer: DF
The session of user SCOTT receives the following error after executing an UPDATE command on
the EMP table:
ERROR at line 1:
ORA00060: deadlock detected while waiting for resource
On investigation, you find that a session opened by user JIM has a transaction that caused the deadlock.
Which two statements are true regarding the session of SCOTT in this scenario? (Choose two.)
A. It is terminated after receiving the error and JIM can continue with his transaction.
B. The transaction in it is terminated after receiving the error and JIM can continue with his transaction.
C. SCOTT should perform a COMMIT or ROLLBACK to allow JIM to continue with his transaction.
D. SCOTT has to reexecute the last command in the transaction after he commits the transaction.
Answer: CD
User A executes the following command to update the TRANS table:
SQL> UPDATE B. trans SET tr_amt=tr_amt+500 WHERE c_code='C005';
Before user A issues a COMMIT or ROLLBACK command, user B executes the following command on
the TRANS table:
SQl> ALTER TABLE trans MODIFY (tr_type VARCHAR2(3));
What would happen in this scenario?
A. The transaction for user A is rolled back.
B. The ALTER TABLE command modifies the column successfully.
C. The ALTER TABLE command fails due to the resource being busy.
D. The ALTER TABLE command waits until user A ends the transaction.
Answer: C