org.apache.jdo.impl.pm
Class TransactionImpl

java.lang.Object
  extended byorg.apache.jdo.impl.pm.TransactionImpl
All Implemented Interfaces:
javax.jdo.Transaction

class TransactionImpl
extends java.lang.Object
implements javax.jdo.Transaction

The Transaction interface allows operations to be performed against the transaction in the target Transaction object. A Transaction object is created corresponding to each PersistentManagerImpl creation. The Transaction object can be used for synchronization registration, transaction completion and status query operations. This implementation is StoreManager independent.

Author:
Marina Vatkina

Field Summary
protected static int BMT_JDO
           
protected static int BMT_UT
           
protected static int CMT
           
private  Connector connector
          Connector associated with this transaction
private  javax.transaction.Transaction jta
          javax.transaction.Transaction instance associated with the current thread or null if there is none.
private static org.apache.commons.logging.Log logger
          Logger instance
private static I18NHelper msg
          I18N message handler
protected static int NON_MGD
          Possible values of txType
private  boolean nontransactionalRead
          Flag that indicates if queries and navigation are allowed without an active transaction
private  boolean nontransactionalWrite
          Flag that indicates if write access is allowed without an active transaction
private  boolean optimistic
          Flag that indicates type of the transaction.
private  java.lang.String password
           
private  PersistenceManagerImpl persistenceManager
          PersistenceManager associated with this transaction (1-1)
private  PersistenceManagerFactoryImpl pmFactory
          PersistenceManagerFactory associated with this transaction
private  boolean restoreValues
          Flag that indicates how to handle objects after rollback.
private  boolean retainValues
          Flag that indicates how to handle objects after commit.
private  boolean startedCommit
          The commit process has already begun (even though the status is still STATUS_ACTIVE).
private  int status
          Transaction status (from javax.transaction.Status).
private  java.lang.Object synchronization
          Registered Synchronization object.
private  java.lang.Object txSync
          Synchronisation object associated with this transaction instance
private  int txType
          Flag to indicate usage mode (non-managed versus managed, and so on).
private  java.lang.String username
          values for the datasource user and user password to access security connections
 
Constructor Summary
(package private) TransactionImpl(PersistenceManagerImpl pm, PersistenceManagerFactoryImpl pmf, java.lang.String username, java.lang.String password)
          Constructors new instance of TransactionImpl for the corresponding PersistenceManagerImpl.
 
Method Summary
protected  void afterCompletion(int st)
          Called in the managed environment only for transaction completion by TransactionSynchronization#afterCompletion(int st).
 void assertReadAllowed()
           
protected  void beforeCompletion()
          Called in the managed environment only for transaction completion by TransactionSynchronization#beforeCompletion().
 void begin()
          Begin a transaction.
protected  void begin(javax.transaction.Transaction t)
          Begin a transaction in a managed environment.
private  void beginInternal()
          Status change and validation.
 void commit()
          Commit the transaction represented by this Transaction object
private  void commitComplete()
          Lower-level commit method - phase 3.
private  void commitConnector()
          Commit a connector does flush if necessary, commit and close
private  void commitPrepare()
          Lower-level prepare-commit method - phase 2.
private  boolean debugging()
          Verifies if debugging is enabled.
private  void finish()
          Finish this transaction
private  void flushConnector()
          Close a connector does flush of the changes and close
private  void flushInstances(boolean commit)
          Flush dirty persistent instances to the datastore.
private  int forceRollback()
          Force rollback.
private  Connector getConnector()
          Get a connector
 boolean getNontransactionalRead()
           
 boolean getNontransactionalWrite()
           
 boolean getOptimistic()
           
 javax.jdo.PersistenceManager getPersistenceManager()
          Returns PersistenceManager associated with this transaction
 boolean getRestoreValues()
           
 boolean getRetainValues()
           
 int getStatus()
          Obtain the status of this transaction object.
 javax.transaction.Synchronization getSynchronization()
           
protected  int getTransactionType()
          Returns current transaction type
