View Javadoc

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