By some strange coincidence, the “London Bus” effect perhaps, there have been three posts on the OTN database forum in the last couple of days relating to deadlocks; and they have prompted me to indulge in a little rant about the myth of Oracle and deadlock detection; it’s the one that goes:
“Oracle detects and resolves deadlocks automatically.”
Oracle may detect deadlocks automatically, but it doesn’t resolve them, it simply reports them (by raising error ORA-00060 and rolling back one statement) then leaves the deadlocked sessions stuck until the session that received the report resolves the problem or an external agent resolves the problem.
Consider the following example (which, I have to admit, I wrote without access to a live instance):
Session 1 delete from t1 where id between 1 and 1000000; 1000000 rows deleted Session 2 update jobs set status = 'STARTING' where id = 99; 1 row updated Session 1 update jobs set status = 'FINISHED' where id = 99; -- session 1 is now waiting on session 2 Session 2 delete from t1 where id between 1 and 1000000 -- session 2 is now waiting on session 1 Session 1 (some time within the next 3 seconds) ORA-00060: deadlock detected (with rollback of the most recent statement - i.e. the update)
On receiving the ORA-00060 error most of the applications that I’ve seen would then crash session 1, resulting in an implicit rollback of the million row delete (worst case I’ve seen: session 2 ran for 10 seconds and resulted in session 1 crashing and rolling back for 3.5 hours); some applications would log “Unexpected Oracle error” to the front-end and retry the most recent action (the update that rolled back) at which point Session 2 would receive an ORA-00060 error and the two sessions would see-saw back and fore every 3 seconds until someone noticed what was going on.
In what way has the deadlock been “resolved” ?
In this specific case I think I’d probably want a supervisor (person or program) to log and kill session 2 and allow session 1 to retry its second update – and then I’d want to find out why this sequence of events had happened at all.
Deadlocks are NOT resolved automatically by Oracle, they are merely reported so that the client code can decide how to resolve them.