public interface Transaction
extends java.lang.AutoCloseable
Try-with-resources scenario:
try (Transaction tx = persistence.createTransaction()) {
// transactional code here
tx.commit();
}
Lambda scenario:
persistence.createTransaction().execute((EntityManager em) -> {
// transactional code here
});
Persistence#runInTransaction(Runnable)
,
Persistence#callInTransaction(Callable)
Modifier and Type | Interface and Description |
---|---|
static interface |
Transaction.Callable<T>
Interface for transactional code.
|
static interface |
Transaction.Runnable
Interface for transactional code that is not intended to return a result.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
commit()
Commit current transaction.
|
void |
commitRetaining()
Commit current transaction and immediately start a new one.
|
void |
end()
This method has to be invoked in the following construct:
|
<T> T |
execute(java.lang.String storeName,
Transaction.Callable<T> callable)
Executes the action specified by the given single method object within a transaction.
|
void |
execute(java.lang.String storeName,
Transaction.Runnable runnable)
Executes the action specified by the given single method object within a transaction.
|
<T> T |
execute(Transaction.Callable<T> callable)
Executes the action specified by the given single method object within a transaction in the main data store.
|
void |
execute(Transaction.Runnable runnable)
Executes the action specified by the given single method object within a transaction in the main data store.
|
<T> T execute(Transaction.Callable<T> callable)
execute(String, Callable)
<T> T execute(java.lang.String storeName, Transaction.Callable<T> callable)
RuntimeException
thrown in the transactional code enforces a rollback.T
- result typestoreName
- data store namecallable
- transactional code in the form of Transaction.Callable
void execute(Transaction.Runnable runnable)
execute(String, Runnable)
void execute(java.lang.String storeName, Transaction.Runnable runnable)
RuntimeException
thrown in the transactional code enforces a rollback.storeName
- data store namerunnable
- transactional code in the form of Runnable
void commit()
void commitRetaining()
void end()
Transaction tx = ...
try {
...
tx.commit();
} finally {
tx.end();
}
In case of successful commit this method does nothing. Otherwise it rollbacks the current transaction.void close()
close
in interface java.lang.AutoCloseable