Data
Transactions
@Transactional, the transaction manager, propagation, isolation, rollback rules, savepoints and timeouts.
Every repository call is atomic on its own. Use a transaction to make several of them succeed or fail together.
@Transactional
@Injectable
open class BankService(private val accounts: AccountRepository) {
@Transactional("main")
open fun transfer(from: String, to: String, amount: Long) {
accounts.save(accounts.findById(from)!!.let { it.copy(balance = it.balance - amount) })
accounts.save(accounts.findById(to)!!.let { it.copy(balance = it.balance + amount) }) // an exception here undoes the debit
}
}The processor writes a subclass (BankService_Tx) that wraps the method, so there are no proxies. The class and the method must be open, and calls from another method of the same instance are not wrapped. On a class, @Transactional covers every public method. @Transactional("main") names the connection; leave it out when there is only one.
| Setting | Values | Default |
|---|---|---|
propagation | REQUIRED, REQUIRES_NEW, SUPPORTS, MANDATORY, NEVER, NOT_SUPPORTED, NESTED | REQUIRED |
isolation | DEFAULT, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE | DEFAULT |
readOnly | refuse writes (SQL) | false |
timeout | seconds, checked before each statement | none |
rollbackFor, noRollbackFor | exception classes | every exception rolls back |
Propagation
| When a transaction is open | When none is | |
|---|---|---|
REQUIRED | joins it | starts one |
REQUIRES_NEW | suspends it and starts another, which commits on its own | starts one |
NESTED | runs in a savepoint that can roll back alone (SQL) | starts one |
SUPPORTS | joins it | runs without |
MANDATORY | joins it | throws TransactionRequiredException |
NEVER | throws IllegalTransactionStateException | runs without |
NOT_SUPPORTED | suspends it and runs without | runs without |
@Transactional("main") open fun place(order: Order) {
audit.record("attempt") // REQUIRES_NEW: written even if place() rolls back
stock.reserve(order) // REQUIRED: part of this transaction
}When an inner @Transactional call fails and the outer code catches the exception, the transaction is marked rollback-only: it will not commit, and ending it throws UnexpectedRollbackException, so a half-failed unit cannot be committed by accident.
Rollback rules
@Transactional("main", noRollbackFor = [SoftFailure::class]) // commit, then rethrow
@Transactional("main", rollbackFor = [PaymentException::class]) // only this rolls backThe transaction manager
Inject TransactionManagers (one manager per connection) or TransactionManager (when there is a single connection, or one called default):
@Injectable
class Checkout(private val tx: TransactionManagers, private val orders: OrderRepository) {
fun run(order: Order) = tx.get("main").run(TransactionOptions(isolation = Isolation.SERIALIZABLE)) {
orders.save(order)
if (stockIsShort()) tx.get("main").setRollbackOnly() // roll back without throwing
}
val inside get() = tx.get("main").active
}run(options) { } is the programmatic twin of the annotation; active tells whether the calling thread is in a transaction; setRollbackOnly() marks it. orm.transaction("main") { } does the same without injection. Pass the manager (or the connection name) around like any other value.
Rules
- A transaction belongs to the thread that opened it, and to one connection. Work you hand to another thread does not join it, and a transaction does not span PostgreSQL and MongoDB.
- Isolation and read-only are SQL settings; MongoDB honours propagation and rollback rules and ignores them. MongoDB transactions need a replica set.
- A timeout is checked before each statement; it does not interrupt one that is already running.
What is tested
Against PostgreSQL, MySQL and MongoDB: commit and rollback, REQUIRES_NEW surviving an outer rollback, savepoints, mandatory and never, rollback rules, rollback-only, a read-only write being refused, a timeout, the isolation level inside and restored after, and class-level @Transactional.