|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.exolab.castor.persist.ObjectLock
public final class ObjectLock
Read/write locks and lock synchronization on an object. Each object is required to have one ObjectLock which at any given time may be unlocked, write locked by one transaction, or read locked by one or more transactions.
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 the acquireLock. 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
occurred (or a dead lock has been detected), LockNotGrantedException is thrown. If the object has been delete
while waiting for the lock, ObjectDeletedWaitingForLockException is
thrown.
When the lock is acquired, the locked object is returned.
The transaction must call release(org.castor.persist.TransactionContext) 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 release(org.castor.persist.TransactionContext).
If the object has been deleted, the transaction must call delete(org.castor.persist.TransactionContext) first, then release(org.castor.persist.TransactionContext).
| Constructor Summary | |
|---|---|
protected |
ObjectLock(OID oid)
Create a new lock for the specified object. |
protected |
ObjectLock(OID oid,
Object[] object,
long version)
Create a new lock for the specified object. |
| Method Summary | |
|---|---|
protected void |
acquireLock(TransactionContext tx,
LockAction action,
int timeout)
The TransactionContext tries to acquire a lock for LockAction. |
void |
confirm(TransactionContext tx,
boolean succeed)
Confirm the lock. |
void |
delete(TransactionContext tx)
Informs the lock that the object has been deleted by the transaction holding the write lock. |
protected void |
enter()
Indicate that a transaction is interested in this lock. |
void |
expire()
Indicate that object needs to be expired from the cache. |
protected void |
expired()
Indicate that object has been removed from the cache. |
Object[] |
getObject()
|
Object[] |
getObject(TransactionContext tx)
Get the object from the DespositBox, only the transaction have the read or write lock can get it. |
OID |
getOID()
Get OID of the entity locked. |
long |
getVersion()
|
protected boolean |
hasLock(TransactionContext tx)
Returns true if the transaction holds a read or write lock on the object. |
protected boolean |
hasWriteLock(TransactionContext tx)
Returns true if the transaction holds a write lock on the object. |
void |
invalidate(TransactionContext tx)
Informs the lock that the object has been invalidated by the transaction holding the write lock. |
protected boolean |
isDisposable()
|
protected boolean |
isEntered()
Are there transactions interested in this lock? |
protected boolean |
isExclusivelyOwned(TransactionContext tx)
|
protected boolean |
isExpired()
Has this entity been expired from the cache? |
protected void |
leave()
Indicate that a transaction is not interested to change the state of this lock anymore (ie, will not call either acquireLock, delete, upgrade or release). |
void |
release(TransactionContext tx)
Releases a lock on the object previously acquired. |
void |
setObject(TransactionContext tx,
Object[] object,
long version)
Set an object into the DespositBox, only a transaction has the write lock may call it method or IllegalArgumentException will be thrown. |
protected void |
setOID(OID oid)
Set OID of the entity locked. |
void |
setVersion(long version)
Set the version of the object. |
String |
toString()
|
protected void |
upgrade(TransactionContext tx,
int timeout)
Acquires a lock on the object on behalf of the specified transaction. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
protected ObjectLock(OID oid)
oid - The object to create a lock for
protected ObjectLock(OID oid,
Object[] object,
long version)
oid - the object to create a lock forobject - the values of the entityversion - the version of the entity| Method Detail |
|---|
public OID getOID()
getOID in interface DepositBoxprotected void setOID(OID oid)
oid - OID of the entity locked.public Object[] getObject()
public Object[] getObject(TransactionContext tx)
DepositBox
getObject in interface DepositBoxtx - The transaction
public void setObject(TransactionContext tx,
Object[] object,
long version)
DepositBox
setObject in interface DepositBoxtx - The TransactionContext which want to set the objectobject - The actual objectversion - the version of the objectpublic long getVersion()
getVersion in interface DepositBoxpublic void setVersion(long version)
setVersion in interface DepositBoxversion - the new versionprotected void enter()
protected void leave()
protected boolean isEntered()
true if there is any transaction called enter(),
but not yet called leave().
protected void acquireLock(TransactionContext tx,
LockAction action,
int timeout)
throws LockNotGrantedException
tx - The TransactionContextaction - The LockActiontimeout - The timeout for acquiring the lock
LockNotGrantedException - If the lock is not granted
public void confirm(TransactionContext tx,
boolean succeed)
tx - The transactionsucceed - whether the transaction's process is OK
protected void upgrade(TransactionContext tx,
int timeout)
throws LockNotGrantedException
LockNotGrantedException is thrown. If the object has
been deleted while waiting for a lock, a ObjectDeletedWaitingForLockException is thrown. To prevent dead locks, a
transaction must only call this method for any given object
from a single thread and must mark the lock it is trying to
acquire and return it from a call to TransactionContext.getWaitOnLock() if the call to this method
has not returned yet. If a read lock is available for the
transaction and a write lock is requested, the read lock is
cancelled whether or not the write is acquired.
tx - The transaction requesting the locktimeout - Timeout waiting to acquire lock (in milliseconds),
zero for no waiting
LockNotGrantedException - Lock could not be granted in
the specified timeout or a dead lock has been detectedpublic void release(TransactionContext tx)
tx - The transaction that holds the lockpublic void delete(TransactionContext tx)
tx - The transaction that holds the lockpublic void invalidate(TransactionContext tx)
tx - The transaction that holds the lockpublic void expire()
protected void expired()
protected boolean isExpired()
true if this entity has been expired from the cache.protected boolean isDisposable()
protected boolean hasLock(TransactionContext tx)
tx - The transaction.
true if the transaction has a lock on this object.protected boolean hasWriteLock(TransactionContext tx)
tx - The transaction.
true if the transaction has a write lock on this object.protected boolean isExclusivelyOwned(TransactionContext tx)
tx - The transaction.
true if the transaction has the lock exclusively on this object.public String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||