国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

pessimistic鎖定對optimistic鎖定(1)

2024-07-21 02:34:19
字體:
來源:轉載
供稿:網友
    pessimistic (悲觀)locking和optimistic (樂觀)locking是Oracle保證數據并行訪問和避免lost update的2種策略.     pessimistic (悲觀)locking是指用戶在讀取和更新數據的時候,悲觀的認為這期間其他用戶可能會更新同一記錄;因此在更新數據前讀取數據的時候,嘗試獲得該記錄的絕對鎖,假如其他用戶已經鎖住該紀錄,則被阻塞用戶可以選擇等待(select for upodate)還是取消等待(select for update nowait)。從而避免lost update,保證了數據一致性。    optimistic (樂觀)locking是指用戶在讀取和更新數據的時候,樂觀的認為這期間其他用戶不可能更新同一記錄,在更新數據前讀取數據的時候,不會獲得該記錄的絕對鎖;因此在更新紀錄的時候,其他用戶很可能已經修改過了該紀錄;因此,在更新的時候需要采取自定義編碼策略來避免lost update,保證數據一致性。    演示lost update的例子    At year end, Manager Ian decide to increase engineer SHERSA’s salary by 500$;while HR Officer need to increase each engineer’s salary by 5%.
    Time1: Ian read SHERSA’s salary record:
    SQL> select * from emp where name=' SHERSA ';    ID NAME SALARY    ---------- ------------------------- ----------    8 SHERSA 3000
    Time2: HR read SHERSA’s salary record:
    SQL> select * from emp where name='SHERSA';    ID NAME SALARY    ---------- ------------------------- ----------    8 SHERSA 3000
    Time3: HR update SHERSA’s salary.
    SQL> update emp set salary=salary*1.05 where id=8;    SQL> commit;    SQL> select * from emp where name='SHERSA';    ID NAME SALARY    ---------- ------------------------- ----------    8 SHERSA 3150
    Time4: Ian update SHERSA’s salary by increment 500    SQL> update emp set salary=3500 where id=8;    SQL> commit;    SQL> select * from emp where name='SHERSA';    ID NAME SALARY    ---------- ------------------------- ----------    8 SHERSA 3500    Ian更新的數據覆蓋了HR更新的紀錄,導致丟失更新(lost update);SHERSA受到了經濟損失。    為了避免lost update,需要pessimistic locking 或者optimistic locking。    pessimistic locking 策略    通過在讀紀錄時,采用select for update鎖住該紀錄,再進行更新。    Time1: Ian read SHERSA’s salary record:
    SQL> select * from emp where name=' SHERSA ' for update;    ID NAME SALARY    ---------- ------------------------- ----------
    8 SHERSA 3000
    Time2: HR read SHERSA’s salary record:
    SQL> select * from emp where name= ' SHERSA ' for update;    無法獲得行上的絕對鎖,被另外的session Ian阻塞    Time3: HR has to wait …. Or cancel Operation by using “for update nowait”
    SQL> select * from emp where name='SHERSA' for update nowait;    select * from emp where name='SHERSA' for update nowait    *    ERROR at line 1:    ORA-00054: resource busy and acquire with NOWAIT specified
    Get “resource busy”錯誤
    Time4: Ian update SHERSA’s salary by increment 500    SQL> update emp set salary=3500 where id=8;    SQL> commit;    SQL> select * from emp where name='SHERSA';    ID NAME SALARY    ---------- ------------------------- ----------    8 SHERSA 3500
    Time5: HR update SHERSA’s salary    SQL> update emp set salary=salary*1.05 where id=8;    SQL> commit;    SQL> select * from emp where id=8;    ID NAME SALARY    ---------- ------------------------- ----------    8 SHERSA 3675

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹巴县| 泰安市| 嫩江县| 洛川县| 新田县| 错那县| 莎车县| 民和| 丘北县| 汉源县| 南昌市| 泸溪县| 二连浩特市| 大宁县| 新和县| 金寨县| 云南省| 曲靖市| 南溪县| 贵港市| 延川县| 双桥区| 宾阳县| 岑溪市| 三门县| 虎林市| 康平县| 高平市| 澎湖县| 西畴县| 当涂县| 永春县| 达州市| 鄂托克前旗| 台湾省| 开原市| 明溪县| 高清| 通辽市| 阳原县| 丰原市|