View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jms;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.transaction.TransactionException;
11  import org.mule.config.i18n.CoreMessages;
12  import org.mule.transaction.AbstractSingleResourceTransaction;
13  import org.mule.transaction.IllegalTransactionStateException;
14  import org.mule.transport.jms.i18n.JmsMessages;
15  
16  import javax.jms.Connection;
17  import javax.jms.JMSException;
18  import javax.jms.Session;
19  
20  /**
21   * <code>JmsTransaction</code> is a wrapper for a JMS local transaction. This
22   * object holds the JMS session and controls when the transaction is committed or
23   * rolled back.
24   */
25  public class JmsTransaction extends AbstractSingleResourceTransaction
26  {
27  
28      public JmsTransaction(MuleContext muleContext)
29      {
30          super(muleContext);
31      }
32  
33      public void bindResource(Object key, Object resource) throws TransactionException
34      {
35          if (!(key instanceof Connection) || !(resource instanceof Session))
36          {
37              throw new IllegalTransactionStateException(
38                  CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
39          }
40  
41          Session session = (Session)resource;
42          try
43          {
44              if (!session.getTransacted())
45              {
46                  throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted());
47              }
48          }
49          catch (JMSException e)
50          {
51              throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
52          }
53  
54          super.bindResource(key, resource);
55      }
56  
57      protected void doBegin() throws TransactionException
58      {
59          // do nothing
60      }
61  
62      protected void doCommit() throws TransactionException
63      {
64          if (resource == null)
65          {
66              logger.warn(CoreMessages.commitTxButNoResource(this));
67              return;
68          }
69  
70          try
71          {
72              ((Session) resource).commit();
73          }
74          catch (JMSException e)
75          {
76              throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
77          }
78          finally
79          {
80              try
81              {
82                  ((Session) resource).close();
83              }
84              catch (JMSException e)
85              {
86                  logger.warn("could not close jms session", e);
87              }
88          }
89      }
90  
91      protected void doRollback() throws TransactionException
92      {
93          if (resource == null)
94          {
95              logger.warn(CoreMessages.rollbackTxButNoResource(this));
96              return;
97          }
98  
99          try
100         {
101             if (logger.isDebugEnabled())
102             {
103                 logger.debug("Rolling back transaction: " + getId());
104             }
105             ((Session) resource).rollback();
106         }
107         catch (JMSException e)
108         {
109             throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
110         }
111         finally
112         {
113             try
114             {
115                 ((Session) resource).close();
116             }
117             catch (JMSException e)
118             {
119                 logger.warn("could not close jms session", e);
120             }
121         }
122     }
123 
124 }