KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transaction > AbstractTransaction


1 /*
2  * $Id: AbstractTransaction.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.umo.TransactionException;
18 import org.mule.umo.UMOTransaction;
19
20 /**
21  * This base class provides low level features for transactions.
22  */

23 public abstract class AbstractTransaction implements UMOTransaction
24 {
25
26     protected final transient Log logger = LogFactory.getLog(getClass());
27
28     /*
29      * (non-Javadoc)
30      *
31      * @see org.mule.umo.UMOTransaction#isRollbackOnly()
32      */

33     public boolean isRollbackOnly() throws TransactionException
34     {
35         return getStatus() == STATUS_MARKED_ROLLBACK;
36     }
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.mule.umo.UMOTransaction#isBegun()
42      */

43     public boolean isBegun() throws TransactionException
44     {
45         int status = getStatus();
46         return status != STATUS_NO_TRANSACTION && status != STATUS_UNKNOWN;
47     }
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.mule.umo.UMOTransaction#isRolledBack()
53      */

54     public boolean isRolledBack() throws TransactionException
55     {
56         return getStatus() == STATUS_ROLLEDBACK;
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.mule.umo.UMOTransaction#isCommitted()
63      */

64     public boolean isCommitted() throws TransactionException
65     {
66         return getStatus() == STATUS_COMMITTED;
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.mule.umo.UMOTransaction#begin()
73      */

74     public void begin() throws TransactionException
75     {
76         logger.debug("Beginning transaction");
77         doBegin();
78         TransactionCoordination.getInstance().bindTransaction(this);
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.mule.umo.UMOTransaction#commit()
85      */

86     public void commit() throws TransactionException
87     {
88         try
89         {
90             logger.debug("Committing transaction");
91
92             if (isRollbackOnly())
93             {
94                 throw new IllegalTransactionStateException(new Message(Messages.TX_MARKED_FOR_ROLLBACK));
95             }
96
97             doCommit();
98         }
99         finally
100         {
101             TransactionCoordination.getInstance().unbindTransaction(this);
102         }
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.mule.umo.UMOTransaction#rollback()
109      */

110     public void rollback() throws TransactionException
111     {
112         try
113         {
114             logger.debug("Rolling back transaction");
115             setRollbackOnly();
116             doRollback();
117         }
118         finally
119         {
120             TransactionCoordination.getInstance().unbindTransaction(this);
121         }
122     }
123
124     /**
125      * Really begin the transaction. Note that resources are enlisted yet.
126      *
127      * @throws TransactionException
128      */

129     protected abstract void doBegin() throws TransactionException;
130
131     /**
132      * Commit the transaction on the underlying resource
133      *
134      * @throws TransactionException
135      */

136     protected abstract void doCommit() throws TransactionException;
137
138     /**
139      * Rollback the transaction on the underlying resource
140      *
141      * @throws TransactionException
142      */

143     protected abstract void doRollback() throws TransactionException;
144
145 }
146
Popular Tags