views:

38

answers:

1

I keep running into "enq: TX - row lock contention", when I run the Sql command below in a oracle 9 DB. The table mytable is a small table, with less than 300 lines.

UPDATE MYTABLE
SET     col1 = col1 + :B3 ,
        col2    = SYSDATE
WHERE   :B2            = col3
    AND :B1        = col4

I run 10 threads at the same time, and some wait as long as 10 seconds to get a chance to update. I know I will face this deadlock issues, but the problem for me is that they should be able to be much faster, since the table is small, so updating it would be fast.

edit:I cannot alter this code, it is inside a third party application. I can only tweak the DB.

what can I do to improve the speed of the rowlock queue? How can I improve this waiting time so my threads run faster?

+2  A: 

Hi kurast,

If you don't want to be blocked don't try to update blindly. Perform a SELECT FOR UPDATE NOWAIT first. If you raise an exception (ORA-00054) this means that another session is currently working with that row. In that case, depending upon your requirement, you could try to update another row (are you trying to build a queue/dequeue process?), wait (dbms_lock.sleep) or return an error message to the calling app.

You will never face a deadlock if you lock the row with FOR UPDATE NOWAIT first.

Vincent Malgrat
forgot to say, but I cannot alter the code. now I edited my question
kurast
@kurast: the update itself probably doesn't take a lot of time. The time is spent waiting for **other sessions** to commit/rollback. If these threads are background batch jobs, maybe the contention is expected/not harmful. If they are active foreground sessions, why are they trying to update the same row ?
Vincent Malgrat