private  void internalAfterCompletion()
          Notify Connector, PersistenceManager, and registered Synchronization instances about afterCompletion().
protected  void internalFlush()
          Flush changes to the datastore.
private  void internalRollback()
          Lower-level internal rollback method.
 boolean isActive()
           
private  boolean isTerminated()
          Confirm that transaction is terminated.
private  void prepareFlush(boolean _commit)
          Lower-level before-commit method - phase 1.
private  void registerSynchronization(javax.transaction.Synchronization sync)
          Register a Synchronization object for this transaction object.
 void rollback()
          Rollback the transaction represented by this transaction object.
private  void rollbackConnector()
          Rollback a connector does rollback and close
 void setNontransactionalRead(boolean flag)
           
 void setNontransactionalWrite(boolean flag)
           
 void setOptimistic(boolean flag)
           
 void setRestoreValues(boolean flag)
           
 void setRetainValues(boolean flag)
           
 void setRollbackOnly()
          Modify the transaction object such that the only possible outcome of the transaction is to roll back.
private  void setStatus(int status)
          Set status under lock (may be a nested lock which is ok)
 void setSynchronization(javax.transaction.Synchronization sync)
           
protected  boolean startedCommit()
          Returns true if commit has started
static java.lang.String statusString(int status)
          Translates a javax.transaction.Status value into a string.
 java.lang.String toString()
          Returns a string representation of this transaction object.
private  void traceCall(java.lang.String call)
          Trace method call.
private  void traceCall(java.lang.String call, int st)
          Trace method call with a provided status.
private  java.lang.String txTypeString()
          Translates a txType value into a string.
protected  boolean verify(java.lang.String username, java.lang.String password)
          Verify that username and password are equal to ones stored before
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

status

private int status
Transaction status (from javax.transaction.Status).


startedCommit

private boolean startedCommit
The commit process has already begun (even though the status is still STATUS_ACTIVE). This is the first thing set during commit or rollback.


synchronization

private java.lang.Object synchronization
Registered Synchronization object.


txSync

private java.lang.Object txSync
Synchronisation object associated with this transaction instance


pmFactory

private PersistenceManagerFactoryImpl pmFactory
PersistenceManagerFactory associated with this transaction


persistenceManager

private PersistenceManagerImpl persistenceManager
PersistenceManager associated with this transaction (1-1)


connector

private Connector connector
Connector associated with this transaction


jta

private javax.transaction.Transaction jta
javax.transaction.Transaction instance associated with the current thread or null if there is none.


retainValues

private boolean retainValues
Flag that indicates how to handle objects after commit. If true, at commit instances retain their values and the instances


restoreValues

private boolean restoreValues
Flag that indicates how to handle objects after rollback. If true, at rollback instances have their values restored.


optimistic

private boolean optimistic
Flag that indicates type of the transaction. Optimistic transactions do not hold data store locks until commit time.


nontransactionalRead

private boolean nontransactionalRead
Flag that indicates if queries and navigation are allowed without an active transaction


nontransactionalWrite

private boolean nontransactionalWrite
Flag that indicates if write access is allowed without an active transaction


username

private java.lang.String username
values for the datasource user and user password to access security connections


password

private java.lang.String password

NON_MGD

protected static final int NON_MGD
Possible values of txType

See Also:
Constant Field Values

CMT

protected static final int CMT
See Also:
Constant Field Values

BMT_UT

protected static final int BMT_UT
See Also:
Constant Field Values

BMT_JDO

protected static final int BMT_JDO
See Also:
Constant Field Values

txType

private int txType
Flag to indicate usage mode (non-managed versus managed, and so on).


logger

private static final org.apache.commons.logging.Log logger
Logger instance


msg

private static final I18NHelper msg
I18N message handler

Constructor Detail

TransactionImpl

TransactionImpl(PersistenceManagerImpl pm,
                PersistenceManagerFactoryImpl pmf,
                java.lang.String username,
                java.lang.String password)
Constructors new instance of TransactionImpl for the corresponding PersistenceManagerImpl. Username and password are provided for future validation and Connector request.

