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.ExternalTransactionAwareTransactionFactory; |
15 | |
import org.mule.api.transaction.Transaction; |
16 | |
import org.mule.api.transaction.TransactionCallback; |
17 | |
import org.mule.api.transaction.TransactionConfig; |
18 | |
import org.mule.api.transaction.TransactionException; |
19 | |
import org.mule.api.transaction.TransactionFactory; |
20 | |
import org.mule.config.i18n.CoreMessages; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
|
25 | |
public class TransactionTemplate<T> |
26 | |
{ |
27 | 0 | private static final Log logger = LogFactory.getLog(TransactionTemplate.class); |
28 | |
|
29 | |
private final TransactionConfig config; |
30 | |
private final MuleContext context; |
31 | |
|
32 | |
public TransactionTemplate(TransactionConfig config, MuleContext context) |
33 | 0 | { |
34 | 0 | this.config = config; |
35 | 0 | this.context = context; |
36 | 0 | } |
37 | |
|
38 | |
public T execute(TransactionCallback<T> callback) throws Exception |
39 | |
{ |
40 | |
|
41 | 0 | if (config == null) |
42 | |
{ |
43 | 0 | return callback.doInTransaction(); |
44 | |
} |
45 | |
|
46 | 0 | Transaction joinedExternal = null; |
47 | 0 | byte action = (config != null) ? config.getAction() : TransactionConfig.ACTION_DEFAULT; |
48 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
49 | 0 | if (tx == null && context != null && config != null && config.isInteractWithExternal()) |
50 | |
{ |
51 | |
|
52 | 0 | TransactionFactory tmFactory = config.getFactory(); |
53 | 0 | if (tmFactory instanceof ExternalTransactionAwareTransactionFactory) |
54 | |
{ |
55 | 0 | ExternalTransactionAwareTransactionFactory extmFactory = |
56 | |
(ExternalTransactionAwareTransactionFactory) tmFactory; |
57 | 0 | joinedExternal = tx = extmFactory.joinExternalTransaction(context); |
58 | |
} |
59 | |
} |
60 | |
|
61 | 0 | Transaction suspendedXATx = null; |
62 | |
|
63 | 0 | if (action == TransactionConfig.ACTION_NEVER && tx != null) |
64 | |
{ |
65 | 0 | throw new IllegalTransactionStateException( |
66 | |
CoreMessages.transactionAvailableButActionIs("Never")); |
67 | |
} |
68 | 0 | else if ((action == TransactionConfig.ACTION_NONE || action == TransactionConfig.ACTION_ALWAYS_BEGIN) |
69 | |
&& tx != null) |
70 | |
{ |
71 | 0 | if (logger.isDebugEnabled()) |
72 | |
{ |
73 | 0 | logger.debug(action + ", " + "current TX: " + tx); |
74 | |
} |
75 | |
|
76 | 0 | if (tx.isXA()) |
77 | |
{ |
78 | |
|
79 | 0 | suspendedXATx = tx; |
80 | 0 | suspendXATransaction(suspendedXATx); |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | |
|
85 | 0 | resolveTransaction(tx); |
86 | |
} |
87 | |
|
88 | 0 | tx = null; |
89 | |
} |
90 | 0 | else if (action == TransactionConfig.ACTION_ALWAYS_JOIN && tx == null) |
91 | |
{ |
92 | 0 | throw new IllegalTransactionStateException( |
93 | |
CoreMessages.transactionNotAvailableButActionIs("Always Join")); |
94 | |
} |
95 | |
|
96 | 0 | if (action == TransactionConfig.ACTION_ALWAYS_BEGIN |
97 | |
|| (action == TransactionConfig.ACTION_BEGIN_OR_JOIN && tx == null)) |
98 | |
{ |
99 | 0 | logger.debug("Beginning transaction"); |
100 | 0 | tx = config.getFactory().beginTransaction(context); |
101 | 0 | logger.debug("Transaction successfully started: " + tx); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 0 | tx = null; |
106 | |
} |
107 | |
|
108 | |
try |
109 | |
{ |
110 | 0 | T result = callback.doInTransaction(); |
111 | 0 | if (tx != null) |
112 | |
{ |
113 | |
|
114 | 0 | tx = TransactionCoordination.getInstance().getTransaction(); |
115 | |
} |
116 | 0 | if (tx != null) |
117 | |
{ |
118 | 0 | resolveTransaction(tx); |
119 | |
} |
120 | 0 | if (suspendedXATx != null) |
121 | |
{ |
122 | 0 | resumeXATransaction(suspendedXATx); |
123 | 0 | tx = suspendedXATx; |
124 | |
} |
125 | 0 | return result; |
126 | |
} |
127 | 0 | catch (Exception e) |
128 | |
{ |
129 | 0 | tx = TransactionCoordination.getInstance().getTransaction(); |
130 | 0 | if (tx != null) |
131 | |
{ |
132 | 0 | tx.setRollbackOnly(); |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 0 | if (tx.isRollbackOnly()) |
139 | |
{ |
140 | 0 | logger.debug("Exception caught: rollback transaction", e); |
141 | |
} |
142 | 0 | resolveTransaction(tx); |
143 | |
} |
144 | 0 | if (suspendedXATx != null) |
145 | |
{ |
146 | 0 | resumeXATransaction(suspendedXATx); |
147 | |
|
148 | |
|
149 | 0 | return null; |
150 | |
} |
151 | 0 | throw e; |
152 | |
} |
153 | 0 | catch (Error e) |
154 | |
{ |
155 | 0 | if (tx != null) |
156 | |
{ |
157 | 0 | logger.info("Error caught, rolling back TX " + tx, e); |
158 | 0 | tx.rollback(); |
159 | |
} |
160 | 0 | throw e; |
161 | |
} |
162 | |
finally |
163 | |
{ |
164 | 0 | if (joinedExternal != null) |
165 | 0 | TransactionCoordination.getInstance().unbindTransaction(joinedExternal); |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
protected void resolveTransaction(Transaction tx) throws TransactionException |
170 | |
{ |
171 | 0 | if (tx.isRollbackOnly()) |
172 | |
{ |
173 | 0 | if (logger.isDebugEnabled()) |
174 | |
{ |
175 | 0 | logger.debug("Transaction has been marked rollbackOnly, rolling it back: " + tx); |
176 | |
} |
177 | 0 | tx.rollback(); |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | if (logger.isDebugEnabled()) |
182 | |
{ |
183 | 0 | logger.debug("Committing transaction " + tx); |
184 | |
} |
185 | 0 | tx.commit(); |
186 | |
} |
187 | 0 | } |
188 | |
|
189 | |
protected void suspendXATransaction(Transaction tx) throws TransactionException |
190 | |
{ |
191 | 0 | if (logger.isDebugEnabled()) |
192 | |
{ |
193 | 0 | logger.debug("Suspending " + tx); |
194 | |
} |
195 | |
|
196 | 0 | tx.suspend(); |
197 | |
|
198 | 0 | if (logger.isDebugEnabled()) |
199 | |
{ |
200 | 0 | logger.debug("Successfully suspended " + tx); |
201 | 0 | logger.debug("Unbinding the following TX from the current context: " + tx); |
202 | |
} |
203 | |
|
204 | 0 | TransactionCoordination.getInstance().unbindTransaction(tx); |
205 | 0 | } |
206 | |
|
207 | |
protected void resumeXATransaction(Transaction tx) throws TransactionException |
208 | |
{ |
209 | 0 | if (logger.isDebugEnabled()) |
210 | |
{ |
211 | 0 | logger.debug("Re-binding and Resuming " + tx); |
212 | |
} |
213 | |
|
214 | 0 | TransactionCoordination.getInstance().bindTransaction(tx); |
215 | 0 | tx.resume(); |
216 | 0 | } |
217 | |
|
218 | |
} |
219 | |
|