0
votes

I have JMS queue implementation in my project in which I am sending 100's of messages in one transaction but performing some DB operations before putting it in queue. i.e

//SuedoCode

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void sendMsg(List orders )
{
   for(Order order : orders)
  {
    order.setStatus("SENT");
    sendToQueue(order);
   }

}

But this transaction is still not committed and receiver picks up the orders before sender's transaction is committed. Now receiver process the messages and change the status again then commits but after that senders transaction commits and it overrides the status which should not happen.

Thus to solve this problem I created a new class (For spring proxy) which has method to change the status of order and this method is in REQUIRES_NEW transaction so the status has changed but If any error occur while sending the message to queue then again status needs to changed (because the previous transaction has already committed). Please suggest me If this approach is correct or something better can be done. Thanks in advance

1

1 Answers

0
votes

The messages queued need to be part of the same transaction, so that everything gets committed at once.

Lacking that, another solution is to stored updated orders in a list and call sendToQueue outside this method as soon as your sure the transaction has been committed and database has been updated.