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.
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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
|
Modifier and Type | Method and Description |
---|---|
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.
|
protected boolean newTransactionRequested
protected boolean flushAfterFinish
Session.flush()
call will be performed just before the finish of the tx fragment.protected boolean callbacksEnabled
protected HibernateTxFragment parentFragment
public HibernateTxFragment()
public HibernateTxFragment(boolean newTransactionRequested)
newTransactionRequested
- set to false to make it linked to existing transaction if any, true to be run in a new transactionpublic HibernateTxFragment(boolean newTransactionRequested, boolean callbacksEnabled)
newTransactionRequested
- set to false to make it linked to existing transaction if any, true to be run in a new transactioncallbacksEnabled
- If true then this fragment will receive callback invocations both before and after the transaction completion.public HibernateTxFragment(boolean newTransactionRequested, boolean callbacksEnabled, boolean flushAfterFinish)
newTransactionRequested
- set to false to make it linked to existing transaction if any, true to be run in a new transactioncallbacksEnabled
- 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.protected void registerForCallbackNotifications()
beforeCommit, afterCommit
will be invoked if the transaction is commited
and beforeRollback, afterRollback
otherwise.protected void markAsRollbackOnly()
protected void markAsRollbackOnly(Throwable t)
t
- The rollback cause.protected final void executeInitiator(HibernateTransaction tx) throws Exception
Exception
protected final void executeChild(HibernateTransaction tx) throws Exception
Exception
protected void txFragment(org.hibernate.Session session) throws Throwable
Throwable
protected void beforeCommit() throws Throwable
Throwable
protected void beforeRollback() throws Throwable
Throwable
protected void afterRollback() throws Throwable
Throwable
Copyright © 2012-2015 JBoss by Red Hat. All Rights Reserved.