Deadlocking

SusieQ

New Member
Hi All,

I am fairly new at this so would somebody mind giving me a bit of a hand.

I'd appreciate a good definition/example of deadlocking, and how to correct it.

I can't seem to find anything in the manual.

TIA

SusieQ
 

mra

Junior???? Member
Hello Susie!

Deadlock is a situation where two users lock the same tables in opposite order, and thus excluding each other.

Example:

User A
Lock table A.
Do something
Lock table B

User B
Lock table B
Do something
Lock table A

If the two users are executing the code simultaneously they will both wait forever.

Most databases have a way of detecting deadlock. One user will then receive an error message, and his/her transaction will be backed out, but the best way is to always lock tables in the same order, eg. first A then B.

Regards
Mike
 

mra

Junior???? Member
Hi Susie

Hmm!! I'll try to explain.

Locks a managed by the database, and you can acquire them implicitly or explicitly ( I prefer to lock the tables explicitly, update the tables and then release the locks, because then I know what's going on - I think I do anyway).

Locks are essential to multiuser app's because they guarantee consistency in the database. Often you don't have to worry about them, but a few rules apply:
1. Don't lock more than you have to (eg. read with the no-lock option whenever possible).
2. Keep the period you lock data as short as possible (eg. don't read with exclusive-lock before you have to, and read with no-lock whenever possible ).
3. Always lock tables in the same order.

There may be more ...

Deadlocks:
Consider an application where you have two programs that run simultaneously.

Program-1 updates TABLE-A and TABLE-B.
Program-2 updates TABLE-A and TABLE-B.

To prevent other programs from updating the tables while Program-1 is running, the program locks the tables
do transaction:
find first TABLE-A exclusive lock where .......
Do some data processing that takes a while.
find first TABLE-B exclusive lock where .......
Some more data processing
end. /* Transaction */

Program-2 also locks the tables, but in opposite order.
do transaction:
find first TABLE-B exclusive lock where .......
Do some data processing that takes a while.
find first TABLE-A exclusive lock where .......
Some more data processing
end. /* Transaction */


If the programs run simultaneously they will DEADLOCK.

I'll try to make a time diagram, you may have to view it in a non-proportional font.

1. Program-1 | Program-2
2. Program line | Program line
3. Program line | Program line
4. do transaction: | Program line
5. find first TABLE-A exclusive lock where ....... | do transaction:
6. Program line | find first TABLE-B exclusive-lock ...
7. Program line | Program line
8. find first TABLE-B exclusive lock where ....... | Program line
9. Some more data processing | find first TABLE-A exclusive lock ....
10. end. /* Transaction */ | Some more data processing
11. | end. /* Transaction */

In step 8 Program-1 can't lock TABLE-B (It is locked by Program-2 in step 6) and is therefore halted by Progress.
In step 9 Program-2 can't lock TABLE-A because Program-1 locked it instep 5.
The two programs are now deadlocked, and will wait forever.

To monitor locks use the promon utility "Record locking table".

Hope this clears things up for you.

Regards
Mike
 

mra

Junior???? Member
Something ate my tab's

Something ate my tab's. view this in a non-proportional font.

1. Program-1 | Program-2
2. Program line | Program line
3. Program line | Program line
4. do transaction: | Program line
5. find first TABLE-A exclusive lock where ....... | do transaction:
6. Program line | find first TABLE-B exclusive-lock ...
7. Program line | Program line
8. find first TABLE-B exclusive lock where ....... | Program line
9. Some more data processing | find first TABLE-A exclusive lock ....
10. end. /* Transaction */ | Some more data processing
11. | end. /* Transaction */
 
Top