Advanced Scope control
There are a number of useful ways to interact with the scopes created by the Transaction Control Service
Determining the current scope
The Transaction Control Service has methods which can be used to work out whether a scope is in effect:
-
txControl.activeScope();
- Whentrue
there is a scope in effect and resources can be accessed. The scope may, or may not, be transactional. When false there is no current scope andtxControl.getCurrentContext();
will returnnull
. -
txControl.activeTransaction()
- Whentrue
there is a transactional scope in effect and resources can be accessed transactionally. When false there may, or may not, be a "No Transaction" scope in effect.
Note that assert txControl.activeTransaction();
can be used to enforce the presence of a transaction.
This is equivalent to a "Mandatory" transaction in Spring or Java EE.
Avoiding rollback
When setting up a transaction certain exception types can be marked as not triggering rollback:
txControl.build() .noRollbackFor(MyCustomException.class) .required(() -> { // A MyCustomException thrown here will not trigger rollback });