Transactions
36 questions in 9 subtopics.
@Transactional semantics
What are the default settings of@Transactional (propagation, isolation, timeout, read-only, rollback) and which method visibilities are supported?How does @Transactional work in Spring? Describe what happens from the moment a caller invokes an annotated method on a bean.A @Transactional service method saves an order and then calls an external payment API that takes up to 10 seconds. What problems does this cause, and how would you restructure it?Set rules for where @Transactional belongs in a layered service architecture (controllers, application services, domain services, repositories). Explain the reasoning.Declarative transactions with @Transactional made it easy for developers to ignore transaction boundaries. Looking at distributed systems built over 15 years, how should an organisation think about transactional consistency as systems spread across services?Propagation
Explain REQUIRED,REQUIRES_NEW, NESTED, SUPPORTS, NOT_SUPPORTED, MANDATORY and NEVER propagation.A @Transactional outer method calls inner() on another bean, also @Transactional (REQUIRED). inner() throws a RuntimeException, which the outer method catches and logs. At commit the outer method fails with UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only. Explain.You need to write an audit record that must be saved even if the business transaction rolls back. Write the code and explain the risks.A method annotated @Transactional(propagation = NESTED) works with JdbcTemplate but throws NestedTransactionNotSupportedException after the team moves to JPA with JpaTransactionManager. Why, and what are the alternatives?Code reviews show REQUIRES_NEW used widely to 'make sure things get saved'. What problems does this cause at scale, and what alternatives would you recommend as a standard?Isolation levels
Describe the four SQL isolation levels and the anomalies each allows. What are the default levels in PostgreSQL and MySQL InnoDB?How does PostgreSQL's REPEATABLE READ differ from MySQL InnoDB's REPEATABLE READ when two transactions update the same row?Two doctors must always be on call. Each transaction checks that at least two are on call, then sets one doctor off call. Both run concurrently under READ COMMITTED and both succeed, leaving nobody on call. Name the anomaly and the ways to prevent it.Teams pick isolation levels inconsistently, and some set SERIALIZABLE everywhere after a bug. How would you set guidance for isolation and concurrency control across the organisation?Many teams treat the database as a simple store and handle consistency in application code. Over a decade, what does this cost, and how would you rebalance where invariants are enforced?Rollback rules
Inside a@Transactional method you catch an exception and want the transaction to roll back without rethrowing. How do you do it?A @Transactional method throws a checked IOException after writing two rows, but the rows are committed. Why, and how do you change it?A team uses noRollbackFor on a business exception so that a failure record is saved, but now partial order data is also committed when that exception occurs. How would you redesign it?Bugs from committed partial data after checked exceptions keep appearing in Java and Kotlin services. Would you change the rollback default for the organisation, and how?Self-invocation and proxies
Why does@Transactional on a private method have no effect, and which other Spring annotations share the same limitation?In this class, saveRow() runs without a transaction when called from importFile(), even though saveRow() is annotated @Transactional:@Service
class ImportService {
public void importFile(File f) {
for (Row r : parse(f)) saveRow(r);
}
@Transactional
public void saveRow(Row r) { ... }
}
@Transactional on the implementation class works when the bean is injected by its interface but injecting by the concrete class fails. Why, and how does Spring Boot avoid this by default?Read-only transactions
What does@Transactional(readOnly = true) do with Spring, Hibernate and the JDBC driver? Does it prevent writes?Design read replica routing in a Spring Boot service so that read-only transactions go to a replica and others go to the primary. What pitfalls exist?Your read traffic is ten times write traffic and the primary database is overloaded. Teams propose routing all readOnly = true transactions to replicas. What risks and rules would you set?TransactionTemplate
Rewrite a method so that only part of it runs in a transaction, usingTransactionTemplate.Write code that retries a whole transaction up to three times when an ObjectOptimisticLockingFailureException occurs.When would you prefer TransactionTemplate over @Transactional? Give examples from batch jobs, retries and loops.Transactions and async
Inside a@Transactional method, code calls CompletableFuture.runAsync(() -> repo.save(entity)). The save is not part of the transaction and sometimes fails with LazyInitializationException. Why?After an order is committed you need to send an email and publish an event, but never if the transaction rolls back. Show how to do this in Spring.Design reliable publishing of domain events to Kafka from services that write to PostgreSQL, so that no event is lost and none is published for rolled-back changes.Event-driven architecture is being promoted as the default for all new systems. What long-term costs and risks related to transactions and consistency would you raise before adopting it company-wide?Long transactions
A PostgreSQL database shows table bloat and slow queries, and pg_stat_activity shows sessions 'idle in transaction' for hours from one Spring service. What is the likely cause and the fix?A nightly job updates 10 million rows in a single@Transactional method. It holds locks for an hour and blocks daytime traffic when it runs late. How would you redesign it?Several incidents trace back to long transactions: external calls inside transactions, giant batch transactions and OSIV. What engineering standards and checks would you introduce?Your monolith relied on large database transactions for consistency. As it is split into services, cross-service operations lose atomicity. What long-term consistency strategy would you set, and how do you decide where to keep data together?No question matches these choices.