org.exolab.castor.persist
public final class ObjectLock extends Object implements DepositBox
In order to obtain a lock, the transaction must call one of the acquire, passing itself, the lock type and the lock timeout. The transaction must attempt to obtain only one lock at any given time by synchronizing all calls to one of the acquire. If the transaction has acquired a read lock it may attempt to re-acquire the read lock. If the transaction attempts to acquire a write lock the lock will be upgraded.
A read lock cannot be acquired while there is a write lock on the object, and a write lock cannot be acquired while there is one or more read locks. If a lock cannot be acquired, the transaction will hold until the lock is available or timeout occurs. If timeout occured (or a dead lock has been detected), LockNotGrantedException is thrown. If the object has been delete while waiting for the lock, ObjectDeletedException is thrown.
When the lock is acquired, the locked object is returned.
The transaction must call ObjectLock when the lock is no longer required, allowing other transactions to obtain a lock. The transaction must release all references to the object prior to calling ObjectLock.
If the object has been deleted, the transaction must call ObjectLock instead of ObjectLock.
Version: $Revision: 1.7 $ $Date: 2005/07/27 11:54:21 $
See Also: TransactionContext
Nested Class Summary | |
---|---|
static class | ObjectLock.LinkedTx
Object uses to hold a linked list of transactions holding
write locks or waiting for a read/write lock. |
Field Summary | |
---|---|
static short | ACTION_CREATE |
static short | ACTION_READ |
static short | ACTION_UPDATE |
static short | ACTION_WRITE |
static int | idcount |
static int[] | lock |
Constructor Summary | |
---|---|
ObjectLock(OID oid)
Create a new lock for the specified object. | |
ObjectLock(CacheEntry entry) |
Method Summary | |
---|---|
void | acquireCreateLock(TransactionContext tx) |
void | acquireLoadLock(TransactionContext tx, boolean write, int timeout) |
void | acquireUpdateLock(TransactionContext tx, int timeout) |
void | confirm(TransactionContext tx, boolean succeed) |
void | delete(TransactionContext tx)
Informs the lock that the object has been deleted by the
transaction holding the write lock. |
void | enter()
Indicate that a transaction is interested in this lock.
|
void | expire()
Indicate that object needs to be expired from the cache |
void | expired()
Indicate that object has been removed from the cache. |
Object | getObject() |
Object | getObject(TransactionContext tx) |
OID | getOID()
Return the object's OID. |
long | getTimeStamp() |
boolean | hasLock(TransactionContext tx, boolean write)
Returns true if the transaction holds a read or write lock on
the object. |
void | invalidate(TransactionContext tx) |
boolean | isDisposable()
Return true if this object can be safely disposed. |
boolean | isEntered()
Return true if there is any transaction called ObjectLock,
but not yet called ObjectLock. |
boolean | isExclusivelyOwned(TransactionContext tx) |
boolean | isExpired()
Return true if this entry has been expired from the cache |
boolean | isFree()
Return true if and only if this lock can be safely disposed
|
void | leave()
Indicate that a transaction is not interested to change the
state of this lock anymore. (ie, will not call either acquire
update, release or delete.)
|
void | release(TransactionContext tx)
Releases a lock on the object previously acquired.
|
void | setObject(TransactionContext tx, Object object) |
void | setOID(OID oid)
Set OID of this lock to new value.
|
String | toString() |
void | upgrade(TransactionContext tx, int timeout)
Acquires a lock on the object on behalf of the specified
transaction. |
Parameters: oid The object to create a lock for
Parameters: tx The transaction that holds the lock
Throws: RuntimeException Attempt to delete object without acquiring a write lock
Parameters: tx The transaction write True if must have a write lock
Returns: True if the transaction has a lock on this object
Returns: True if no lock and no waiting
Parameters: tx The transaction that holds the lock
Parameters: tx The transaction requesting the lock timeout Timeout waiting to acquire lock (in milliseconds), zero for no waiting
Throws: LockNotGrantedException Lock could not be granted in the specified timeout or a dead lock has been detected ObjectDeletedWaitingForLockException The object has been deleted while waiting for the lock