org.apache.ojb.odmg
Class TransactionImpl

java.lang.Object
  extended by org.apache.ojb.odmg.TransactionImpl
All Implemented Interfaces:
CollectionProxyListener, MaterializationListener, Configurable, HasBroker, TransactionExt, Transaction
Direct Known Subclasses:
J2EETransactionImpl

public class TransactionImpl
extends Object
implements Transaction, MaterializationListener, Configurable, CollectionProxyListener, TransactionExt

Implementation of Transaction for org.odmg.Transaction.

Version:
$Id: TransactionImpl.java,v 1.1 2007-08-24 22:17:37 ewestfal Exp $
Author:
Thomas Mahler & David Dixon-Peugh, Matthew Baird, Armin Waibel, Brian McCallister

Field Summary
protected  PersistenceBrokerInternal broker
           
protected  ObjectEnvelopeTable objectEnvelopeTable
          the internal table containing all Objects "touched" by this tx and their respective transactional state
 
Fields inherited from interface org.odmg.Transaction
READ, UPGRADE, WRITE
 
Constructor Summary
TransactionImpl(ImplementationImpl implementation)
          Creates new Transaction
 
Method Summary
 void abort()
          Abort and close the transaction.
 void afterLoading(CollectionProxyDefaultImpl colProxy)
          Remove colProxy from list of pending collections and register its contents with the transaction.
 void afterMaterialization(IndirectionHandler handler, Object materializedObject)
          this callback is invoked after an Object is materialized within an IndirectionHandler.
 void beforeLoading(CollectionProxyDefaultImpl colProxy)
          noop -- here for interface
 void beforeMaterialization(IndirectionHandler handler, Identity oid)
          this callback is invoked before an Object is materialized within an IndirectionHandler.
 void begin()
          Start a transaction.
protected  boolean cascadeDeleteFor(ObjectReferenceDescriptor ord)
          Returns true if cascading delete is enabled for the specified single or collection descriptor.
protected  void checkForBegin()
           
protected  void checkForCommit()
           
 void checkpoint()
          Commit the transaction, but reopen the transaction, retaining all locks.
protected  void cleanupBroker()
           
 void clearRegistrationList()
          Clears the list of processed object Identity.
 void commit()
          Commit and close the transaction.
 void configure(Configuration config)
          configure an object using the Configuration pConfig
 void deletePersistent(RuntimeObject rt)
           
protected  void doAbort()
          Do the Aborts, but don't release the locks.
protected  void doClose()
          Close a transaction and do all the cleanup associated with it.
protected  void doWriteObjects(boolean isFlush)
          Write objects to data store, but don't release the locks.
 void flush()
           Calling flush flushes persistent object modifications made within the ODMG transaction since the last checkpoint to the underlying database transaction, but does commit the database transaction.
 DatabaseImpl getAssociatedDatabase()
          Returns the associated database
 PersistenceBroker getBroker()
           
 PersistenceBrokerInternal getBrokerInternal()
          Gets the broker associated with the transaction.
 String getGUID()
           
 ImplementationImpl getImplementation()
           
 NamedRootsMap getNamedRootsMap()
           
 Object getObjectByIdentity(Identity id)
          Get object by identity.
 ArrayList getRegistrationList()
          Returns an empty List for registration of processed object Identity.
protected  int getStatus()
           
protected  boolean hasBroker()
          Returns true if an PersistenceBroker was associated with this tx instance.
 boolean isDeleted(Identity id)
          Checks if the object with the given Identity has been deleted within the transaction using Database.deletePersistent(Object) or TransactionExt.markDelete(Object).
 boolean isImplicitLocking()
          Returns true if implicite locking is enabled.
 boolean isOpen()
          Determine whether the transaction is open or not.
 boolean isOrdering()
          Return true if the OJB ordering algorithm is enabled.
