public interface TransactionHandler
Modifier and Type | Method and Description |
---|---|
default void |
createAndAccept(Consumer<? super Transaction> action)
Creates a new
Transaction and invokes the provided action with
the new transaction. |
<R> R |
createAndApply(Function<? super Transaction,? extends R> mapper)
Creates a new
Transaction and returns the value of applying the
given function to the new transaction. |
Isolation |
getIsolation()
Returns the current isolation level used for new transactions.
|
void |
setIsolation(Isolation level)
Sets the
Isolation level for subsequent transactions created by
this transaction handler. |
void setIsolation(Isolation level)
Isolation
level for subsequent transactions created by
this transaction handler.level
- the new Isolation level to useNullPointerException
- if the provided isolation level is nullIsolation getIsolation()
default void createAndAccept(Consumer<? super Transaction> action) throws TransactionException
Transaction
and invokes the provided action with
the new transaction.
Data that has not been explicitly committed will be automatically rolled
back after the action has been invoked. Explicitly invoking Transaction.rollback()
at the end of the action is a redundant operation.
If a TransactionException
is thrown, parts of the transaction
that has not been previously explicitly committed using Transaction.commit()
will be automatically rolled back.
NB: The transaction is only valid within the scope of the call and only for the current Thread.
EXAMPLE:
// Print out all films and languages in a single transaction
txHandler.createAndAccept(tx -> {
films.stream().forEach(System.out::println);
languages.stream().forEach(System.out::println);
});
action
- to be performed on the new transactionTransactionException
- if the action throws an exceptionNullPointerException
- if the provided mapper is null<R> R createAndApply(Function<? super Transaction,? extends R> mapper) throws TransactionException
Transaction
and returns the value of applying the
given function to the new transaction.
Data that has not been explicitly committed will be automatically rolled
back after the action has been invoked. Explicitly invoking Transaction.rollback()
at the end of the action is a redundant operation.
If a TransactionException
is thrown, parts of the transaction
that has not been previously explicitly committed using Transaction.commit()
will be automatically rolled back.
NB: The transaction is only valid within the scope of the call and only for the current Thread. EXAMPLE:
// Retrieve a list of all films in the English language
// in a single transaction.
List<Film> filmsInEnglish = txHandler.createAndAccept(tx ->
languages.stream()
.filter(Language.NAME.equal("English"))
.flatMap(films.finderBackwardsBy(Film.LANGUAGE_ID))
.collect(Collectors.toList())
);
R
- Return type of the provided mappermapper
- to apply on the new transactionTransactionException
- if the action throws an exceptionNullPointerException
- if the provided mapper is nullCopyright © 2018 Speedment, Inc.. All rights reserved.