public static < T > T withinTx ( TransactionManager tm , Callable < T > c ) throws Exception { if ( tm == null ) { try { return c . call ( ) ; } catch ( Exception e ) { throw e ; } } else { tm . begin ( ) ; try { return c . call ( ) ; } catch ( Exception e ) { tm . setRollbackOnly ( ) ; throw e ; } finally { if ( tm . getStatus ( ) == Status . STATUS_ACTIVE ) { tm . commit ( ) ; } else { tm . rollback ( ) ; } } } }
Call an operation within a transaction . This method guarantees that the right pattern is used to make sure that the transaction is always either committed or rollbacked .