Spring Boot Transaction Management Concepts 4 — Questions and Answers
Question 1: What does TransactionSynchronizationManager allow developers to do?
- Programmatically demarcate transaction boundaries without annotations
- Register callbacks that execute at specific phases of the current transaction lifecycle (Correct answer)
- Switch between transaction managers at runtime
- Inspect and modify transaction isolation levels mid-transaction
Correct answer: Register callbacks that execute at specific phases of the current transaction lifecycle
TransactionSynchronizationManager lets you register TransactionSynchronization callbacks that fire on events like commit, rollback, or completion.
Question 2: When using programmatic transaction management, which Spring interface provides the core API?
- TransactionDefinition
- PlatformTransactionManager (Correct answer)
- TransactionStatus
- TransactionTemplate
Correct answer: PlatformTransactionManager
PlatformTransactionManager defines the core contract — getTransaction(), commit(), and rollback() — used by all Spring transaction managers.
Question 3: What is the role of TransactionTemplate in Spring?
- An annotation processor that replaces @Transactional at compile time
- A template that simplifies programmatic transaction management by handling boilerplate (Correct answer)
- A JDBC template specifically designed for transactional batch inserts
- A proxy factory that creates transactional wrappers for any object
Correct answer: A template that simplifies programmatic transaction management by handling boilerplate
TransactionTemplate wraps programmatic transaction logic, handling begin/commit/rollback automatically so you only provide the business logic via a callback.
Question 4: Which of the following describes a 'non-repeatable read' anomaly?
- A transaction reads rows that did not exist at the start of the transaction
- A transaction reads the same row twice and gets different values due to an intervening update (Correct answer)
- A transaction reads data that was never committed
- A read fails because the row was locked by the same transaction
Correct answer: A transaction reads the same row twice and gets different values due to an intervening update
A non-repeatable read occurs when a row read once returns different data on a second read because another transaction committed an update in between.
Question 5: What Spring Boot property configures the default transaction timeout?
- spring.transaction.default-timeout (Correct answer)
- spring.jpa.transaction.timeout
- spring.datasource.timeout
- spring.tx.timeout
Correct answer: spring.transaction.default-timeout
spring.transaction.default-timeout sets the default timeout (in seconds) applied to all @Transactional methods that don't specify their own timeout.
Question 6: In Spring Data, where should @Transactional typically be placed to follow best practices?
- On every repository method individually
- On the service layer that orchestrates multiple repository calls (Correct answer)
- On the repository interface class declaration only
- On the entity class itself
Correct answer: On the service layer that orchestrates multiple repository calls
Placing @Transactional on the service layer ensures a single transaction spans all repository calls that belong to the same business operation.
Question 7: What does the noRollbackFor attribute on @Transactional do?
- Suppresses exceptions so they are never propagated to the caller
- Prevents rollback when specific exception types are thrown, allowing commit instead (Correct answer)
- Disables transaction support for the annotated method entirely
- Marks certain exceptions as retriable, causing automatic retry on failure
Correct answer: Prevents rollback when specific exception types are thrown, allowing commit instead
noRollbackFor lists exception types that, when thrown, should NOT trigger a rollback — the transaction proceeds to commit despite the exception.
What does TransactionSynchronizationManager allow developers to do?