protected  boolean isTransient(ClassDescriptor cld, Object obj, Identity oid)
          Detect new objects.
 void join()
          Attach the caller's thread to this Transaction and detach the thread from any former Transaction the thread may have been associated with.
 void leave()
          Detach the caller's thread from this Transaction , but do not attach the thread to another Transaction .
 void lock(Object obj, int lockMode)
          Upgrade the lock on the given object to the given lock mode.
 void lockAndRegister(RuntimeObject rtObject, int lockMode, boolean cascade, List registeredObjects)
          Lock and register the specified object, make sure that when cascading locking and register is enabled to specify a List to register the already processed object Identiy.
 void lockAndRegister(RuntimeObject rtObject, int lockMode, List registeredObjects)
          Lock and register the specified object, make sure that when cascading locking and register is enabled to specify a List to register the already processed object Identiy.
 void markDelete(Object anObject)
          Marks an object for deletion without locking the object.
 void markDirty(Object anObject)
          Marks an object as dirty without locking the object.
protected  void performTransactionAwareAfterCommit()
           
protected  void performTransactionAwareAfterRollback()
           
protected  void performTransactionAwareBeforeCommit()
           
protected  void performTransactionAwareBeforeRollback()
           
protected  void prepareCommit()
          Prepare does the actual work of moving the changes at the object level into storage (the underlying rdbms for instance).
protected  void refresh()
          cleanup tx and prepare for reuse
protected  void registerToIndirectionHandler(IndirectionHandler handler)
           
protected  void registerUnmaterializedLocks(Object obj)
          register proxy objects that were locked but haven't been materialized yet so they can be unlocked when closing the transaction
 void setCascadingDelete(Class target, boolean doCascade)
          Allows to change the cascading delete behavior of all references of the specified class while this transaction is in use - if the specified class is an interface, abstract class or class with "extent" classes the cascading flag will be propagated.
 void setCascadingDelete(Class target, String referenceField, boolean doCascade)
          Allows to change the cascading delete behavior of the specified reference of the target class while this transaction is in use.
 void setImplicitLocking(boolean value)
          This method can be used to activate or deactivate the implicit locking mechanism for the current transaction.
 void setOrdering(boolean ordering)
          Allows to enable/disable the OJB persistent object ordering algorithm.
protected  void setStatus(int status)
           
 boolean tryLock(Object obj, int lockMode)
          Upgrade the lock on the given object to the given lock mode.
protected  void unRegisterFromAllCollectionProxies()
           
protected  void unRegisterFromAllIndirectionHandlers()
           
protected  void unregisterFromCollectionProxy(CollectionProxy handler)
           
protected  void unregisterFromIndirectionHandler(IndirectionHandler handler)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

broker

protected PersistenceBrokerInternal broker

objectEnvelopeTable

protected ObjectEnvelopeTable objectEnvelopeTable
the internal table containing all Objects "touched" by this tx and their respective transactional state

Constructor Detail

TransactionImpl

public TransactionImpl(ImplementationImpl implementation)
Creates new Transaction

Parameters:
implementation - The odmg Implementation class
Method Detail

getImplementation

public ImplementationImpl getImplementation()

getNamedRootsMap

public NamedRootsMap getNamedRootsMap()

getAssociatedDatabase

public DatabaseImpl getAssociatedDatabase()
Returns the associated database


getStatus

protected int getStatus()

setStatus

protected void setStatus(int status)

isOpen

public boolean isOpen()
Determine whether the transaction is open or not. A transaction is open if a call has been made to begin , but a subsequent call to either commit or abort has not been made.

Specified by:
isOpen in interface Transaction
Returns:
True if the transaction is open, otherwise false.

join

public void join()
Attach the caller's thread to this Transaction and detach the thread from any former Transaction the thread may have been associated with.

Specified by:
join in interface Transaction

lock

public void lock(Object obj,
                 int lockMode)
          throws LockNotGrantedException
Upgrade the lock on the given object to the given lock mode. The call has no effect if the object's current lock is already at or above that level of lock mode.

Specified by:
lock in interface Transaction
Parameters:
obj - object to acquire a lock on.
lockMode - lock mode to acquire. The lock modes are READ , UPGRADE , and WRITE .
Throws:
LockNotGrantedException - Description of Exception

getRegistrationList

public ArrayList getRegistrationList()
Returns an empty List for registration of processed object Identity.


