2015-08-28 63 views
1

我嘗試通過c#更新記錄。它工作正常,但如果其他人編輯相同的記錄並且不提交或回滾它(因此事務仍處於打開狀態),程序將凍結,直到它被提交或回滾。它不是問題,但我不想讓程序凍結。它應該打印一個錯誤或其他東西。捕獲鎖定的oracle sql記錄c#

有什麼線索可以抓到它嗎?

回答

1

在你的情況,你必須

  1. 嘗試鎖定進行更新(獨佔鎖)記錄
  2. 如果成功,更新他們在同一事務

所以,而不是隻是更新你應該執行這樣的事情:

select 1 
    from MyTable 
    where id = :prm_Id -- the same condition as in the update part 
    for update nowait; -- throw exception if the record is locked; 
         -- "skip locked" is an alternative Oracle 11g behaviour 

    update MyTable 
    set myField = :prm_myField 
    where id = :prm_Id; 

    commit; -- or continue the transaction