Transaction Manager
Hint
This article is generated by AI translation.
The TransactionManager interface is the core of dbVisitor's transaction management. At an abstract level, it exposes the following methods:
| Method | Purpose |
|---|---|
begin() | Start a transaction (defaults to REQUIRED + DEFAULT) |
begin(Propagation) | Start a transaction with specified propagation behavior |
begin(Propagation, Isolation) | Start a transaction with specified propagation and isolation |
commit() / commit(TransactionStatus) | Commit a transaction |
rollBack() / rollBack(TransactionStatus) | Roll back a transaction |
hasTransaction() | Check if a transaction is in progress |
isTopTransaction(TransactionStatus) | Check if the specified transaction is at the top of the stack |
info
- The same transaction manager can open multiple transactions in sequence. See Propagation and Isolation for how they interact.
TransactionManagerimplements theCloseableinterface and can be used with try-with-resources.
How it works
Each call to begin creates a new transaction and pushes it onto a transaction stack.
Example: three transactions via TransactionManager
DataSource dataSource = ...
TransactionManager txManager = new LocalTransactionManager(dataSource);
TransactionStatus tranA = txManager.begin();
TransactionStatus tranB = txManager.begin();
TransactionStatus tranC = txManager.begin();
...
txManager.commit(tranC);
txManager.commit(tranB);
txManager.commit(tranA);
On the transaction stack they appear like this; it is a standard LIFO stack:
+--------+--------+--------+ <<<< push
| Tran A | Tran B | Tran C |
+--------+--------+--------+ >>>> pop
Note
With the transaction stack, dbVisitor allows operating on any position. For example, after opening three transactions you can commit them in one call.
DataSource dataSource = ...
TransactionManager manager = new LocalTransactionManager(dataSource);
TransactionStatus tranA = manager.begin();
TransactionStatus tranB = manager.begin();
TransactionStatus tranC = manager.begin();
...
manager.commit(tranA);
If you commit out of order, dbVisitor will automatically perform the needed sequence under the hood, equivalent to:
manager.commit(tranC);
manager.commit(tranB);
manager.commit(tranA);
How to use
dbVisitor provides three ways to work with transactions:
- Programmatic API: call
LocalTransactionManagermethods directly to control transactions. - Transaction Template: use the
TransactionTemplateinterface to execute transactional code blocks with automatic commit/rollback. - Annotation-based: declaratively control transactions with the
@Transactionalannotation.