Parameters:
pm - calling instance of PersistenceManagerImpl
pmf - PersistenceManagerFactoryImpl associated with the instance of PersistenceManagerImpl
username - user name for Connector request
password - user password for Connector request
Method Detail

getPersistenceManager

public javax.jdo.PersistenceManager getPersistenceManager()
Returns PersistenceManager associated with this transaction

Specified by:
getPersistenceManager in interface javax.jdo.Transaction
See Also:
Transaction.getPersistenceManager()

isActive

public boolean isActive()
Specified by:
isActive in interface javax.jdo.Transaction
See Also:
Transaction.isActive()

setRetainValues

public void setRetainValues(boolean flag)
Specified by:
setRetainValues in interface javax.jdo.Transaction
See Also:
Transaction.setRetainValues(boolean flag)

getRetainValues

public boolean getRetainValues()
Specified by:
getRetainValues in interface javax.jdo.Transaction
See Also:
Transaction.getRetainValues()

setRestoreValues

public void setRestoreValues(boolean flag)
Specified by:
setRestoreValues in interface javax.jdo.Transaction
See Also:
Transaction.setRestoreValues(boolean flag)

getRestoreValues

public boolean getRestoreValues()
Specified by:
getRestoreValues in interface javax.jdo.Transaction
See Also:
Transaction.getRestoreValues()

setNontransactionalRead

public void setNontransactionalRead(boolean flag)
Specified by:
setNontransactionalRead in interface javax.jdo.Transaction
See Also:
Transaction.setNontransactionalRead(boolean flag)

getNontransactionalRead

public boolean getNontransactionalRead()
Specified by:
getNontransactionalRead in interface javax.jdo.Transaction
See Also:
Transaction.getNontransactionalRead()

setNontransactionalWrite

public void setNontransactionalWrite(boolean flag)
Specified by:
setNontransactionalWrite in interface javax.jdo.Transaction
See Also:
Transaction.setNontransactionalWrite(boolean flag)

getNontransactionalWrite

public boolean getNontransactionalWrite()
Specified by:
getNontransactionalWrite in interface javax.jdo.Transaction
See Also:
Transaction.getNontransactionalWrite()

setOptimistic

public void setOptimistic(boolean flag)
Specified by:
setOptimistic in interface javax.jdo.Transaction
See Also:
Transaction.setOptimistic(boolean flag)

getOptimistic

public boolean getOptimistic()
Specified by:
getOptimistic in interface javax.jdo.Transaction
See Also:
Transaction.getOptimistic()

setSynchronization

public void setSynchronization(javax.transaction.Synchronization sync)
Specified by:
setSynchronization in interface javax.jdo.Transaction
See Also:
Transaction.setSynchronization(Synchronization sync)

getSynchronization

public javax.transaction.Synchronization getSynchronization()
Specified by:
getSynchronization in interface javax.jdo.Transaction
See Also:
Transaction.getRetainValues()

assertReadAllowed

public void assertReadAllowed()

begin

public void begin()
Begin a transaction.

Specified by:
begin in interface javax.jdo.Transaction
See Also:
Transaction.begin()

commit

public void commit()
Commit the transaction represented by this Transaction object

Specified by:
commit in interface javax.jdo.Transaction
See Also:
Transaction.commit()

rollback

public void rollback()
Rollback the transaction represented by this transaction object.

Specified by:
rollback in interface javax.jdo.Transaction
See Also:
Transaction.rollback()

setRollbackOnly

public void setRollbackOnly()
Modify the transaction object such that the only possible outcome of the transaction is to roll back.


getStatus

public int getStatus()
Obtain the status of this transaction object.

Returns:
The transaction status.

statusString

public static java.lang.String statusString(int status)
Translates a javax.transaction.Status value into a string.

Parameters:
status - Status object to translate.
Returns:
Printable String for a Status object.

toString

public java.lang.String toString()
Returns a string representation of this transaction object.

Returns:
String describing contents of this Transaction object.

getTransactionType

