Spring Boot Transaction Management Concepts 2 — Questions and Answers
Question 1: What happens when a @Transactional method calls another @Transactional method within the same class?
- The inner method starts a new independent transaction
- The inner transaction is ignored due to Spring proxy limitations (Correct answer)
- Both methods share the same transaction by default
- Spring throws a TransactionRequiredException
Correct answer: The inner transaction is ignored due to Spring proxy limitations
Spring's @Transactional uses proxy-based AOP, so self-invocations bypass the proxy and the inner method's transaction annotation is ignored.
Question 2: Which propagation behavior creates a new transaction and suspends the current one?
- REQUIRED
- NESTED
- REQUIRES_NEW (Correct answer)
- MANDATORY
Correct answer: REQUIRES_NEW
REQUIRES_NEW always starts a new transaction and suspends any existing transaction until the new one completes.
Question 3: What is the default isolation level used by Spring's @Transactional when set to DEFAULT?
- READ_COMMITTED
- READ_UNCOMMITTED
- REPEATABLE_READ
- The underlying database's default isolation level (Correct answer)
Correct answer: The underlying database's default isolation level
Isolation.DEFAULT defers to the default isolation level configured in the underlying database driver.
Question 4: Which exception type does NOT trigger a rollback by default in Spring's @Transactional?
- RuntimeException
- Error
- IllegalArgumentException
- IOException (Correct answer)
Correct answer: IOException
By default, Spring only rolls back on unchecked exceptions (RuntimeException and Error); checked exceptions like IOException do not trigger rollback.
Question 5: What does the 'readOnly = true' attribute on @Transactional do?
- Prevents any database writes at the JDBC level
- Provides a hint to the database and ORM to optimize for read-only access (Correct answer)
- Throws an exception if any write operation is attempted
- Disables transaction management entirely
Correct answer: Provides a hint to the database and ORM to optimize for read-only access
readOnly=true is a hint that allows the database and Hibernate to apply optimizations like skipping dirty checking, but doesn't enforce a hard write restriction.
Question 6: Which Spring transaction manager should be used in a plain JDBC application without JPA?
- JpaTransactionManager
- DataSourceTransactionManager (Correct answer)
- JtaTransactionManager
- HibernateTransactionManager
Correct answer: DataSourceTransactionManager
DataSourceTransactionManager manages transactions against a single JDBC DataSource without requiring JPA or Hibernate.
Question 7: What is a 'dirty read' in the context of database transaction isolation?
- Reading data that was written by the current transaction
- Reading uncommitted changes made by another transaction (Correct answer)
- Reading stale data from a cache instead of the database
- Reading data that has been deleted in the current session
Correct answer: Reading uncommitted changes made by another transaction
A dirty read occurs when a transaction reads data that another concurrent transaction has modified but not yet committed.
What happens when a @Transactional method calls another @Transactional method within the same class?