1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.MuleServer;
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
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22
23
24
25 public abstract class AbstractTransaction implements Transaction
26 {
27
28 protected final transient Log logger = LogFactory.getLog(getClass());
29
30
31
32
33
34
35 public boolean isRollbackOnly() throws TransactionException
36 {
37 return getStatus() == STATUS_MARKED_ROLLBACK;
38 }
39
40
41
42
43
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
53
54
55
56 public boolean isRolledBack() throws TransactionException
57 {
58 return getStatus() == STATUS_ROLLEDBACK;
59 }
60
61
62
63
64
65
66 public boolean isCommitted() throws TransactionException
67 {
68 return getStatus() == STATUS_COMMITTED;
69 }
70
71
72
73
74
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
86
87
88
89 public void commit() throws TransactionException
90 {
91 try
92 {
93 logger.debug("Committing transaction " + this);
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
111
112
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
131
132
133
134 protected abstract void doBegin() throws TransactionException;
135
136
137
138
139
140
141 protected abstract void doCommit() throws TransactionException;
142
143
144
145
146
147
148 protected abstract void doRollback() throws TransactionException;
149
150
151
152
153
154
155 protected void fireNotification(TransactionNotification notification)
156 {
157
158 MuleServer.getMuleContext().fireNotification(notification);
159 }
160
161 public boolean isXA()
162 {
163 return false;
164 }
165
166 public void resume() throws TransactionException
167 {
168 throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
169 }
170
171 public javax.transaction.Transaction suspend() throws TransactionException
172 {
173 throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
174 }
175 }