1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.transaction.Transaction;
15 import org.mule.api.transaction.TransactionException;
16 import org.mule.config.i18n.CoreMessages;
17 import org.mule.context.notification.TransactionNotification;
18 import org.mule.util.UUID;
19
20 import java.text.MessageFormat;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25
26
27
28 public abstract class AbstractTransaction implements Transaction
29 {
30
31 protected final transient Log logger = LogFactory.getLog(getClass());
32
33 protected String id = UUID.getUUID();
34
35 protected MuleContext muleContext;
36
37 protected AbstractTransaction(MuleContext muleContext)
38 {
39 this.muleContext = muleContext;
40 }
41
42 public boolean isRollbackOnly() throws TransactionException
43 {
44 return getStatus() == STATUS_MARKED_ROLLBACK;
45 }
46
47 public boolean isBegun() throws TransactionException
48 {
49 int status = getStatus();
50 return status != STATUS_NO_TRANSACTION && status != STATUS_UNKNOWN;
51 }
52
53 public boolean isRolledBack() throws TransactionException
54 {
55 return getStatus() == STATUS_ROLLEDBACK;
56 }
57
58 public boolean isCommitted() throws TransactionException
59 {
60 return getStatus() == STATUS_COMMITTED;
61 }
62
63 public void begin() throws TransactionException
64 {
65 logger.debug("Beginning transaction");
66 doBegin();
67 TransactionCoordination.getInstance().bindTransaction(this);
68 fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_BEGAN));
69 }
70
71 public void commit() throws TransactionException
72 {
73 try
74 {
75 logger.debug("Committing transaction " + this);
76
77 if (isRollbackOnly())
78 {
79 throw new IllegalTransactionStateException(CoreMessages.transactionMarkedForRollback());
80 }
81
82 doCommit();
83 fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_COMMITTED));
84 }
85 finally
86 {
87 TransactionCoordination.getInstance().unbindTransaction(this);
88 }
89 }
90
91 public void rollback() throws TransactionException
92 {
93 try
94 {
95 logger.debug("Rolling back transaction");
96 setRollbackOnly();
97 doRollback();
98 fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_ROLLEDBACK));
99 }
100 finally
101 {
102 unbindTransaction();
103 }
104 }
105
106
107
108
109 protected void unbindTransaction() throws TransactionException
110 {
111 TransactionCoordination.getInstance().unbindTransaction(this);
112 }
113
114
115
116
117
118
119
120 protected abstract void doBegin() throws TransactionException;
121
122
123
124
125
126
127 protected abstract void doCommit() throws TransactionException;
128
129
130
131
132
133
134 protected abstract void doRollback() throws TransactionException;
135
136
137
138
139
140
141 protected void fireNotification(TransactionNotification notification)
142 {
143
144 muleContext.fireNotification(notification);
145 }
146
147 public boolean isXA()
148 {
149 return false;
150 }
151
152 public void resume() throws TransactionException
153 {
154 throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
155 }
156
157 public javax.transaction.Transaction suspend() throws TransactionException
158 {
159 throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
160 }
161
162 public String getId()
163 {
164 return id;
165 }
166
167 @Override
168 public String toString()
169 {
170 int status;
171 try
172 {
173 status = getStatus();
174 }
175 catch (TransactionException e)
176 {
177 status = -1;
178 }
179 return MessageFormat.format("{0}[id={1} , status={2}]", getClass().getName(), id, status);
180 }
181 }