public void executeWithinTransaction(TransactionalClosure closure) { boolean initiallyInTransaction = inTransaction(); if(!initiallyInTransaction) { startTransaction(); } try { closure.preExecute(); closure.execute(); closure.onSuccess(); if(!initiallyInTransaction) { endTransaction(); } } catch (RuntimeException ex) { closure.onFailure(); if(!initiallyInTransaction) { // temp TODO fix swallowing of exception // System.out.println(ex.getMessage()); // ex.printStackTrace(); try { abortTransaction(); } catch (Exception e) { LOG.error("Abort failure after exception", e); // System.out.println(e.getMessage()); // e.printStackTrace(); throw new NakedObjectTransactionManagerException("Abort failure: " + e.getMessage(), ex); } } throw ex; } }
public <Q> Q executeWithinTransaction(TransactionalClosureWithReturn<Q> closure) { boolean initiallyInTransaction = inTransaction(); if(!initiallyInTransaction) { startTransaction(); } try { closure.preExecute(); Q retVal = closure.execute(); closure.onSuccess(); if(!initiallyInTransaction) { endTransaction(); } return retVal; } catch (RuntimeException ex) { closure.onFailure(); if(!initiallyInTransaction) { abortTransaction(); } throw ex; } }