Product Information > JADE .NET Developer’s Reference > Chapter 2 - Object Management > Changing Lock Duration

Changing Lock Duration

Lock duration can be upgraded only, from transaction to session. The lock duration cannot be downgraded from session to transaction.

A request to downgrade both duration and type is ignored. The lock type for a lock with session duration can be downgraded only by an explicit lock request that specifies session duration.

Lock duration upgrades do not result in queuing, provided they are not accompanied by requests to upgrade the lock type.

JADE keeps track of the lock type associated with a lock’s most-recent session duration request. When a JADE session has a session duration lock and issues a request for a higher lock type but of transaction duration, the lock type is upgraded but the duration remains session duration. If automatic unlocking of transaction duration locks then occurs (for example, when committing or rolling back a transaction), the session duration lock is retained but its lock type reverts to the prior value.

Similarly, when a JADE session is in transaction state and a session duration lock with a lower lock type is requested on an existing lock of transaction duration, the lock duration is upgraded to session but the lock type is not downgraded until the transaction is committed or rolled back.

The following code fragment gives examples of changing lock duration.

TimeSpan timeOut = TimeSpan.FromSeconds(5);
context.Lock(obj1, LockType.Shared, LockDuration.Transaction, timeOut);
context.Lock(obj1, LockType.Shared, LockDuration.Session, timeOut);
// obj1’s lock is now shared, session duration.
context.Lock(obj2, LockType.Exclusive, LockDuration.Session, timeOut);
context.Lock(obj2, LockType.Exclusive, LockDuration.Transaction, timeOut);
// obj2’s lock remains exclusive, session duration.
context.Lock(obj3, LockType.Shared, LockDuration.Session, timeOut);
using (System.Data.IDbTransaction tran = context.BeginTransaction())
{
    context.Lock(obj3, LockType.Exclusive, LockDuration.Transaction, timeOut);
    // obj3’s lock is upgraded to exclusive, session duration.
    tran.Commit();
    // obj3’s lock reverts to shared, session duration.
    // Session duration locks remain in place.
}
context.Lock(obj1, LockType.Exclusive, LockDuration.Session,
             TimeSpan.FromSeconds(5));
// obj1’s lock type is upgraded to exclusive.
context.Lock(obj2, LockType.Reserve, LockDuration.Session, timeOut);
// obj2’s lock type is downgraded to reserve.
context.Lock(obj2, LockType.Shared, LockDuration.Transaction, timeOut);
// This is ignored, as it is requesting to downgrade both duration and type.
// Obj2’s lock remains as reserve, session duration.

context.Unlock(obj3);
context.Unlock(obj2);
context.Unlock(obj1);