@Transactional Annotation in Spring Flashcards
- What Is @Transactional?
- What does the annotation allow us to set?
- We can use @Transactional to wrap a method in a database transaction.
- It allows us to set propagation, isolation, timeout, read-only, and rollback conditions for our transaction. We can also specify the transaction manager.
How does the @Transactional annotation work, how is it implemented?
Spring creates a proxy or manipulates the class byte-code to manage the creation, commit and rollback of the transaction.
Spring will wrap some transaction management code around the invocation @Transactional method called.
What is Transaction Propagation?
Propagation defines our business logic’s transaction boundary. Spring manages to start and pause a transaction according to our propagation setting.
Spring calls TransactionManager::getTransaction
to get or create a transaction according to the propagation
What are the different types of Propagation?
REQUIRED Propagation
SUPPORTS Propagation
MANDATORY Propagation
NEVER Propagation
NOT_SUPPORTED Propagation
REQUIRES_NEW Propagation
NESTED Propagation
Define REQUIRED Propagation for @Transactional
REQUIRED is the default propagation. Spring checks if there is an active transaction, and if nothing exists, it creates a new one. Otherwise, the business logic appends to the currently active transaction.
Define SUPPORTS Propagation for @Transactional
ForSUPPORTS, Spring first checks if an active transaction exists. If a transaction exists, then the existing transaction will be used. If there isn’t a transaction, it is executed non-transactional.
Define MANDATORY Propagation for @Transactional
When the propagation isMANDATORY, if there is an active transaction, then it will be used. If there isn’t an active transaction, then Spring throws an exception:
Define NEVER Propagation for @Transactional
For transactional logic withNEVER propagation, Spring throws an exception if there’s an active transaction.
Define NOT_SUPPORTED Propagation for @Transactional
If a current transaction exists, first Spring suspends it, and then the business logic is executed without a transaction.
Define REQUIRES_NEW Propagation for @Transactional
When the propagation is REQUIRES_NEW, Spring suspends the current transaction if it exists, and then creates a new one:
Define NESTED Propagation for @Transactional
For NESTED propagation, Spring checks if a transaction exists, and if so, it marks a save point. This means that if our business logic execution throws an exception, then the transaction rollbacks to this save point. If there’s no active transaction, it works like REQUIRED.
What is Transaction Isolation
Transaction Isolation describes how changes applied by concurrent transactions are visible to each other.
What are the different types of Transaction Isolation
- DEFAULT
- READ_UNCOMMITTED
- READ_COMMITTED
- REPEATABLE_READ
- SERIALIZABLE
What could the concurrency side effects of a transaction be?
- Dirty read:read the uncommitted change of a concurrent transaction
- Nonrepeatable read: get different value on re-read of a row if a concurrent transaction updates the same row and commits
- Phantom read:get different rows after re-execution of a range query if another transaction adds or removes some rows in the range and commits
@Transactional DEFAULT Isolation
The default isolation level isDEFAULT. As a result, when Spring creates a new transaction, the isolation level will be the default isolation of our RDBMS.