View Javadoc

1   /*
2    * $Id: ExternalXaTransaction.java 19191 2010-08-25 21:05:23Z tcarlson $
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.transaction;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.transaction.TransactionException;
15  import org.mule.config.i18n.CoreMessages;
16  
17  import java.text.MessageFormat;
18  
19  import javax.transaction.Status;
20  import javax.transaction.Synchronization;
21  
22  /**
23   * <code>ExternalXaTransaction</code> represents an external XA transaction in Mule.
24   */
25  public class ExternalXaTransaction extends XaTransaction
26  {
27      public ExternalXaTransaction(MuleContext muleContext)
28      {
29          super(muleContext);
30      }
31  
32      protected void doBegin() throws TransactionException
33      {
34          if (txManager == null)
35          {
36              throw new IllegalStateException(
37                      CoreMessages.objectNotRegistered("javax.transaction.TransactionManager", "Transaction Manager").getMessage());
38          }
39  
40          try
41          {
42              synchronized (this)
43              {
44                  transaction = txManager.getTransaction();
45                  transaction.registerSynchronization(new ExternalTransaction(muleContext));
46              }
47          }
48          catch (Exception e)
49          {
50              throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
51          }
52      }
53  
54      /**
55       * This class is notified when an external transaction is complete and cleans up
56       * Mule-specific resources
57       */
58      class ExternalTransaction extends AbstractTransaction implements Synchronization
59      {
60          ExternalTransaction(MuleContext muleContext)
61          {
62              super(muleContext);
63          }
64          
65          /** Nothing to do */
66          public void beforeCompletion()
67          {
68          }
69  
70          /** Clean up mule resources */
71          public void afterCompletion(int status)
72          {
73              boolean commit = status == Status.STATUS_COMMITTED;
74  
75              try
76              {
77                  if (commit)
78                  {
79                      commit();
80                  }
81                  else
82                  {
83                      rollback();
84                  }
85              }
86              catch (TransactionException ex)
87              {
88                  logger.warn(MessageFormat.format(
89                      "Exception while {0} an external transaction {1}", commit ? "committing" : "rolling back", this), ex);
90              }
91          }
92  
93          @Override
94          protected void unbindTransaction()
95          {
96              // no-op -- already unbound in TransactionTemplate
97          }
98  
99          @Override
100         protected void doCommit()
101         {
102             delistResources();
103             closeResources();
104             transaction = null;
105         }
106 
107         @Override
108         protected void doRollback()
109         {
110             closeResources();
111             transaction = null;
112         }
113 
114         @Override
115         protected void doBegin()
116         {
117         }
118 
119         @Override
120         public boolean isRollbackOnly() throws TransactionException
121         {
122             return ExternalXaTransaction.this.isRollbackOnly();
123         }
124 
125         public int getStatus() throws TransactionException
126         {
127             return ExternalXaTransaction.this.getStatus();
128         }
129 
130         public Object getResource(Object key)
131         {
132             return ExternalXaTransaction.this.getResource(key);
133         }
134 
135         public boolean hasResource(Object key)
136         {
137             return ExternalXaTransaction.this.hasResource(key);
138         }
139 
140         public void bindResource(Object key, Object resource) throws TransactionException
141         {
142         }
143 
144         public void setRollbackOnly() throws TransactionException
145         {
146         }
147     }
148 }