JMS transaction

蓝咒 提交于 2019-12-10 04:28:36

问题


Database transactions is a familiar concept.

try {
  ...
  ..
  updateDB()
  ..
  ...
  commit();
} catch error {
  rollback();
}

if any error occurs any changes made by updateDB will be discarded.

I wanted to know what a message queue transaction rollback will undo.

try{
  ...
  ...
  //EDIT: swapped the order of receive and send
  Message m = queue1.receiveMessage(..)
  ..
  ..
  queue2.sendMessage(..)
  ..
  ..
  commit();
} catch error {
  rollback();
}

specifically, what will rollback do

  1. cancel the sending of message
  2. un-receive the message ie put back the received message back to queue

or am i stretching the database tx analogy too far.

thanks

EDIT: i am not implying the send and receive operations are related. i just wanted to say there are two operations that change the state of the message broker -- receive will take out a message from the queue which will be unavailable for other consumers if there were any.


回答1:


Rollback of the send is straight forward, the message will not be put to queue2.

Rollback of the receive will typically put the message back on the queue (queue1). Depending on your JMS provider setup and configuration, the message will be redelivered a number of times. If the transaction rolls back too many times (too many is configurable) it will be put to a "Backout queue" (or dead letter queue), so that it will not block the queue for other messages. A backed out message typically needs some manual error handling.




回答2:


Yes, you are stretching it too far.

In transacted mode, your queue.receiveMessage() will never return (assuming that it is set to wait for a particular reply message, and not just "any" message), simply because queue.sendMessage() didn't really send the message yet (it will be sent when the transaction is committed).

It's a common mistake, by the way. When using JMS (which is an asynchronous protocol) for synchronous communications, it is natural to view the send/receive cycle as consisting of one transaction. That, however, isn't the case. Once you issue sendMessage() in transacted mode, nothing really happens yet; the message will be sent only when the transaction is committed.



来源:https://stackoverflow.com/questions/13890287/jms-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!