2015-08-28 65 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