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.transaction;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.transaction.ExternalTransactionAwareTransactionFactory;
11  import org.mule.api.transaction.Transaction;
12  import org.mule.api.transaction.TransactionException;
13  import org.mule.config.i18n.CoreMessages;
14  
15  import javax.transaction.TransactionManager;
16  
17  /**
18   * <code>XaTransactionFactory</code> Is used to create/retrieve a Transaction from
19   * a transaction manager configured on the MuleManager.
20   */
21  public class XaTransactionFactory implements ExternalTransactionAwareTransactionFactory
22  {
23      public Transaction beginTransaction(MuleContext muleContext) throws TransactionException
24      {
25          try
26          {
27              XaTransaction xat = new XaTransaction(muleContext);
28              xat.begin();
29              return xat;
30          }
31          catch (Exception e)
32          {
33              throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
34          }
35      }
36  
37      /**
38       * Create a Mule transaction that represents a transaction started outside of Mule
39       */
40      public Transaction joinExternalTransaction(MuleContext muleContext) throws TransactionException
41      {
42          try
43          {
44              TransactionManager txManager = muleContext.getTransactionManager();
45              if (txManager.getTransaction() == null)
46              {
47                  return null;
48              }
49              XaTransaction xat = new ExternalXaTransaction(muleContext);
50              xat.begin();
51              return xat;
52          }
53          catch (Exception e)
54          {
55              throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
56          }
57      }
58  
59      /**
60       * Determines whether this transaction factory creates transactions that are
61       * really transacted or if they are being used to simulate batch actions, such as
62       * using Jms Client Acknowledge.
63       */
64      public boolean isTransacted()
65      {
66          return true;
67      }
68  }