So in Ebean and EbeanServer there are the 'execute' [in a transaction] methods that take a TxScope:
execute(TxScope scope, TxCallable callable)
execute(TxScope scope, TxRunnable callable)
These methods are similar to the try finally block approach except they support controlling how nested transactions work. TxScope has TxType's of REQUIRED, REQUIRES_NEW, MANDATORY, SUPPORTS, NOT_SUPPORTED, NEVER (plus defining exceptions to rollbackFor, noRollbackFor, transaction isolation level and readOnly).
... if you want "Requires New" then you'd so something like:
ebeanServer.execute(TxScope.requiresNew(), {
// closure
});
Notes:
- You could also use the @Transactional annotation to achieve the same result.
- Ebean.beginTransaction() can't do 'nested transactions'
- In your finally block you should use Ebean.endTransaction() and not Ebean.rollbackTransaction()
Also note that the 'behaviour' to watch for is when you have the same transaction with nested 'transactional methods' and where you have RuntimeExceptions thrown in the child method and caught in a try catch block of the outer method (as sometimes you don't get the behaviour that you were looking for). I'll look to find or create an example of this scenario because I think it can get confusing there (what exceptions cause a rollback and when).
Does that help answer your question?
Cheers, Rob.