- Introduction to Messaging
- Components of the JMS Architecture
- The Two JMS Message Models
- The JMS Interfaces
- The Details of a JMS Message
- Message Selection and Filtering
- Using the JMS Point-to-Point Model
- Using the JMS Publish/Subscribe Model
- Synchronous Versus Asynchronous Messaging
- Message Persistence
- Using Transactions with JMS
- Using JMS with Enterprise JavaBeans
- Troubleshooting
Using Transactions with JMS
You learned about the Session interface back in the "The JMS Interfaces" section earlier. When using a transacted session, all messages that are produced and sent are performed as a unit of work. If anything happens that causes a need to roll back the transaction, all messages that are produced within that transaction are destroyed, and all the messages that are sent are recovered. This allows a client to treat a group of messages as an atomic unit. Either all of them succeed or none of them do.
You can use the session's commit or rollback methods to cause a session to succeed or fail, respectively. After a transaction is complete by calling either of the two methods, a new transaction is automatically created for the client.
There are three different methods for using a transaction in your JMS application:
Use a JMS transacted session
If you are using EJB with JMS, you should use a Java Transaction API (JTA) user transaction in a nontransacted JMS Session
Use message-driven beans
You've seen how to create a JMS transacted session earlier in the chapter.
→ Using user transactions with the JTA is beyond the scope of this chapter and is covered briefly later in the book. For more information on user transactions, see Chapter 12, "Transactions."
The next chapter covers the new message-driven bean and discusses how to use transactions with it.