DepositBox
public final class ObjectLock extends java.lang.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 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)
instead of release(org.castor.persist.TransactionContext)
.
TransactionContext
Modifier and Type | Class | Description |
---|---|---|
(package private) static class |
ObjectLock.LinkedTx |
Object uses to hold a linked list of transactions holding
write locks or waiting for a read/write lock.
|
Modifier and Type | Field | Description |
---|---|---|
protected static short |
ACTION_CREATE |
|
protected static short |
ACTION_READ |
|
protected static short |
ACTION_UPDATE |
|
protected static short |
ACTION_WRITE |
Constructor | Description |
---|---|
ObjectLock(OID oid) |
Create a new lock for the specified object.
|
ObjectLock(OID oid,
java.lang.Object[] object,
long version) |
Modifier and Type | Method | Description |
---|---|---|
(package private) void |
acquireCreateLock(TransactionContext tx) |
|
(package private) void |
acquireLoadLock(TransactionContext tx,
boolean write,
int timeout) |
|
(package private) void |
acquireUpdateLock(TransactionContext tx,
int timeout) |
|
(package private) void |
confirm(TransactionContext tx,
boolean succeed) |
|
(package private) void |
delete(TransactionContext tx) |
Informs the lock that the object has been deleted by the
transaction holding the write lock.
|
(package private) 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.
|
java.lang.Object[] |
getObject() |
|
java.lang.Object[] |
getObject(TransactionContext tx) |
Get the object from the DespositBox, only the transaction
have the read or write lock can get it.
|
OID |
getOID() |
Return the object's OID.
|
long |
getVersion() |
|
(package private) boolean |
hasLock(TransactionContext tx,
boolean write) |
Returns true if the transaction holds a read or write lock on
the object.
|
(package private) void |
invalidate(TransactionContext tx) |
|
(package private) boolean |
isDisposable() |
Return true if this object can be safely disposed.
|
(package private) boolean |
isEntered() |
|
(package private) boolean |
isExclusivelyOwned(TransactionContext tx) |
|
(package private) boolean |
isExpired() |
Return true if this entry has been expired from the cache.
|
(package private) boolean |
isFree() |
Return true if and only if this lock can be safely disposed.
|
(package private) void |
leave() |
Indicate that a transaction is not interested to change the
state of this lock anymore.
|
(package private) void |
release(TransactionContext tx) |
Releases a lock on the object previously acquired.
|
void |
setObject(TransactionContext tx,
java.lang.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.
|
(package private) void |
setOID(OID oid) |
Set OID of this lock to new value.
|
void |
setVersion(long version) |
Sets the new version of this object.
|
java.lang.String |
toString() |
|
(package private) void |
upgrade(TransactionContext tx,
int timeout) |
Acquires a lock on the object on behalf of the specified
transaction.
|
protected static final short ACTION_READ
protected static final short ACTION_WRITE
protected static final short ACTION_CREATE
protected static final short ACTION_UPDATE
public ObjectLock(OID oid)
oid
- The object to create a lock forpublic ObjectLock(OID oid, java.lang.Object[] object, long version)
public OID getOID()
getOID
in interface DepositBox
void setOID(OID oid)
void enter()
void leave()
boolean isEntered()
boolean isDisposable()
boolean hasLock(TransactionContext tx, boolean write)
tx
- The transactionwrite
- True if must have a write lockboolean isFree()
boolean isExclusivelyOwned(TransactionContext tx)
boolean isExpired()
public java.lang.Object[] getObject()
public void expire()
public void expired()
void acquireLoadLock(TransactionContext tx, boolean write, int timeout) throws LockNotGrantedException
LockNotGrantedException
void acquireCreateLock(TransactionContext tx) throws LockNotGrantedException
LockNotGrantedException
void acquireUpdateLock(TransactionContext tx, int timeout) throws LockNotGrantedException
LockNotGrantedException
public void setObject(TransactionContext tx, java.lang.Object[] object, long version)
DepositBox
setObject
in interface DepositBox
tx
- the transaction in actionobject
- to be store into deposit boxversion
- of the object to be storedpublic java.lang.Object[] getObject(TransactionContext tx)
DepositBox
getObject
in interface DepositBox
tx
- the transaction in actionpublic long getVersion()
getVersion
in interface DepositBox
public void setVersion(long version)
DepositBox
setVersion
in interface DepositBox
version
- a timestamp.void confirm(TransactionContext tx, boolean succeed)
void upgrade(TransactionContext tx, int timeout) throws LockNotGrantedException
LockNotGrantedException
is thrown. If the object has
been deleted while waiting for a lock, a ObjectDeletedException
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 waitingLockNotGrantedException
- Lock could not be granted in
the specified timeout or a dead lock has been detectedObjectDeletedWaitingForLockException
- The object has
been deleted while waiting for the lockvoid release(TransactionContext tx)
tx
- The transaction that holds the lockvoid delete(TransactionContext tx)
ObjectDeletedException
.tx
- The transaction that holds the lockjava.lang.RuntimeException
- Attempt to delete object without
acquiring a write lockvoid invalidate(TransactionContext tx)
public java.lang.String toString()
toString
in class java.lang.Object
Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com