Step 1: Set Up JMS Application, Creating Transacted Session Step 2: Perform Desired Operations Step 3: Commit or Roll Back the JMS Transacted Session

12-2 Programming JMS for Oracle WebLogic Server ■ If you are mixing other operations, such as EJB, with JMS operations, you should use a Java Transaction API JTA user transaction in a non-transacted JMS session. ■ Use message driven beans. The following sections explain how to use a JMS transacted session and JTA user transaction.

12.2 Using JMS Transacted Sessions

A JMS transacted session supports transactions that are located within the session. A JMS transacted sessions transaction will not have any effects outside of the session. For example, rolling back a session will roll back all sends and receives on that session, but will not roll back any database updates. JTA user transactions are ignored by JMS transacted sessions. Transactions in JMS transacted sessions are started implicitly, after the first occurrence of a send or receive operation, and chained together—whenever you commit or roll back a transaction, another transaction automatically begins. Before using a JMS transacted session, the system administrator should adjust the connection factory Transaction Timeout andor session pool Transaction attributes, as necessary for the application development environment. The following figure illustrates the steps required to set up and use a JMS transacted session. Figure 12–1 Setting Up and Using a JMS Transacted Session

12.2.1 Step 1: Set Up JMS Application, Creating Transacted Session

Set up the JMS application as described in Section 5.2, Setting Up a JMS Application, however, when creating sessions, as described in Section 5.2.3, Step 3: Create a Session Using the Connection, specify that the session is to be transacted by setting the transacted boolean value to true. For example, the following methods illustrate how to create a transacted session for the PTP and Pubsub messaging models, respectively: Note: When using transactions, it is recommended that you define a session exception listener to handle any problems that occur before a transaction is committed or rolled back, as described in Section 6.3.1, Defining a Connection Exception Listener. If the acknowledge method is called within a transaction, it is ignored. If the recover method is called within a transaction, a JMSException is thrown. Using Transactions with WebLogic JMS 12-3 qsession = qcon.createQueueSession true, Session.AUTO_ACKNOWLEDGE ; tsession = tcon.createTopicSession true, Session.AUTO_ACKNOWLEDGE ; Once defined, you can determine whether or not a session is transacted using the following session method: public boolean getTransacted throws JMSException

12.2.2 Step 2: Perform Desired Operations

Perform the desired operations associated with the current transaction.

12.2.3 Step 3: Commit or Roll Back the JMS Transacted Session

Once you have performed the desired operations, execute one of the following methods to commit or roll back the transaction. To commit the transaction, execute the following method: public void commit throws JMSException The commit method commits all messages sent or received during the current transaction. Sent messages are made visible, while received messages are removed from the messaging system. To roll back the transaction, execute the following method: public void rollback throws JMSException The rollback method cancels any messages sent during the current transaction and returns any messages received to the messaging system. If either the commit or rollback methods are issued outside of a JMS transacted session, a IllegalStateException is thrown.

12.3 Using JTA User Transactions