Transactional Messaging

JMS supports transactional messaging in two ways. In its simplest form, a Session is created with the transactional option (the first argument to QueueConnection.createQueueSession() and TopicConnection.createTopicSession()):

    QueueSession xactSession =
      qConn.createQueueSession(TRUE, Session.AUTO_ACKNOWLEDGE);

When using a transactional Session, the client performs a series of “transactions” with the Session (sends or receives messages from consumers and producers associated with the Session). These sends and receives are either committed by calling the Session’s commit() method or cancelled by calling rollback(). If a Session is committed, all of the sends and receives are committed to the JMS provider, which causes the new state of the Destination(s) affected to be committed. If a Session is rolled back, all changes to the resources on the JMS provider are rolled back. In either case, the transaction is closed and a new one is started automatically, for any subsequent messaging actions.

JMS providers can also support transactional messaging through the Java Transaction API (JTA), which allows messaging transactions to be integrated with other resources, like databases. For broader coverage of JTA in a variety of contexts, see Chapter 16. The following material highlights some transactional details specific to JMS messaging.

JTA-based transactions are distributed: the underlying transactional resources can be distributed across the enterprise. The JMS ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.