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