ne 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.