View Javadoc

1   /*
2    * $Id: AbstractTransaction.java 10384 2008-01-18 11:35:23Z akuzmin $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleManager;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.impl.internal.notifications.TransactionNotification;
16  import org.mule.umo.TransactionException;
17  import org.mule.umo.UMOTransaction;
18  
19  import javax.transaction.Transaction;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * This base class provides low level features for transactions.
26   */
27  public abstract class AbstractTransaction implements UMOTransaction
28  {
29  
30      protected final transient Log logger = LogFactory.getLog(getClass());
31  
32      /*
33       * (non-Javadoc)
34       * 
35       * @see org.mule.umo.UMOTransaction#isRollbackOnly()
36       */
37      public boolean isRollbackOnly() throws TransactionException
38      {
39          return getStatus() == STATUS_MARKED_ROLLBACK;
40      }
41  
42      /*
43       * (non-Javadoc)
44       * 
45       * @see org.mule.umo.UMOTransaction#isBegun()
46       */
47      public boolean isBegun() throws TransactionException
48      {
49          int status = getStatus();
50          return status != STATUS_NO_TRANSACTION && status != STATUS_UNKNOWN;
51      }
52  
53      /*
54       * (non-Javadoc)
55       * 
56       * @see org.mule.umo.UMOTransaction#isRolledBack()
57       */
58      public boolean isRolledBack() throws TransactionException
59      {
60          return getStatus() == STATUS_ROLLEDBACK;
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see org.mule.umo.UMOTransaction#isCommitted()
67       */
68      public boolean isCommitted() throws TransactionException
69      {
70          return getStatus() == STATUS_COMMITTED;
71      }
72  
73      /*
74       * (non-Javadoc)
75       * 
76       * @see org.mule.umo.UMOTransaction#begin()
77       */
78      public void begin() throws TransactionException
79      {
80          logger.debug("Beginning transaction");
81          doBegin();
82          TransactionCoordination.getInstance().bindTransaction(this);
83          fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_BEGAN));
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see org.mule.umo.UMOTransaction#commit()
90       */
91      public void commit() throws TransactionException
92      {
93          try
94          {
95              logger.debug("Committing transaction " + this);
96  
97              if (isRollbackOnly())
98              {
99                  throw new IllegalTransactionStateException(CoreMessages.transactionMarkedForRollback());
100             }
101 
102             doCommit();
103             fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_COMMITTED));
104         }
105         finally
106         {
107             TransactionCoordination.getInstance().unbindTransaction(this);
108         }
109     }
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see org.mule.umo.UMOTransaction#rollback()
115      */
116     public void rollback() throws TransactionException
117     {
118         try
119         {
120             logger.debug("Rolling back transaction");
121             setRollbackOnly();
122             doRollback();
123             fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_ROLLEDBACK));
124         }
125         finally
126         {
127             TransactionCoordination.getInstance().unbindTransaction(this);
128         }
129     }
130 
131     /**
132      * Really begin the transaction. Note that resources are enlisted yet.
133      * 
134      * @throws TransactionException
135      */
136     protected abstract void doBegin() throws TransactionException;
137 
138     /**
139      * Commit the transaction on the underlying resource
140      * 
141      * @throws TransactionException
142      */
143     protected abstract void doCommit() throws TransactionException;
144 
145     /**
146      * Rollback the transaction on the underlying resource
147      * 
148      * @throws TransactionException
149      */
150     protected abstract void doRollback() throws TransactionException;
151 
152     /**
153      * Fires a server notification to all registered
154      * {@link org.mule.impl.internal.notifications.TransactionNotificationListener}s.
155      *
156      */
157     protected void fireNotification(TransactionNotification notification)
158     {
159         MuleManager.getInstance().fireNotification(notification);
160     }
161 
162     public boolean isXA()
163     {
164         return false;
165     }
166 
167     public void resume() throws TransactionException
168     {
169         throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
170     }
171 
172     public Transaction suspend() throws TransactionException
173     {
174         throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
175     }
176 }