org.mockejb
Class TransactionManager

java.lang.Object
  extended byorg.mockejb.TransactionManager
All Implemented Interfaces:
Interceptor, java.io.Serializable

public class TransactionManager
extends java.lang.Object
implements Interceptor, java.io.Serializable

Provides the support for the container-managed transactions according to EJB spec (chapter 18).

Note that RequiredNew is not fully supported since TransactionManager does not know how to suspend transactions.

Transaction policy must be provided in the invocationContext in the "transactionPolicy" field. If it is not provided the Supprts policy is used.

Author:
Alexander Ananiev
See Also:
Serialized Form

Field Summary
static java.lang.String POLICY_CONTEXT_KEY
           
static java.lang.String USER_TRANSACTION_JNDI
           
 
Constructor Summary
TransactionManager()
          Creates a new instance of the TransactionManager with the default (Supports) policy.
TransactionManager(TransactionPolicy policy)
          Creates a new instance of the TransactionManager with the given policy.
 
Method Summary
 boolean equals(java.lang.Object obj)
          Returns true if the given object is of the same type and it has the same transaction policy.
 TransactionPolicy getPolicy()
          Returns the currently set transaction policy.
static javax.transaction.UserTransaction getUserTransaction()
          Returns UserTransaction object.
protected  boolean handlePolicy(TransactionPolicy policy, java.lang.Object targetObj, java.lang.reflect.Method method, java.lang.Object[] args)
          Performs the actions necessary to handle the transaction policy according to the spec.
 int hashCode()
           
 void intercept(InvocationContext invocationContext)
          Begins, commits and rolls back the transaction according to the currently set policy and EJB spec.
protected  void log(java.lang.String message)
           
 void setPolicy(TransactionPolicy policy)
          Sets the transaction policy.
 void setUserTransaction(javax.transaction.UserTransaction userTransaction)
          Sets the shared instance of UserTransaction that will be used by MockEJB.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USER_TRANSACTION_JNDI

public static final java.lang.String USER_TRANSACTION_JNDI
See Also:
Constant Field Values

POLICY_CONTEXT_KEY

public static final java.lang.String POLICY_CONTEXT_KEY
See Also:
Constant Field Values
Constructor Detail

TransactionManager

public TransactionManager(TransactionPolicy policy)
Creates a new instance of the TransactionManager with the given policy.

Parameters:
policy - transaction policy

TransactionManager

public TransactionManager()
Creates a new instance of the TransactionManager with the default (Supports) policy.

Method Detail

getPolicy

public TransactionPolicy getPolicy()
Returns the currently set transaction policy.

Returns:
transaction policy

setPolicy

public void setPolicy(TransactionPolicy policy)
Sets the transaction policy.

Parameters:
policy - policy to set.

intercept

public void intercept(InvocationContext invocationContext)
               throws java.lang.Exception
Begins, commits and rolls back the transaction according to the currently set policy and EJB spec.

Specified by:
intercept in interface Interceptor
Parameters:
invocationContext - provides the info about this call. Also calls interceptors in turn according to their order in the chain. invocationContext provides access to the return value and thrown exceptions of this call.
Throws:
java.lang.Exception

handlePolicy

protected boolean handlePolicy(TransactionPolicy policy,
                               java.lang.Object targetObj,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws javax.transaction.SystemException,
                               javax.naming.NamingException
Performs the actions necessary to handle the transaction policy according to the spec. Determines whether the new transaction has to begin for the given method.

Parameters:
policy - policy of this invoker
targetObj - bean being called
method - method being called
args - parameter values of the method being called
Returns:
true if the new transaction must begin for the given method
Throws:
javax.transaction.SystemException
javax.naming.NamingException

getUserTransaction

public static javax.transaction.UserTransaction getUserTransaction()
Returns UserTransaction object. If setUserTransaction() was called, will return the object that was set by this method. Otherwise, tries to obtain UserTransaction object from JNDI.

Returns:
UserTransaction object

setUserTransaction

public void setUserTransaction(javax.transaction.UserTransaction userTransaction)
Sets the shared instance of UserTransaction that will be used by MockEJB. This is convenient when the remote JNDI is used and the cost of obtaining UserTransaction object from JNDI every time is too high. TransactionManager tries to get UserTransaction for every EJB method call.


log

protected void log(java.lang.String message)

equals

public boolean equals(java.lang.Object obj)
Returns true if the given object is of the same type and it has the same transaction policy.


hashCode

public int hashCode()