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 | 0 | protected final transient Log logger = LogFactory.getLog(getClass()); |
32 | |
|
33 | 0 | protected String id = UUID.getUUID(); |
34 | |
|
35 | |
protected MuleContext muleContext; |
36 | |
|
37 | |
protected AbstractTransaction(MuleContext muleContext) |
38 | 0 | { |
39 | 0 | this.muleContext = muleContext; |
40 | 0 | } |
41 | |
|
42 | |
public boolean isRollbackOnly() throws TransactionException |
43 | |
{ |
44 | 0 | return getStatus() == STATUS_MARKED_ROLLBACK; |
45 | |
} |
46 | |
|
47 | |
public boolean isBegun() throws TransactionException |
48 | |
{ |
49 | 0 | int status = getStatus(); |
50 | 0 | return status != STATUS_NO_TRANSACTION && status != STATUS_UNKNOWN; |
51 | |
} |
52 | |
|
53 | |
public boolean isRolledBack() throws TransactionException |
54 | |
{ |
55 | 0 | return getStatus() == STATUS_ROLLEDBACK; |
56 | |
} |
57 | |
|
58 | |
public boolean isCommitted() throws TransactionException |
59 | |
{ |
60 | 0 | return getStatus() == STATUS_COMMITTED; |
61 | |
} |
62 | |
|
63 | |
public void begin() throws TransactionException |
64 | |
{ |
65 | 0 | logger.debug("Beginning transaction"); |
66 | 0 | doBegin(); |
67 | 0 | TransactionCoordination.getInstance().bindTransaction(this); |
68 | 0 | fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_BEGAN)); |
69 | 0 | } |
70 | |
|
71 | |
public void commit() throws TransactionException |
72 | |
{ |
73 | |
try |
74 | |
{ |
75 | 0 | logger.debug("Committing transaction " + this); |
76 | |
|
77 | 0 | if (isRollbackOnly()) |
78 | |
{ |
79 | 0 | throw new IllegalTransactionStateException(CoreMessages.transactionMarkedForRollback()); |
80 | |
} |
81 | |
|
82 | 0 | doCommit(); |
83 | 0 | fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_COMMITTED)); |
84 | |
} |
85 | |
finally |
86 | |
{ |
87 | 0 | TransactionCoordination.getInstance().unbindTransaction(this); |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | |
public void rollback() throws TransactionException |
92 | |
{ |
93 | |
try |
94 | |
{ |
95 | 0 | logger.debug("Rolling back transaction"); |
96 | 0 | setRollbackOnly(); |
97 | 0 | doRollback(); |
98 | 0 | fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_ROLLEDBACK)); |
99 | |
} |
100 | |
finally |
101 | |
{ |
102 | 0 | unbindTransaction(); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
protected void unbindTransaction() throws TransactionException |
110 | |
{ |
111 | 0 | TransactionCoordination.getInstance().unbindTransaction(this); |
112 | 0 | } |
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 | 0 | muleContext.fireNotification(notification); |
145 | 0 | } |
146 | |
|
147 | |
public boolean isXA() |
148 | |
{ |
149 | 0 | return false; |
150 | |
} |
151 | |
|
152 | |
public void resume() throws TransactionException |
153 | |
{ |
154 | 0 | throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this)); |
155 | |
} |
156 | |
|
157 | |
public javax.transaction.Transaction suspend() throws TransactionException |
158 | |
{ |
159 | 0 | throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this)); |
160 | |
} |
161 | |
|
162 | |
public String getId() |
163 | |
{ |
164 | 0 | return id; |
165 | |
} |
166 | |
|
167 | |
@Override |
168 | |
public String toString() |
169 | |
{ |
170 | |
int status; |
171 | |
try |
172 | |
{ |
173 | 0 | status = getStatus(); |
174 | |
} |
175 | 0 | catch (TransactionException e) |
176 | |
{ |
177 | 0 | status = -1; |
178 | 0 | } |
179 | 0 | return MessageFormat.format("{0}[id={1} , status={2}]", getClass().getName(), id, status); |
180 | |
} |
181 | |
} |