protected int getTransactionType()
Returns current transaction type

Returns:
current transaction type as int.

verify

protected boolean verify(java.lang.String username,
                         java.lang.String password)
Verify that username and password are equal to ones stored before

Parameters:
username - as String
password - as String
Returns:
true if they are equal

startedCommit

protected boolean startedCommit()
Returns true if commit has started

Returns:
true if commit has started

internalFlush

protected void internalFlush()
Flush changes to the datastore. Performed in an active datastore transaction only.


begin

protected void begin(javax.transaction.Transaction t)
Begin a transaction in a managed environment. Called by PersistenceManagerFactoryImpl when JTA Transaction associated with the current thread is active.

Parameters:
t - JTA Transaction associated with the current thread

beforeCompletion

protected void beforeCompletion()
Called in the managed environment only for transaction completion by TransactionSynchronization#beforeCompletion().


afterCompletion

protected void afterCompletion(int st)
Called in the managed environment only for transaction completion by TransactionSynchronization#afterCompletion(int st).


beginInternal

private void beginInternal()
Status change and validation. Called by begin methods.


prepareFlush

private void prepareFlush(boolean _commit)
Lower-level before-commit method - phase 1. This is called to flush changes to the store. State transition: STATUS_ACTIVE starting state internalBeforeCompletion() called while still active STATUS_PREPARING no longer active, about to "really" commit

Parameters:
_commit - true if called during the commit processing For exceptions see commit() method.

commitPrepare

private void commitPrepare()
Lower-level prepare-commit method - phase 2. This is called when flush is finished but before connectorCommit. Will allow to support 2-phase commit. State transition: STATUS_PREPARING starting state STATUS_PREPARED For exceptions see commit() method.


commitComplete

private void commitComplete()
Lower-level commit method - phase 3. Called only in a non- managed environment. State transition: STATUS_PREPARED starting state STATUS_COMMITTING starting to do final phase commitConnector() commit the flush. STATUS_COMMITTED


internalRollback

private void internalRollback()
Lower-level internal rollback method. This is to avoid concurrent rollbacks.


forceRollback

private int forceRollback()
Force rollback. This is called when something goes wrong during a late state check (i.e. some failure occurred during the prepare stage). Unless we're not already rolling back (or rolled back) this will blindly change the state of the transaction and complete the latter stage of rollback.

Returns:
the final status of the transaction. See internalRollback() for exceptions

registerSynchronization

private void registerSynchronization(javax.transaction.Synchronization sync)
Register a Synchronization object for this transaction object. The transction manager invokes the beforeCompletion method prior to starting the transaction commit process. After the transaction is completed (or aborted), the transaction manager invokes the afterCompletion method.

Parameters:
sync - The Synchronization object for the transaction.

isTerminated

private boolean isTerminated()
Confirm that transaction is terminated.

Returns:
True if transaction is completed or not started.

flushInstances

private void flushInstances(boolean commit)
Flush dirty persistent instances to the datastore. If called during the commit processing, notifies registered Synchronization interfaces with beforeCompletion().


internalAfterCompletion

private void internalAfterCompletion()
Notify Connector, PersistenceManager, and registered Synchronization instances about afterCompletion(). All status changes occured before executing this method.


setStatus

private void setStatus(int status)
Set status under lock (may be a nested lock which is ok)


finish

private void finish()
Finish this transaction


getConnector

private Connector getConnector()
Get a connector


flushConnector

private void flushConnector()
Close a connector does flush of the changes and close


rollbackConnector

private void rollbackConnector()
Rollback a connector does rollback and close


commitConnector

private void commitConnector()
Commit a connector does flush if necessary, commit and close


debugging

private boolean debugging()
Verifies if debugging is enabled.

Returns:
true if debugging is enabled.

traceCall

private void traceCall(java.lang.String call)
Trace method call.


traceCall

private void traceCall(java.lang.String call,
                       int st)
Trace method call with a provided status.


txTypeString

private java.lang.String txTypeString()
Translates a txType value into a string.

Returns:
Printable String for a txType value