Question

I've got two Oracle queries running in different sessions, which are deadlocking, and I'm having trouble seeing why that's happening.

The query in session 1 is this:

UPDATE REFS R SET R.REFS_NAME = :B2 WHERE R.REFS_CODE = :B1 

The query in session 2 is this:

UPDATE REFS R SET R.STATUS_CODE = :B3, R.STATUS_TYPE = :B2 WHERE R.REFS_CODE = :B1 

Each is surrounded by a cursor that loops through a selection of primary key values. When these queries are run at the same time, they deadlock. REFS_CODE is the primary key, and the Oracle trace shows that they're updating different rowids. The primary key is indexed, obviously, and there are some foreign key constraints, which are supported by indexes as this has been a problem for us in the past.

Moving into the realm of desperation, I've tried disabling triggers on the table, and that didn't help. Also tried using autonomous transactions, but that made things much worse.

Is there something I'm missing? Thanks for any help!

Was it helpful?

Solution

If a commit is happening after the entire cursor batch is updated, then it may just be a straight forward deadlocking scenario where the two cursors are operating on the same rows but in a different order.

  • Assume session 1 has cursor set 1 and is updating refs_code 1 and refs_code 2 in that order before attempting a commit.
  • Assume session 2 has cursor set 2 and is updating refs_code 2 and refs_code 1 in that order before attempting a commit.

Then, interleaving the updates:

time  cursor set 1    cursor set 2
====  ============    ============

t1    refs_code 1     -
t2    -               refs_code 2
t3    refs_code 2     -
t4    -               refs_code 1

at t3, cursor set 1 is waiting on cursor set 2 to commit refs_code 2 at t4, cursor set 2 is waiting on cursor set 1 to commit refs_code 1

The two transactions are waiting on different rowids. If that is the case, you may be able to add an order by (in the same direction) to both cursors to help avoid this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top