Dashboard Builder Commons 6.0.0.Beta3

org.jboss.dashboard.database.hibernate
Class HibernateTxFragment

java.lang.Object
  extended by org.jboss.dashboard.database.hibernate.HibernateTxFragment

public abstract class HibernateTxFragment
extends Object

A transaction fragment is a block of code executed inside the scope of a global transaction.

Pattern:

 // Define the transaction fragment
 final String finalVar = "";
 new HibernateTxFragment() {
 new HibernateTxFragment(false, true) { // Activate callback methods afterXXX/beforeXXX.
 new HibernateTxFragment(true, false) { // Open a brand new transaction.
 protected void txFragment(Session session) throws Exception {
   // Transactional block
   // ...
   finalVar = ""; // Forbidden sentence
 }}.execute();
 

IMPORTANT NOTE: All variables accessed inside the transaction fragment block must be defined as final because a inner class can't change the reference to a variable defined into its scope.

Another issue to be considered is what happens when a fragment needs to return a value from txFragment method. Use an array or collection to store the return value(s):

 final Object[] result = new Object[] {null};
 new HibernateTxFragment() {
 protected void txFragment(Session session) throws Exception {
    // ...
    result[0] = ...;
 }}.execute();
 return result[0];
 

The variable result can be an array or a collection or any other structure where we can store the results to be returned by the fragment block.


Field Summary
protected  boolean callbacksEnabled
          If true then this fragment will receive callback invocations both before and after the transaction completion.
protected  boolean flushAfterFinish
          Flag to control the Hibernate flush.
protected  boolean newTransactionRequested
          Flag to control the reuse of the current transaction.
protected  HibernateTxFragment parentFragment
          The parent fragment.
 
Constructor Summary
HibernateTxFragment()
          Create a new fragment linked to existing transaction if any
HibernateTxFragment(boolean newTransactionRequested)
          Create a new fragment
HibernateTxFragment(boolean newTransactionRequested, boolean callbacksEnabled)
          Create a new fragment
HibernateTxFragment(boolean newTransactionRequested, boolean callbacksEnabled, boolean flushAfterFinish)
          Create a new fragment
 
Method Summary
protected  void afterCommit()
          Callback method invoked after a transaction rollback.
protected  void afterRollback()
          Callback method invoked after a transaction commit.
protected  void beforeCommit()
          Callback method invoked before the transaction is commited.
protected  void beforeRollback()
          Callback method invoked before the transaction is rolled back.
 void execute()
          Execute this fragment.
protected  void executeChild(HibernateTransaction tx)
           
protected  void executeInitiator(HibernateTransaction tx)
           
protected  void markAsRollbackOnly()
          Mark the transaction as rollback only
protected  void markAsRollbackOnly(Throwable t)
          Mark the transaction as rollback only
protected  void registerForCallbackNotifications()
          Enable callback method notifications: beforeCommit, afterCommit will be invoked if the transaction is commited and beforeRollback, afterRollback otherwise.
protected  void txFragment(org.hibernate.Session session)
          Custom fragment implementation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

newTransactionRequested

protected boolean newTransactionRequested
Flag to control the reuse of the current transaction. If true then a brand new transaction is opened and added to the list of transactions linked with the current thread.


flushAfterFinish

protected boolean flushAfterFinish
Flag to control the Hibernate flush. If true a Session.flush() call will be performed just before the finish of the tx fragment.


callbacksEnabled

protected boolean callbacksEnabled
If true then this fragment will receive callback invocations both before and after the transaction completion.


parentFragment

protected HibernateTxFragment parentFragment
The parent fragment. It only applies for child fragments.

Constructor Detail

HibernateTxFragment

public HibernateTxFragment()
Create a new fragment linked to existing transaction if any


HibernateTxFragment

public HibernateTxFragment(boolean newTransactionRequested)
Create a new fragment

Parameters:
newTransactionRequested - set to false to make it linked to existing transaction if any, true to be run in a new transaction

HibernateTxFragment

public HibernateTxFragment(boolean newTransactionRequested,
                           boolean callbacksEnabled)
Create a new fragment

Parameters:
newTransactionRequested - set to false to make it linked to existing transaction if any, true to be run in a new transaction
callbacksEnabled - If true then this fragment will receive callback invocations both before and after the transaction completion.

HibernateTxFragment

public HibernateTxFragment(boolean newTransactionRequested,
                           boolean callbacksEnabled,
                           boolean flushAfterFinish)
Create a new fragment

Parameters:
newTransactionRequested - set to false to make it linked to existing transaction if any, true to be run in a new transaction
callbacksEnabled - If true then this fragment will receive callback invocations both before and after the transaction completion.
flushAfterFinish - If true a Session.flush() call will be performed just before the finish of the tx fragment.
Method Detail

registerForCallbackNotifications

protected void registerForCallbackNotifications()
Enable callback method notifications: beforeCommit, afterCommit will be invoked if the transaction is commited and beforeRollback, afterRollback otherwise.


markAsRollbackOnly

protected void markAsRollbackOnly()
Mark the transaction as rollback only


markAsRollbackOnly

protected void markAsRollbackOnly(Throwable t)
Mark the transaction as rollback only

Parameters:
t - The rollback cause.

execute

public final void execute()
                   throws Exception
Execute this fragment.

Throws:
Exception

executeInitiator

protected final void executeInitiator(HibernateTransaction tx)
                               throws Exception
Throws:
Exception

executeChild

protected final void executeChild(HibernateTransaction tx)
                           throws Exception
Throws:
Exception

txFragment

protected void txFragment(org.hibernate.Session session)
                   throws Throwable
Custom fragment implementation.

Throws:
Throwable

beforeCommit

protected void beforeCommit()
                     throws Throwable
Callback method invoked before the transaction is commited.

Throws:
Throwable

beforeRollback

protected void beforeRollback()
                       throws Throwable
Callback method invoked before the transaction is rolled back.

Throws:
Throwable

afterRollback

protected void afterRollback()
                      throws Throwable
Callback method invoked after a transaction commit.

Throws:
Throwable

afterCommit

protected void afterCommit()
                    throws Throwable
Callback method invoked after a transaction rollback.

Throws:
Throwable

Dashboard Builder Commons 6.0.0.Beta3

Copyright © 2012-2013 JBoss by Red Hat. All Rights Reserved.