clearRegistrationList

public void clearRegistrationList()
Clears the list of processed object Identity.


lockAndRegister

public void lockAndRegister(RuntimeObject rtObject,
                            int lockMode,
                            List registeredObjects)
Lock and register the specified object, make sure that when cascading locking and register is enabled to specify a List to register the already processed object Identiy.


lockAndRegister

public void lockAndRegister(RuntimeObject rtObject,
                            int lockMode,
                            boolean cascade,
                            List registeredObjects)
Lock and register the specified object, make sure that when cascading locking and register is enabled to specify a List to register the already processed object Identiy.


leave

public void leave()
Detach the caller's thread from this Transaction , but do not attach the thread to another Transaction .

Specified by:
leave in interface Transaction

doWriteObjects

protected void doWriteObjects(boolean isFlush)
                       throws TransactionAbortedException,
                              LockNotGrantedException
Write objects to data store, but don't release the locks. I don't know what we should do if we are in a checkpoint and we need to abort.

Throws:
TransactionAbortedException
LockNotGrantedException

doAbort

protected void doAbort()
Do the Aborts, but don't release the locks. Do the aborts on the NamedRootsMap first, then abort the other stuff.


doClose

protected void doClose()
Close a transaction and do all the cleanup associated with it.


refresh

protected void refresh()
cleanup tx and prepare for reuse


checkpoint

public void checkpoint()
Commit the transaction, but reopen the transaction, retaining all locks. Calling checkpoint commits persistent object modifications made within the transaction since the last checkpoint to the database. The transaction retains all locks it held on those objects at the time the checkpoint was invoked.

Specified by:
checkpoint in interface Transaction

flush

public void flush()
Description copied from interface: TransactionExt

Calling flush flushes persistent object modifications made within the ODMG transaction since the last checkpoint to the underlying database transaction, but does commit the database transaction. The ODMG transaction retains all locks it held on those objects at the time the flush was invoked.

This method is very similair to Transaction.checkpoint().

Specified by:
flush in interface TransactionExt
See Also:
TransactionExt.flush()

markDelete

public void markDelete(Object anObject)
Description copied from interface: TransactionExt
Marks an object for deletion without locking the object. If the object wasn't locked before, OJB will ask for a WRITE lock at commit.

Specified by:
markDelete in interface TransactionExt
Parameters:
anObject - Object to be marked
See Also:
TransactionExt.markDelete(java.lang.Object)

deletePersistent

public void deletePersistent(RuntimeObject rt)

markDirty

public void markDirty(Object anObject)
Description copied from interface: TransactionExt
Marks an object as dirty without locking the object. If the object wasn't locked before, OJB will ask for a WRITE lock at commit.

Specified by:
markDirty in interface TransactionExt
Parameters:
anObject - Object to be marked
See Also:
TransactionExt.markDirty(java.lang.Object)

isDeleted

public boolean isDeleted(Identity id)
Description copied from interface: TransactionExt
Checks if the object with the given Identity has been deleted within the transaction using Database.deletePersistent(Object) or TransactionExt.markDelete(Object).

Specified by:
isDeleted in interface TransactionExt
Parameters:
id - The identity of the object.
Returns:
true if the object has been deleted within the transaction.
See Also:
TransactionExt.isDeleted(org.apache.ojb.broker.Identity)

tryLock

public boolean tryLock(Object obj,
                       int lockMode)
Upgrade the lock on the given object to the given lock mode. Method tryLock is the same as lock except it returns a boolean indicating whether the lock was granted instead of generating an exception.

Specified by:
tryLock in interface Transaction
Parameters:
obj - Description of Parameter
lockMode - Description of Parameter
Returns:
Description of the Returned Value , UPGRADE , and WRITE .

commit

