Top 20 Spring Transactions Interview Questions and Answers

Top 20 Spring Transactions Interview Questions and Answers Work through these 20 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. What is CSRF and how does Spring Security handle it?

    CSRF tricks logged-in user's browser to POST to your site. Spring stores CsrfToken and validates on mutating requests. For SPA with session cookies, expose token via...

    Full answer

  2. Bean scopes in Spring — singleton vs prototype?

    Default singleton shares one instance — stateless services. Prototype for per-operation objects. Request/session scopes tie to web lifecycle with proxies. Prototype inside...

    Full answer

  3. How does @Transactional work?

    Spring creates a proxy around @Transactional beans. Calling createOrder() from another bean starts a transaction on the thread-bound EntityManager; commit on success, rollback...

    Full answer

  4. How do Spring application events work with @EventListener?

    OrderService publishes OrderCreatedEvent after save. EmailListener @EventListener sends confirmation. @TransactionalEventListener(AFTER_COMMIT) prevents email on rolled-back...

    Full answer

  5. Saga pattern — choreography vs orchestration?

    Order saga: reserve inventory → charge payment → ship. Failure at payment triggers release inventory compensation. Orchestration clearer for complex flows with visibility....

    Full answer

  6. Explain the transactional outbox pattern.

    OrderService @Transactional saves Order and OutboxEvent(type=ORDER_CREATED, payload=json). OutboxProcessor @Scheduled polls FOR UPDATE SKIP LOCKED, sends to Kafka, updates...

    Full answer

  7. Load shedding and backpressure in microservices?

    Tomcat max queue + reject policy returns 503 with Retry-After. WebClient timeout + circuit breaker stops calling slow dependency. Kafka consumer pause partitions when...

    Full answer

  8. What happens if @Transactional fails midway? How do you verify rollback?

    Default: runtime exception marks rollback-only; transaction commits nothing on exit. ```java @Transactional(rollbackFor = Exception.class) public void placeOrder(Order o) {...

    Full answer

  9. New deployment causes random 5xx errors. Rollback fixes it. Why?

    Compare metrics before/after: error rate, pool saturation, GC. Enable DEBUG temporarily in canary pod only. Check new dependency version, connection pool default change, or...

    Full answer

  10. How do you stop a LangChain4j agent loop from running forever, and who owns the workflow?

    I set maxSequentialToolsInvocations to a small number — three or four for triage. Writes take an idempotency key. Approve is not a @Tool. Automating a business process with AI...

    Full answer

  11. How does Spring Boot auto-configuration work?

    @SpringBootApplication enables @EnableAutoConfiguration, which loads candidate configs from starters. Each auto-config uses @ConditionalOn* to register beans only when...

    Full answer

  12. Describe the Spring Bean lifecycle.

    Container instantiates the bean, injects dependencies, runs BeanPostProcessors, then @PostConstruct or InitializingBean. Singleton lives for context lifetime; prototype creates...

    Full answer

  13. What is the N+1 query problem and how do you fix it?

    Loading 100 orders then accessing each order's items triggers 101 queries. Fix with JOIN FETCH in JPQL, @EntityGraph on repository method, or a DTO query selecting only...

    Full answer

  14. What is dependency injection and why does Spring use it?

    DI supplies collaborators from outside the class — typically via constructor. Problem solved: tight coupling to concrete classes, hard unit tests, scattered object...

    Full answer

  15. @RestController vs @Controller?

    @Controller is for traditional MVC — return "user/list" view name. @RestController adds @ResponseBody so return types become JSON/XML body — standard for REST APIs. Use...

    Full answer

  16. Explain JWT authentication flow in Spring Security.

    POST /login with credentials → auth provider validates → issue JWT with exp/sub/roles. Subsequent requests include Bearer token. JwtAuthenticationFilter runs before...

    Full answer

  17. What are Spring Boot starters?

    spring-boot-starter-web brings spring-webmvc, Tomcat, Jackson with tested versions and enables WebMvcAutoConfiguration. You declare one starter instead of 10 coordinates....

    Full answer

  18. How do you handle exceptions globally in Spring?

    Create @RestControllerAdvice with @ExceptionHandler(MethodArgumentNotValidException.class) returning 400 + field map, NotFoundException → 404, generic Exception → 500 without...

    Full answer

  19. Difference between @Component, @Service, @Repository?

    All register as singleton beans via component scan. Choose by layer for readability: @Repository on DAOs (plus SQLException translation), @Service on business logic, @Component...

    Full answer

  20. What is the difference between JPA and Hibernate?

    JPA is the API contract; Hibernate is the engine under Spring Boot by default. You write @Entity per JPA spec; Hibernate handles SQL generation. Spring Data JpaRepository adds...

    Full answer

Spring Transactions Interview Questions and Answers · Top 50 Spring Transactions Interview Questions and Answers (2026 Edition) · Top 40 Spring Transactions Interview Questions | Must-Prepare before interview. · Spring Transactions Interview Questions Every Fresher Must Know

All interview question lists · Interview guides · Home · Support the platform