Locking

Transactions protect against inconsistencies that can occur if something goes wrong within the transaction itself, but they can do so only within a single thread of execution. Whenever two or more database transactions are operating at the same time, there is the risk that they may interfere with each other by modifying the same objects.

Concurrency control is necessary, and for this we use locks.

To protect against inconsistencies, Jade provides mechanisms to lock objects. In Jade, a lock does two things. Firstly, it controls concurrent access to an object. Secondly, locking an object ensures that the latest edition of the object is brought into local cache in the node. In the Jade language, you can use the exclusiveLock, sharedLock, and reserveLock methods of the Object class to lock objects. The valid concurrent lock combinations are displayed in the following table.

  Exclusive Shared Reserve
Exclusive No No No
Shared No Yes Yes
Reserve No Yes No

Exclusive locks are also known as write locks and shared locks are also known as read locks.

Locks can have two durations: session and transaction. Session locks are held until the end of the session (process/application) that acquired the lock or until the lock is explicitly released using the unlock method. Transaction duration locks are held until the end (either commit or abort) of the next transaction (at which point all transaction duration locks for the process are released) or until the lock is explicitly released using the unlock method when not in transaction state (manual unlocks of transaction duration locks within a transaction are ignored).

Ignoring explicit unlocks of transaction duration locks when in transaction state and releasing all transaction duration locks at the end of a transaction is known as two-phase locking. By doing so, Jade avoids the classic "assumed update" problem, by not allowing a second process to update objects modified by a first process until the first process has committed or aborted the entire transaction.

For examples of locking in the Erewhon demonstration system, see the methods on the ModelTA class and subclasses in the model schema.