public void commit()
Commit and close the transaction. Calling commit commits to the database all persistent object modifications within the transaction and releases any locks held by the transaction. A persistent object modification is an update of any field of an existing persistent object, or an update or creation of a new named object in the database. If a persistent object modification results in a reference from an existing persistent object to a transient object, the transient object is moved to the database, and all references to it updated accordingly. Note that the act of moving a transient object to the database may create still more persistent references to transient objects, so its referents must be examined and moved as well. This process continues until the database contains no references to transient objects, a condition that is guaranteed as part of transaction commit. Committing a transaction does not remove from memory transient objects created during the transaction. The updateObjectList contains a list of all objects for which this transaction has write privledge to. We need to update these objects.

Specified by:
commit in interface Transaction

checkForCommit

protected void checkForCommit()

prepareCommit

protected void prepareCommit()
                      throws TransactionAbortedException,
                             LockNotGrantedException
Prepare does the actual work of moving the changes at the object level into storage (the underlying rdbms for instance). prepare Can be called multiple times, and does not release locks.

Throws:
TransactionAbortedException - if the transaction has been aborted for any reason.
IllegalStateException - Method called if transaction is not in the proper state to perform this operation
TransactionNotInProgressException - if the transaction is closed.
LockNotGrantedException

abort

public void abort()
Abort and close the transaction. Calling abort abandons all persistent object modifications and releases the associated locks. Aborting a transaction does not restore the state of modified transient objects

Specified by:
abort in interface Transaction

begin

public void begin()
Start a transaction. Calling begin multiple times on the same transaction object, without an intervening call to commit or abort , causes the exception TransactionInProgressException to be thrown on the second and subsequent calls. Operations executed before a transaction has been opened, or before reopening after a transaction is aborted or committed, have undefined results; these may throw a TransactionNotInProgressException exception.

Specified by:
begin in interface Transaction

checkForBegin

protected void checkForBegin()

getGUID

public String getGUID()

getObjectByIdentity

public Object getObjectByIdentity(Identity id)
                           throws PersistenceBrokerException
Get object by identity. First lookup among objects registered in the transaction, then in persistent storage.

Parameters:
id - The identity
Returns:
The object
Throws:
PersistenceBrokerException

beforeMaterialization

public void beforeMaterialization(IndirectionHandler handler,
                                  Identity oid)
this callback is invoked before an Object is materialized within an IndirectionHandler.

Specified by:
beforeMaterialization in interface MaterializationListener
Parameters:
handler - the invoking handler
oid - the identity of the object to be materialized

afterMaterialization

public void afterMaterialization(IndirectionHandler handler,
                                 Object materializedObject)
this callback is invoked after an Object is materialized within an IndirectionHandler. this callback allows to defer registration of objects until it's really neccessary.

Specified by:
afterMaterialization in interface MaterializationListener
Parameters:
handler - the invoking handler
materializedObject - the materialized Object

unRegisterFromAllIndirectionHandlers

protected void unRegisterFromAllIndirectionHandlers()

unRegisterFromAllCollectionProxies

protected void unRegisterFromAllCollectionProxies()

unregisterFromCollectionProxy

protected void unregisterFromCollectionProxy(CollectionProxy handler)

unregisterFromIndirectionHandler

protected void unregisterFromIndirectionHandler(IndirectionHandler handler)

registerToIndirectionHandler

protected void registerToIndirectionHandler(IndirectionHandler handler)

registerUnmaterializedLocks

protected void registerUnmaterializedLocks(Object obj)
register proxy objects that were locked but haven't been materialized yet so they can be unlocked when closing the transaction


getBrokerInternal

public PersistenceBrokerInternal getBrokerInternal()
Gets the broker associated with the transaction. MBAIRD: only return the associated broker if the transaction is open, if it's closed, throw a TransactionNotInProgressException. If we allow brokers to be reaquired by an already closed transaction, there is a very good chance the broker will be leaked as the doClose() method of transactionImpl will never be called and thus the broker will never be closed and returned to the pool.

Returns:
Returns a PersistenceBroker
Throws:
TransactionNotInProgressException - is the transaction is not open;

getBroker

public PersistenceBroker getBroker()
Specified by:
getBroker in interface HasBroker

hasBroker

protected boolean hasBroker()
Returns true if an PersistenceBroker was associated with this tx instance.


cleanupBroker

protected void cleanupBroker()

configure

public void configure(Configuration config)
               throws ConfigurationException
