Quick revision
The short answer
With sessionTransacted=true, the listener container acknowledges a message only when our method returns normally, and an exception rolls it back for redelivery, so a crash never loses the message. The database commit is still separate, so a crash between the two commits causes a redelivery of a message whose changes are already saved. We can close that gap with XA and a JTA transaction manager, at a real cost, or, more commonly, keep local transactions and make the consumer idempotent by recording processed message ids in the same database transaction, with broker redelivery limits and a dead-letter destination for messages that keep failing.
What to remember
In this answer, we saw that sessionTransacted=true acknowledges a message only after our listener returns, so a crash never loses it. Because the database and broker commit separately, with the database first, a crash between them causes a redelivery of work that is already saved. XA with a JTA transaction manager can make both commits atomic but adds cost and recovery problems. The usual answer is local transactions plus an idempotent consumer that stores processed ids in the same database transaction, together with redelivery limits and a dead-letter destination on the broker.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With sessionTransacted=true, the listener container acknowledges a message only when our method returns normally, and an exception rolls it back for redelivery, so a crash never loses the message. The database commit is still separate, so a crash between the two commits causes a redelivery of a message whose changes are already saved. We can close that gap with XA and a JTA transaction manager, at a real cost, or, more commonly, keep local transactions and make the consumer idempotent by recording processed message ids in the same database transaction, with broker redelivery limits and a dead-letter destination for messages that keep failing.
Why this question is asked
Every message-driven service eventually crashes at the worst moment: after it changed the database but before the broker heard that the message was done. Whether the result is a lost message, a duplicate payment or nothing at all depends on choices we make up front. Interviewers ask this to see whether we understand where the gap between two systems lies and how to close it at a reasonable price. In this answer, we will recap acknowledgement and local transactions, find exactly where the crash window is, look at XA and why it is rarely chosen, build an idempotent consumer, and finish with redelivery limits.
1A Quick Recap of Acknowledgements and Local Transactions
When a consumer receives a JMS message, the broker keeps it until the consumer acknowledges it. If the consumer's connection drops before that, the broker gives the message to someone else. A transacted session makes this explicit: receiving is part of a local transaction (a transaction that covers only this broker), and the message is acknowledged when the session commits and returned to the queue when it rolls back.
In Spring, setting sessionTransacted to true on the listener container (or the container factory) turns this on. The DefaultMessageListenerContainer receives the message inside a transacted session, calls our @JmsListener method, and commits the session if the method returns normally. If the method throws, it rolls back, and the broker redelivers the message.
So a crash before our method returns is safe: the message was never acknowledged and will come back. That covers loss. It does not cover duplicates, as the next step shows.
2Where the Crash Window Is
Our listener also writes to the database, inside its own JPA transaction. Now there are two commits, one in the database and one in the broker, and they happen one after the other. Spring orders them so the database commits first, while the method runs, and the JMS session commits after the method returns.
7 messages in order. Play them, or step through them one at a time.
The redelivery is correct from the broker's view. Only the consumer can tell it already did the work.
Notice that this order is chosen on purpose. If the broker committed first and then the database failed, the message would be gone and the work lost. Committing the database first means the worst case is a duplicate, never a loss. A duplicate is something we can detect; a loss is not. This approach is sometimes called best-effort one-phase commit.
3Option 1: XA and a JTA Transaction Manager
XA is a protocol for two-phase commit across several resources. A JTA transaction manager (such as Narayana, or the one in an application server) asks both the database and the broker to prepare, and only when both say yes does it tell both to commit. With XA connection factories and Spring's JtaTransactionManager set on the listener container, the message acknowledgement and the database change commit as one.
It works, but it costs a lot. Each message needs extra round trips and log writes for the prepare phase, often halving throughput. Both drivers must support XA properly, and the transaction manager needs a durable log. And when a crash happens during the commit phase, transactions are left in doubt and must be recovered by the transaction manager or, sometimes, by hand. Recent Spring Boot versions also leave standalone JTA setup to third-party starters, so check your version. For a single listener, this is usually more machinery than the problem deserves.
4Option 2: Local Transactions Plus an Idempotent Consumer
The common choice keeps sessionTransacted=true and makes the second delivery harmless. In the same database transaction as the business change, we insert the message's id into a processed_messages table with a unique key. If the insert fails because the id is already there, we have done this work before, and we simply return so the container acknowledges the duplicate.
Because the id and the debit commit together, either both are saved or neither is. On redelivery the insert returns 0 and the debit is skipped. Notice that we use a business id, paymentId, rather than the JMSMessageID: if the producer itself resends after a timeout, the broker gives the copy a new JMSMessageID, but the payment id stays the same. We also delete old rows on a schedule, keeping them longer than the longest possible redelivery delay.
5Redelivery Limits and a Dead-Letter Destination
There is one more failure: a message that throws every time, for example because its data is invalid. With sessionTransacted=true it would roll back and come back forever, blocking or slowing the queue. The broker handles this. In ActiveMQ Artemis, the address settings set max-delivery-attempts (10 by default) and a dead-letter-address; after the last attempt, the message moves to the dead-letter queue. Other brokers have equivalent settings, and the JMSXDeliveryCount property tells the listener how many times a message has been delivered.
We monitor the dead-letter queue and alert when it grows, because each message there is work that did not happen. We also add a short redelivery delay so a briefly unavailable database is not hit with immediate retries.
6Follow-up questions
Why not use CLIENT_ACKNOWLEDGE and acknowledge after the database commit?
It gives the same ordering as sessionTransacted, database first and then the acknowledgement, and the same crash window, so it still needs idempotency. It is also easier to get wrong, because acknowledging one message in a session acknowledges all earlier ones in that session. Transacted sessions with the listener container are simpler.
Can I wrap the JMS session in the same JpaTransactionManager?
No. A JPA transaction manager only controls the database. Spring can synchronise the two so the commits happen close together, in the order we saw, but they remain two commits. Only an XA transaction manager makes them one atomic unit.
How long should processed message ids be kept?
At least as long as a duplicate can still arrive: the broker's longest redelivery delay, plus any time a producer may resend, plus a margin. Many teams keep a few days and delete older rows with a scheduled job, or partition the table by day so old partitions can be dropped cheaply.
7Conclusion
In this answer, we saw that sessionTransacted=true acknowledges a message only after our listener returns, so a crash never loses it. Because the database and broker commit separately, with the database first, a crash between them causes a redelivery of work that is already saved. XA with a JTA transaction manager can make both commits atomic but adds cost and recovery problems. The usual answer is local transactions plus an idempotent consumer that stores processed ids in the same database transaction, together with redelivery limits and a dead-letter destination on the broker.
Open with
With sessionTransacted set to true on the listener container, the message is received in a transacted JMS session and only acknowledged when my listener returns normally; if it throws, the session rolls back and the broker redelivers.
Then, if they want more
So a crash never loses the message. But the database has its own transaction, and Spring commits it first, then the JMS session. If the service crashes between those two commits, the database change is saved and the message comes back, so without care I'd apply it twice. That ordering is deliberate: a duplicate is detectable, a loss isn't.
One option is XA with a JTA transaction manager, which makes both commits atomic through two-phase commit. I'd avoid it unless I truly need it, because it slows every message, needs XA support in both drivers and a transaction log, and in-doubt transactions after crashes need recovery.
What I'd normally do is keep local transactions and make the consumer idempotent: in the same database transaction as the business change, insert the business id of the message into a processed-messages table with a unique key. On a redelivery the insert finds the row and I skip the work.
Finally I'd configure the broker with a redelivery limit, a small delay and a dead-letter queue, and alert when that queue grows.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- RabbitMQ documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A JMS listener processes a message, updates a database, and must not lose or...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/8edeb5de89/