• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            Select For Update行級鎖定

            Posted on 2010-03-17 23:42 Prayer 閱讀(303) 評論(0)  編輯 收藏 引用 所屬分類: 數據庫,SQL
            OracleSelect For Update語句可以實現在讀取數據后馬上鎖定相關資源,防止被其他session修改數據的目的。也就是我們常常談到的“悲觀鎖定”(現實應用開發(fā)中,使用悲觀鎖定的情況少之又少,也許是因為樂觀鎖定的實現更加靈活和便捷的緣故)。
            這個小文兒做一個小小的實驗,來看看Select For Update語句實現的行級鎖定

            1.創(chuàng)建實驗表table_sfu,并初始化三條數據
            sec@ora10g> create table table_sfu (a number);

            Table created.

            sec@ora10g> insert into table_sfu values (1);

            1 row created.

            sec@ora10g> insert into table_sfu values (2);

            1 row created.

            sec@ora10g> insert into table_sfu values (3);

            1 row created.

            sec@ora10g> commit;

            Commit complete.

            sec@ora10g> select * from table_sfu;

                     A
            ----------
                     1
                     2
                     3

            2.使用Select For Update語句得到第一條數據
            sec@ora10g> select * from table_sfu where a = 1 for update;

                     A
            ----------
                     1

            3.查看一下現在系統(tǒng)中的鎖定情況,152會話(即上面語句所在的會話)獲得了一個TX鎖和一個TM鎖了,鎖定的表就是TABLE_SFU
            sec@ora10g> @lock

            lock        lock
            holder    holder         lock             lock request       blocked
            username  sessid SERIAL# type    id1  id2 mode    mode BLOCK  sessid
            -------- ------- ------- ---- ------ ---- ---- ------- ----- -------
            SEC          152   14985 TM    15396    0    3       0     0
            SEC          152   14985 TX   327722 1790    6       0     0
                         164       1 TS        3    1    3       0     0
                         165       1 CF        0    0    2       0     0
                         165       1 RS       25    1    2       0     0
                         165       1 XR        4    0    1       0     0
                         166       1 RT        1    0    6       0     0

            7 rows selected.

            sec@ora10g> col OWNER for a6
            sec@ora10g> col OBJECT_NAME for a10
            sec@ora10g> select OWNER,OBJECT_NAME,OBJECT_ID,OBJECT_TYPE from dba_objects where object_id = '15396';

            OWNER  OBJECT_NAM  OBJECT_ID OBJECT_TYPE
            ------ ---------- ---------- -------------------
            SEC    TABLE_SFU       15396 TABLE

            4.另外新打開一個session,執(zhí)行以下修改任務
            sec@ora10g> update table_sfu set a = 100 where a = 1;
            OK,效果出現了,這里出現了“鎖等待”現象,原因就是因為在第一個session中使用Select For Update語句鎖定了第一行數據,不允許其他的session對它修改。

            5.這時系統(tǒng)中鎖定情況如下,可以看到第一個session(session id是152)會話鎖定了第二個session(session id是145)會話的事務
            sec@ora10g> @lock

            lock        lock
            holder    holder         lock             lock request       blocked
            username  sessid SERIAL# type    id1  id2 mode    mode BLOCK  sessid
            -------- ------- ------- ---- ------ ---- ---- ------- ----- -------
            SEC          145   11388 TM    15396    0    3       0     0
            SEC          152   14985 TM    15396    0    3       0     0
            SEC          152   14985 TX   327722 1790    6       0     1     145
                         164       1 TS        3    1    3       0     0
                         165       1 CF        0    0    2       0     0
                         165       1 RS       25    1    2       0     0
                         165       1 XR        4    0    1       0     0
                         166       1 RT        1    0    6       0     0

            8 rows selected.

            6.因為僅僅是鎖定了第一條數據,所以其他記錄可以順利的進行修改,如下
            sec@ora10g> update table_sfu set a = 200 where a = 2;

            1 row updated.

            sec@ora10g> commit;

            Commit complete.

            7.解鎖方式:commit或rollback后即完成鎖定的接觸

            8.反過來思考一下,如果Select For Update與要鎖定的行已經在其他session中完成了修改,再執(zhí)行回出現什么效果呢?這個很顯然,同樣的會出現“鎖等待”的現象,不過我想強調的是,這里可以使用nowait和wait選項來進行“探測”待鎖定行是否可被鎖定
            實驗效果如下:
            第一個session:
            sec@ora10g> update table_sfu set a = 100 where a = 1;

            1 row updated.

            第二個session:
            sec@ora10g> select * from table_sfu where a = 1 for update;
            此處是“鎖等待”效果

            sec@ora10g> select * from table_sfu where a = 1 for update nowait;
            select * from table_sfu where a = 1 for update nowait
                          *
            ERROR at line 1:
            ORA-00054: resource busy and acquire with NOWAIT specified
            這里提示了錯誤,原因就是已經“探測”到該行已經被別的事務鎖定,這里無法對其進行鎖定操作。

            sec@ora10g> select * from table_sfu where a = 1 for update wait 3;
            select * from table_sfu where a = 1 for update wait 3
                          *
            ERROR at line 1:
            ORA-30006: resource busy; acquire with WAIT timeout expired
            這里提示的錯誤內容與上面的一樣,不過這里wait 3表示,我等你三秒的時間,如果三秒過后還無法鎖定資源,就報錯。

            9.更進一步,請參考Oracle官方文檔中相關的描述
            《for_update_clause ::=》
            http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2126016
            語法格式:
            FOR UPDATE
            [ OF [ [ schema. ]
                   { table | view } . ]column
                   [, [ [ schema. ]
                        { table | view } . ]column
                   ]...
            ]
            [ NOWAIT | WAIT integer ]

            同上述連接,搜索關鍵字“for_update_clause”可以得到每個選項的解釋信息

            《Using the FOR UPDATE Clause: Examples 》
            http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2130052

            10.小結
            上面的小實驗展示了一下Select For Update行級鎖定的效果,Oracle的鎖定機制還是非常的靈活的,基于這個鎖定可以實現“悲觀鎖定”。
            99久久国产热无码精品免费久久久久| 91久久精品国产91性色也| 国产一级做a爰片久久毛片| 国产精品美女久久久久网| 大香网伊人久久综合网2020| 婷婷综合久久狠狠色99h| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 97久久婷婷五月综合色d啪蜜芽| 久久亚洲2019中文字幕| 欧美亚洲国产精品久久| 久久精品九九亚洲精品| 国产精品免费久久| 久久亚洲中文字幕精品有坂深雪 | 午夜精品久久久久久99热| 97久久久久人妻精品专区| 久久综合九色综合久99| 伊人久久亚洲综合影院| 久久久青草久久久青草| 一级a性色生活片久久无| 97久久天天综合色天天综合色hd| 中文字幕无码久久精品青草| www.久久热.com| 国产aⅴ激情无码久久| 一级做a爰片久久毛片看看| 欧美一区二区精品久久| 国产精品久久久天天影视| 国产成人精品综合久久久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 人妻少妇精品久久| 久久r热这里有精品视频| 亚洲va久久久噜噜噜久久男同 | 久久99精品久久久久子伦| 精品伊人久久久| 久久久www免费人成精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 99久久精品国产一区二区| 国产亚州精品女人久久久久久 | 麻豆精品久久精品色综合| 久久er99热精品一区二区| 久久精品国产亚洲AV大全| 精品久久无码中文字幕|