1
2
3
4
5
6
7
8
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
26
27 public abstract class AbstractTransaction implements UMOTransaction
28 {
29
30 protected final transient Log logger = LogFactory.getLog(getClass());
31
32
33
34
35
36
37 public boolean isRollbackOnly() throws TransactionException
38 {
39 return getStatus() == STATUS_MARKED_ROLLBACK;
40 }
41
42
43
44
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
54
55
56
57
58 public boolean isRolledBack() throws TransactionException
59 {
60 return getStatus() == STATUS_ROLLEDBACK;
61 }
62
63
64
65
66
67
68 public boolean isCommitted() throws TransactionException
69 {
70 return getStatus() == STATUS_COMMITTED;
71 }
72
73
74
75
76
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
88
89
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
113
114
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
133
134
135
136 protected abstract void doBegin() throws TransactionException;
137
138
139
140
141
142
143 protected abstract void doCommit() throws TransactionException;
144
145
146
147
148
149
150 protected abstract void doRollback() throws TransactionException;
151
152
153
154
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 }