View Javadoc

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