org.multiverse.templates
Class TransactionBoilerplate

java.lang.Object
  extended by org.multiverse.templates.TransactionBoilerplate
All Implemented Interfaces:
MultiverseConstants

public final class TransactionBoilerplate
extends java.lang.Object
implements MultiverseConstants

TransactionBoilerplate contains the boilerplate logic to deal with transaction and needs to be used in combination with the TransactionalCallable.

example:

 new TransactionLogic().execute(new TransactionalCallable {
         Integer call(){
            return queue.pop();
         }
 }).execute();
 

TransactionLogic is threadsafe and is designed to be reused. In case of transactional actors, you can create a TransactionLogic object per class of actors (so static field) or per actor (instance field).

Author:
Sai Venkat, Peter Veentjer.

Field Summary
 
Fields inherited from interface org.multiverse.MultiverseConstants
___BUGSHAKER_ENABLED, ___TRACING_ENABLED
 
Constructor Summary
TransactionBoilerplate()
          Creates a new TransactionLogic that uses the STM stored in the GlobalStmInstance and works the the ThreadLocalTransaction.
TransactionBoilerplate(Stm stm)
          Creates a new TransactionLogic using the provided STM.
TransactionBoilerplate(TransactionFactory transactionFactory)
          Creates a new TransactionLogic with the provided TransactionFactory and that is aware of the ThreadLocalTransaction.
TransactionBoilerplate(TransactionFactory transactionFactory, TransactionLifecycleListener listener, boolean threadLocalAware)
          Creates a new TransactionLogic with the provided TransactionFactory.
 
Method Summary
<E> E
execute(TransactionalCallable<E> callable)
          Executes the TransactionalCallable.
<E> E
executeChecked(TransactionalCallable<E> callable)
          Executes the Template.
 TransactionFactory getTransactionFactory()
          Returns the TransactionFactory this TransactionLogic uses to createReference Transactions.
 TransactionLifecycleListener getTransactionLifecycleListener()
          Returns the TransactionLifecycleListener
 boolean isThreadLocalAware()
          Checks if the TransactionLogic should work together with the ThreadLocalTransaction.
protected  void onStart(Transaction tx)
          Lifecycle method that is called every time the transaction is started.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionBoilerplate

public TransactionBoilerplate()
Creates a new TransactionLogic that uses the STM stored in the GlobalStmInstance and works the the ThreadLocalTransaction. It automatically creates update transactions that are able to track reads.

This constructor should only be used for experimentation purposes. If you want something fast, you need to pass in a TransactionFactory.


TransactionBoilerplate

public TransactionBoilerplate(Stm stm)
Creates a new TransactionLogic using the provided STM. The transaction used is stores/retrieved from the ThreadLocalTransaction.

It automatically creates read tracking update transactions.

This constructor should only be used for experimentation purposes. If you want something fast, you need to pass in a TransactionFactory.

Parameters:
stm - the stm to use for transactions.
Throws:
java.lang.NullPointerException - if stm is null.

TransactionBoilerplate

public TransactionBoilerplate(TransactionFactory transactionFactory)
Creates a new TransactionLogic with the provided TransactionFactory and that is aware of the ThreadLocalTransaction.

Parameters:
transactionFactory - the TransactionFactory used to createReference Transactions.
Throws:
java.lang.NullPointerException - if txFactory is null.

TransactionBoilerplate

public TransactionBoilerplate(TransactionFactory transactionFactory,
                              TransactionLifecycleListener listener,
                              boolean threadLocalAware)
Creates a new TransactionLogic with the provided TransactionFactory.

Parameters:
transactionFactory - the TransactionFactory used to createReference Transactions.
threadLocalAware - true if this TransactionLogic should look at the ThreadLocalTransaction and publish the running transaction there. todo
listener - true if the lifecycle callback methods should be enabled. Enabling them causes extra object creation, so if you want to squeeze out more performance, set this to false at the price of not having the lifecycle methods working.
Method Detail

isThreadLocalAware

public boolean isThreadLocalAware()
Checks if the TransactionLogic should work together with the ThreadLocalTransaction.

Returns:
true if the TransactionLogic should work together with the ThreadLocalTransaction.

getTransactionLifecycleListener

public TransactionLifecycleListener getTransactionLifecycleListener()
Returns the TransactionLifecycleListener

Returns:
the TransactionLifecycleListener. Is allowed to be null which indicates that no events will be send.

getTransactionFactory

public TransactionFactory getTransactionFactory()
Returns the TransactionFactory this TransactionLogic uses to createReference Transactions.

Returns:
the TransactionFactory this TransactionLogic uses to createReference Transactions.

onStart

protected void onStart(Transaction tx)
Lifecycle method that is called every time the transaction is started. It could be that a transaction is retried (for example because of a read or write conflict or because of a blocking transaction).

If this TransactionLogic doesn't starts its own transaction, this method won't be called.

If an exception is thrown while executing this method, the execute and transaction will abort.

Parameters:
tx - the Transaction that is started.

execute

public <E> E execute(TransactionalCallable<E> callable)
Executes the TransactionalCallable. If the callable throws a checked exception, this exception is wrapped inside a InvisibleCheckedException.

Parameters:
callable - the TransactionalCallable to execute.
Returns:
the result.
Throws:
InvisibleCheckedException - if a checked exception was thrown while executing the callable.
DeadTransactionException - if the exception was explicitly aborted.
TooManyRetriesException - if the template retried the transaction too many times. The cause of the last failure (also an exception) is included as cause. So you have some idea where to look for problems
java.lang.NullPointerException - if callable is null.

executeChecked

public <E> E executeChecked(TransactionalCallable<E> callable)
                 throws java.lang.Exception
Executes the Template.

Parameters:
callable - the callable to execute.
Returns:
the result
Throws:
DeadTransactionException - if the exception was explicitly aborted.
TooManyRetriesException - if the template retried the transaction too many times. The cause of the last failure (also an exception) is included as cause. So you have some idea where to look for problems
java.lang.NullPointerException - if callable is null.
java.lang.Exception - if the callable throws an exception.


Copyright © 2008-2010 Multiverse. All Rights Reserved.