6.9. Using Transactions in Java JMS

6.9. Using Transactions in Java JMS

MRG Messaging supports standard Java JMS transacted sessions. When a session is created it can be made transactional by setting the first parameter of createSession() to true:

Session transactedSession = connection.createSession(true, Session.SESSION_TRANSACTED);

Producers and consumers that are created by a transacted session are transactional, and are governed by commits and rollbacks made to that session:

MessageConsumer transactedConsumer = transactedSession.createConsumer(queue);
MessageProducer transactedProducer = transactedSession.createProducer(topic);

For example, in the following code, either the messages received and sent are both committed, or they are both rolled back:

receivedMessage = transactedConsumer.receive();
transactedProducer.send(receivedMessage);

if (_commit)
{
    transactedSession.commit();
}
else
{
    transactedSession.rollback();
}