Wednesday, November 5, 2003 06:49 PM
Deadlocks can be occur when you are inconsistent in the way you access 2 related tables. Consider this:
1. methodA() reads tableA and then tableB. Each table is accessed with a read/write lock.
2. methodB() reads tableB and then tableA. Again each table is accessed with a read/write lock.
If 2 concurrent threads call these methods simultaneously, there will be a deadlock as both threads will require the other to finish before proceding since they have read/write locks. As a general rule, you should always access 2 related tables in the same order. If both methods always access tableA first then tableB second, there will never be a deadlock.
Deadlocks can also occur if you lock more than one table within a transaction. As a general rule, you should consistently use the same table for locking and lock only 1 table. The code above should only lock tableA and access it first.