How do you implement distributed transactions in Spring?
Spring supports distributed transactions through JtaTransactionManager and XA resources, but microservices usually need sagas and the transactional outbox instead.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Spring supports distributed transactions through JtaTransactionManager and XA resources, but microservices usually need sagas and the transactional outbox instead.
Queues deliver a message to one consumer, topics deliver it to every subscriber. See the differences with Jakarta Messaging examples and broker mappings.
Use Spring AMQP for RabbitMQ or Spring Kafka for Kafka. See configuration, producers, consumers and how to choose between the two brokers.
Spring AMQP gives Spring apps a template-and-listener model for RabbitMQ. See its core components, Java configuration, JSON messages and retry with dead-letter queues.
Add spring-boot-starter-amqp, declare the queue and put @RabbitListener on a bean method. Includes JSON conversion, retries and dead-lettering.
@EnableRabbit activates @RabbitListener processing in Spring AMQP. Boot applies it for you, so see when you need it and how to configure RabbitMQ.
Declare queues and bindings, send JSON with RabbitTemplate and consume with @RabbitListener, then add retries and dead-letter handling.
A message broker lets services communicate asynchronously. See how Spring integrates RabbitMQ, Kafka and STOMP, and how to choose between them.
ApplicationEventPublisher lets Spring beans publish events that listeners handle without direct coupling. See publishEvent, @EventListener and transactional events.
Inject ApplicationEventPublisher, call publishEvent with a record, and react with @EventListener. Covers transaction-bound and async listeners.
@TransactionalEventListener runs a Spring listener after a transaction commits or rolls back, so side effects happen only when data is saved. Phases and examples.
Add a @Version field so JPA detects concurrent updates, then handle OptimisticLockException with retries or a conflict response.
@Version enables optimistic locking in JPA. See how to use it with Spring Data repositories, retry on conflicts and pass the version through REST ETags.
Plain JPA offers only lifecycle callbacks. Learn to track creation and modification time and user with callbacks and Spring Data JPA auditing.
Learn how JPA @PrePersist and @PostPersist callbacks work: when they fire, how to set defaults and timestamps, entity listeners and common pitfalls.
JPA lifecycle callbacks run methods before or after an entity is persisted, updated, removed or loaded. Learn the seven annotations, listeners and their limits.
@EntityListeners links listener classes to JPA lifecycle events. See the callback signatures, execution order and rules for what callbacks may do.
Learn to call methods through the Java Reflection API, handle static, private and varargs cases, unwrap exceptions and know when MethodHandle is better.
Reflection is slower than direct calls due to lookups, boxing and lost inlining. Learn how to cache, use MethodHandle, and measure the real cost with JMH.
Get a Class object with a literal, getClass or Class.forName, then inspect its fields, methods, constructors and annotations with java.lang.reflect.