Product Information > JADE .NET Developer’s Reference > Chapter 2 - Object Management > Single Object Deadlocks

Single Object Deadlocks

A deadlock can occur with only one object being involved. For example, this can happen in conjunction with lock type upgrades, as follows.

The initial situation is:

The attempted actions are:

The outcome is:

This type of deadlock occurs with the following sort of code sequence.

TimeSpan timeOut = TimeSpan.FromSeconds(5);
using ( System.Data.IDbTransaction tran = context.BeginTransaction())
{
    context.Lock(obj1, LockType.Shared, LockDuration.Transaction, timeOut);
    //...
    context.Lock(obj1, LockType.Exclusive, LockDuration.Transaction, timeOut);
    //...
    tran.Commit();
}

Two JADE sessions simultaneously executing this sequence using the same JADE object can get in a deadlock situation.

This type of sequence can occur subtly. For example, consider the following pattern where a JADE session checks to see if a JADE collection contains an object, then adds it to the collection if it is not there.

using (System.Data.IDbTransaction tran = context.BeginTransaction())
{
    if (!allPeople.Contains(person))
        allPeople.Add(person);
    tran.Commit();
}

A deadlock can result if two JADE sessions execute this sequence at the same time with the same collection. Using Contains places a shared lock on the collection, and adding the member attempts to upgrade the lock to exclusive (or update). Both JADE sessions can end up with a shared lock on the collection, thereby preventing one another from upgrading.