Java Message Service: Transacted Session

A transaction is the atomic unit of many operations, such as message sends and receives, so that the operations either all succeed or all fail. An application can participate in such transactions by creating a transacted session (or local transaction). The application completely controls the message delivery by either committing or rolling back the session. If all the operations succeed, the transaction is committed. If any one of the operations fails, the transaction is rolled back, and the operations may be attempted again from the beginning.

Think about an application that reliably copies all the messages from a source queue to a destination queue. So, receiving a message from the source and sending it to the destination queue must either occur together or fail both. For this purpose, it can create a transacted session as follows:

sn = con.createSession(true, Session.AUTO_ACKNOWLEDGE);

The complete source code ( is shown below:

import javax.jms.*;

public class Copier implements MessageListener {

Session sn;

MessageProducer mp; public Copier(String args[]) {

try {

ConnectionFactory cf = new com.sun.messaging.ConnectionFactory();

((com.sun.messaging.ConnectionFactory)cf).setProperty(com.sun.messaging.Connect ionConfiguration.imqAddressList,”″);

Connection con = cf.createConnection();

sn = con.createSession(true, Session.AUTO_ACKNOWLEDGE);

Destination from = sn.createQueue(args[0]);

MessageConsumer mc = sn.createConsumer(from);

Destination to = sn.createQueue(args[1]);

mp = sn.createProducer(to); con.start();


} catch (Exception e) { e.printStackTrace(); }


public static void main(String[] args) { new Copier(args);


public void onMessage(Message msg) { try {



} catch (Exception e) { try {

sn.rollback() ;

}catch(Exception el) {e1.printStackTrace();}




Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *