OCP-1Z0-051 第157题 事务和锁

来源:互联网 发布:微博修改个性域名 编辑:程序博客网 时间:2024/06/01 22:57
一、原题
View the Exhibit and examine the structure of the ORDERS and CUSTOMERS tables.

Evaluate the following SQL command:
SQL>SELECT o.order_id, c.cust_name, o.order_total, c.credit_limit
      FROM orders o
      JOIN customers c
     USING (customer_id)
     WHERE o.order_total > c.credit_limit
       FOR UPDATE
     ORDER BY o.order_id;
Which two statements are true regarding the outcome of the above query? (Choose two.)
A. It locks all the rows that satisfy the condition in the statement.
B. It locks only the columns that satisfy the condition in both the tables.
C. The locks are released only when a COMMIT or ROLLBACK is issued.
D. The locks are released after a DML statement is executed on the locked rows.

答案:AC

二、题目翻译
查看ORDERS和CUSTOMERS表的结构
评估下面的SQL命令:
关于上面查询的结果哪两句话是正确的?(选择2项)
A.锁定语句中满足条件的所有行。
B.只锁定两个表中满足条件的列。
C.只有执行COMMIT或ROLLBACK后,锁才会释放。
D.在锁定的行上执行DML语句后,锁被释放。

三、题目解析
A选项正确,select .. for update语句,就是锁定select出来的所有行,以防止其它会话变更。
B选项不正确,oracle中有行锁和表锁,没有列锁,会将相关的行都锁住。

C选项,答案中是正确,但是这个说法,个人认为不是太完整。
            commit和rollback后,锁的确可以被释放,
            但如果说只有在commit或rollback之后才能释放锁,就有些不恰当,因为只要事务结束,锁就会被释放,而事务结束,不光只有commit和rollback,比如,还有进行DDL操作的时候,或用户正常或不正常退出的时候,都会结束事务,事务结束,锁就会被释放。

D选项不正确,在当前会话中,被锁定的行上可以做DML操作,但仍然不会释放锁,如果在其它会话中在被锁定的行上做DML操作,则会被阻塞。

四、测试
       关于C选项,测试如下,选择其中一种情况,DDL的时候,会自动提交事务,并释放锁。

会话1:
SQL> select sid from v$mystat where rownum=1;

       SID
----------
       158

SQL> select * from emp where ename='SCOTT' for update;

     EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
      7788 SCOTT                ANALYST                  7566 19-APR-87          3000                    20

会话2中查锁的相关信息:
SQL> set linesize 150
SQL> column o_name format a10
SQL> column lock_type format a20
SQL> column object_name  format a15
SQL> select sid,type,id1,id2,
  2     decode(lmode,0,'None',
  3         1,'Null',
  4         2,'Row share',
  5         3,'Row Exclusive',
  6         4,'Share',
  7         5,'Share Row Exclusive',
  8         6,'Exclusive') lock_type,
  9     request,ctime,block
 10  from v$lock
 11  where sid=158;

       SID TYPE        ID1        ID2 LOCK_TYPE               REQUEST      CTIME      BLOCK
---------- ---- ---------- ---------- -------------------- ---------- ---------- ----------
       158 TM        58060          0 Row Exclusive                 0         75          0
       158 TX        65540       5023 Exclusive                     0         75          0

会话1中,继续进行DDL操作:
SQL> create table test_lock(id int);
Table created.

会话2中再查锁的相关信息,就没有了,说明DDL操作也会导致事务结束,这里是自动提交了:
SQL>  select sid,type,id1,id2,
  2      decode(lmode,0,'None',
  3          1,'Null',
  4          2,'Row share',
  5          3,'Row Exclusive',
  6          4,'Share',
  7          5,'Share Row Exclusive',
  8          6,'Exclusive') lock_type,
  9      request,ctime,block
 10   from v$lock
 11   where sid=158;

no rows selected.

0 0