Scopes

Name Description
REQUIRED Uses an existing transaction and if none exists will starts a new Transaction. This is the default scope.
MANDATORY A transaction MUST already have been started. Throws TransactionRequiredException.
SUPPORTS Uses the existing transaction if one exists, otherwise the method does not run with a transaction. Used this with caution.
REQUIRES_NEW Always start a new transaction. Suspend an existing once if required.
NOT_SUPPORTED Suspends an existing transaction if required. Method runs without a transaction.
NEVER If there is an existing transaction throws an Exception. Method runs without a transaction.

 

REQUIRED is default

The default transaction scope when not specified is REQUIRED. This is by far the most common transaction scope used in most applications.

 

Example with @Transactional

We can specify other transaction scopes on methods annotated with @Transactional.

@Transactional(type = TxType.REQUIRES_NEW)
public void doInner() {
  // execute using a new transaction ...

}

 

Example with beginTransaction()

We can specify other transaction scopes via beginTransaction(TxScope).

try (Transaction transaction = database.beginTransaction(TxScope.requiresNew())) {
  // using a new transaction ...
  transaction.commit();
}