Spring Boot Transaction Management Concepts 3 — Questions and Answers
Question 1: What does the NESTED propagation behavior do when there is an existing transaction?
- Creates a completely independent new transaction
- Uses a savepoint within the existing transaction allowing partial rollback (Correct answer)
- Joins the existing transaction with no isolation
- Throws an exception indicating nesting is not allowed
Correct answer: Uses a savepoint within the existing transaction allowing partial rollback
NESTED creates a savepoint within the existing transaction so the nested operation can be rolled back independently without affecting the outer transaction.
Question 2: When using Spring Boot with Spring Data JPA, which transaction manager is auto-configured?
- DataSourceTransactionManager
- JtaTransactionManager
- JpaTransactionManager (Correct answer)
- ChainedTransactionManager
Correct answer: JpaTransactionManager
Spring Boot auto-configures JpaTransactionManager when spring-data-jpa is on the classpath and an EntityManagerFactory is present.
Question 3: What is a 'phantom read' in database transactions?
- Reading a row that was deleted in the same transaction
- A query returning different sets of rows due to inserts by another concurrent transaction (Correct answer)
- Reading the same row twice and getting different values
- A read that fails due to a network partition
Correct answer: A query returning different sets of rows due to inserts by another concurrent transaction
A phantom read happens when a transaction re-executes a range query and sees new rows inserted by another committed transaction.
Question 4: How can you configure @Transactional to roll back on a specific checked exception?
- Use rollbackOn = CheckedException.class
- Use rollbackFor = CheckedException.class (Correct answer)
- Wrap the checked exception in a RuntimeException first
- Override the DefaultRollbackRule in TransactionInterceptor
Correct answer: Use rollbackFor = CheckedException.class
The rollbackFor attribute accepts exception classes and instructs Spring to roll back the transaction when those exceptions are thrown.
Question 5: Which isolation level prevents dirty reads and non-repeatable reads but still allows phantom reads?
- READ_UNCOMMITTED
- READ_COMMITTED
- REPEATABLE_READ (Correct answer)
- SERIALIZABLE
Correct answer: REPEATABLE_READ
REPEATABLE_READ prevents dirty and non-repeatable reads by locking read rows, but does not prevent phantom reads from new row insertions.
Question 6: What is the purpose of the MANDATORY propagation behavior?
- Forces Spring to always create a new transaction
- Requires an existing transaction and throws an exception if none exists (Correct answer)
- Makes transaction participation optional
- Marks the method as requiring distributed transaction support
Correct answer: Requires an existing transaction and throws an exception if none exists
MANDATORY propagation requires an active transaction to already exist; if none is found, Spring throws an IllegalTransactionStateException.
Question 7: In Spring, what class is commonly used as the superclass for custom application exceptions that should NOT trigger rollback?
- RuntimeException
- Exception (checked) (Correct answer)
- TransactionException
- DataAccessException
Correct answer: Exception (checked)
Checked exceptions (extending Exception) do not trigger rollback by default in Spring, making them suitable for business exceptions that should allow commit.
What does the NESTED propagation behavior do when there is an existing transaction?