Description copied from interface: Configurable
configure an object using the Configuration pConfig

Specified by:
configure in interface Configurable
Parameters:
config - the Configuration object used to configure current instance
Throws:
ConfigurationException

setImplicitLocking

public void setImplicitLocking(boolean value)
Description copied from interface: TransactionExt
This method can be used to activate or deactivate the implicit locking mechanism for the current transaction.
If set true OJB implicitly locks objects to ODMG transactions after performing OQL queries. Also if implicit locking is used locking objects is recursive, that is associated objects are also locked. If ImplicitLocking is set to 'false', no locks are obtained in OQL queries, lookup objects and there is also no recursive locking.

Turning off implicit locking may improve performance but requires additional care to make sure all changed objects are properly registered to the transaction.

Specified by:
setImplicitLocking in interface TransactionExt
Parameters:
value - If set true implicit locking is enabled, if false, implicit locking is disabled.
See Also:
TransactionExt.setImplicitLocking(boolean)

isImplicitLocking

public boolean isImplicitLocking()
Description copied from interface: TransactionExt
Returns true if implicite locking is enabled.

Specified by:
isImplicitLocking in interface TransactionExt
See Also:
TransactionExt.setImplicitLocking(boolean)

beforeLoading

public void beforeLoading(CollectionProxyDefaultImpl colProxy)
noop -- here for interface

Specified by:
beforeLoading in interface CollectionProxyListener
Parameters:
colProxy - the CollectionProxy

afterLoading

public void afterLoading(CollectionProxyDefaultImpl colProxy)
Remove colProxy from list of pending collections and register its contents with the transaction.

Specified by:
afterLoading in interface CollectionProxyListener
Parameters:
colProxy - the CollectionProxy

performTransactionAwareBeforeCommit

protected void performTransactionAwareBeforeCommit()

performTransactionAwareAfterCommit

protected void performTransactionAwareAfterCommit()

performTransactionAwareBeforeRollback

protected void performTransactionAwareBeforeRollback()

performTransactionAwareAfterRollback

protected void performTransactionAwareAfterRollback()

isTransient

protected boolean isTransient(ClassDescriptor cld,
                              Object obj,
                              Identity oid)
Detect new objects.


setCascadingDelete

public void setCascadingDelete(Class target,
                               String referenceField,
                               boolean doCascade)
Allows to change the cascading delete behavior of the specified reference of the target class while this transaction is in use.

Specified by:
setCascadingDelete in interface TransactionExt
Parameters:
target - The class to change cascading delete behavior of the references.
referenceField - The field name of the 1:1, 1:n or 1:n reference.
doCascade - If true cascading delete is enabled, false disabled.

setCascadingDelete

public void setCascadingDelete(Class target,
                               boolean doCascade)
Allows to change the cascading delete behavior of all references of the specified class while this transaction is in use - if the specified class is an interface, abstract class or class with "extent" classes the cascading flag will be propagated.

Specified by:
setCascadingDelete in interface TransactionExt
Parameters:
target - The class to change cascading delete behavior of all references.
doCascade - If true cascading delete is enabled, false disabled.

cascadeDeleteFor

protected boolean cascadeDeleteFor(ObjectReferenceDescriptor ord)
Returns true if cascading delete is enabled for the specified single or collection descriptor.


isOrdering

public boolean isOrdering()
Return true if the OJB ordering algorithm is enabled.

Specified by:
isOrdering in interface TransactionExt
See Also:
setOrdering(boolean)

setOrdering

public void setOrdering(boolean ordering)
Allows to enable/disable the OJB persistent object ordering algorithm. If true OJB try to order the modified/new/deleted objects in a correct order (based on a algorithm) before the objects are written to the persistent storage.
If false the order of the objects rely on the order specified by the user and on settings like setImplicitLocking(boolean).

Specified by:
setOrdering in interface TransactionExt
Parameters:
ordering - Set true to enable object ordering on commit.
See Also:
ImplementationExt.setOrdering(boolean)


Copyright © 2004-2012 The Kuali Foundation. All Rights Reserved.