1 |
| package org.jboss.cache.transaction; |
2 |
| |
3 |
| import org.apache.commons.logging.Log; |
4 |
| import org.apache.commons.logging.LogFactory; |
5 |
| |
6 |
| import javax.transaction.HeuristicMixedException; |
7 |
| import javax.transaction.HeuristicRollbackException; |
8 |
| import javax.transaction.RollbackException; |
9 |
| import javax.transaction.Status; |
10 |
| import javax.transaction.Synchronization; |
11 |
| import javax.transaction.SystemException; |
12 |
| import javax.transaction.Transaction; |
13 |
| import javax.transaction.xa.XAResource; |
14 |
| import java.util.Set; |
15 |
| import java.util.concurrent.CopyOnWriteArraySet; |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class DummyTransaction implements Transaction |
24 |
| { |
25 |
| private int status = Status.STATUS_UNKNOWN; |
26 |
| private static final Log log = LogFactory.getLog(DummyTransaction.class); |
27 |
| DummyBaseTransactionManager tm_; |
28 |
| |
29 |
| private final Set<Synchronization> participants = new CopyOnWriteArraySet<Synchronization>(); |
30 |
| |
31 |
1121984
| public DummyTransaction(DummyBaseTransactionManager tm)
|
32 |
| { |
33 |
1121984
| tm_ = tm;
|
34 |
1121984
| status = Status.STATUS_ACTIVE;
|
35 |
| } |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
1121767
| public void commit()
|
54 |
| throws RollbackException, HeuristicMixedException, HeuristicRollbackException, |
55 |
| SecurityException, SystemException |
56 |
| { |
57 |
1121767
| boolean doCommit;
|
58 |
1121767
| status = Status.STATUS_PREPARING;
|
59 |
1121767
| try
|
60 |
| { |
61 |
1121767
| boolean outcome = notifyBeforeCompletion();
|
62 |
| |
63 |
1121761
| if (outcome && status != Status.STATUS_MARKED_ROLLBACK)
|
64 |
| { |
65 |
1121698
| status = Status.STATUS_COMMITTING;
|
66 |
1121698
| doCommit = true;
|
67 |
| } |
68 |
| else |
69 |
| { |
70 |
63
| status = Status.STATUS_ROLLING_BACK;
|
71 |
63
| doCommit = false;
|
72 |
| } |
73 |
1121761
| notifyAfterCompletion(doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK);
|
74 |
1121750
| status = doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK;
|
75 |
63
| if (!doCommit) throw new RollbackException("outcome is " + outcome + " status: " + status);
|
76 |
| } |
77 |
| finally |
78 |
| { |
79 |
| |
80 |
1121750
| tm_.setTransaction(null);
|
81 |
| } |
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
270
| public void rollback() throws IllegalStateException, SystemException
|
95 |
| { |
96 |
270
| try
|
97 |
| { |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
270
| status = Status.STATUS_ROLLEDBACK;
|
103 |
270
| notifyAfterCompletion(Status.STATUS_ROLLEDBACK);
|
104 |
| } |
105 |
| catch (Throwable t) |
106 |
| { |
107 |
| } |
108 |
270
| status = Status.STATUS_ROLLEDBACK;
|
109 |
| |
110 |
| |
111 |
270
| tm_.setTransaction(null);
|
112 |
| } |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
102
| public void setRollbackOnly() throws IllegalStateException, SystemException
|
123 |
| { |
124 |
102
| status = Status.STATUS_MARKED_ROLLBACK;
|
125 |
| } |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
5910338
| public int getStatus() throws SystemException
|
136 |
| { |
137 |
5910080
| return status;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
0
| public void setTransactionTimeout(int seconds) throws SystemException
|
151 |
| { |
152 |
0
| throw new SystemException("not supported");
|
153 |
| } |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
0
| public boolean enlistResource(XAResource xaRes)
|
170 |
| throws RollbackException, IllegalStateException, SystemException |
171 |
| { |
172 |
0
| throw new SystemException("not supported");
|
173 |
| } |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
0
| public boolean delistResource(XAResource xaRes, int flag)
|
187 |
| throws IllegalStateException, SystemException |
188 |
| { |
189 |
0
| throw new SystemException("not supported");
|
190 |
| } |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
1125029
| public void registerSynchronization(Synchronization sync)
|
206 |
| throws RollbackException, IllegalStateException, SystemException |
207 |
| { |
208 |
1125029
| if (sync == null)
|
209 |
0
| throw new IllegalArgumentException("null synchronization " + this);
|
210 |
| |
211 |
1125029
| switch (status)
|
212 |
| { |
213 |
1125029
| case Status.STATUS_ACTIVE:
|
214 |
0
| case Status.STATUS_PREPARING:
|
215 |
1125029
| break;
|
216 |
0
| case Status.STATUS_PREPARED:
|
217 |
0
| throw new IllegalStateException("already prepared. " + this);
|
218 |
0
| case Status.STATUS_COMMITTING:
|
219 |
0
| throw new IllegalStateException("already started committing. " + this);
|
220 |
0
| case Status.STATUS_COMMITTED:
|
221 |
0
| throw new IllegalStateException("already committed. " + this);
|
222 |
0
| case Status.STATUS_MARKED_ROLLBACK:
|
223 |
0
| throw new RollbackException("already marked for rollback " + this);
|
224 |
0
| case Status.STATUS_ROLLING_BACK:
|
225 |
0
| throw new RollbackException("already started rolling back. " + this);
|
226 |
0
| case Status.STATUS_ROLLEDBACK:
|
227 |
0
| throw new RollbackException("already rolled back. " + this);
|
228 |
0
| case Status.STATUS_NO_TRANSACTION:
|
229 |
0
| throw new IllegalStateException("no transaction. " + this);
|
230 |
0
| case Status.STATUS_UNKNOWN:
|
231 |
0
| throw new IllegalStateException("unknown state " + this);
|
232 |
0
| default:
|
233 |
0
| throw new IllegalStateException("illegal status: " + status + " tx=" + this);
|
234 |
| } |
235 |
| |
236 |
1125029
| if (log.isDebugEnabled())
|
237 |
| { |
238 |
0
| log.debug("registering synchronization handler " + sync);
|
239 |
| } |
240 |
1125029
| participants.add(sync);
|
241 |
| |
242 |
| } |
243 |
| |
244 |
0
| void setStatus(int new_status)
|
245 |
| { |
246 |
0
| status = new_status;
|
247 |
| } |
248 |
| |
249 |
1121767
| boolean notifyBeforeCompletion()
|
250 |
| { |
251 |
1121767
| boolean retval = true;
|
252 |
| |
253 |
1121767
| for (Synchronization s : participants)
|
254 |
| { |
255 |
1124690
| if (log.isDebugEnabled())
|
256 |
| { |
257 |
0
| log.debug("processing beforeCompletion for " + s);
|
258 |
| } |
259 |
1124690
| try
|
260 |
| { |
261 |
1124690
| s.beforeCompletion();
|
262 |
| } |
263 |
| catch (Throwable t) |
264 |
| { |
265 |
63
| retval = false;
|
266 |
63
| log.error("beforeCompletion() failed for " + s, t);
|
267 |
| } |
268 |
| } |
269 |
1121761
| return retval;
|
270 |
| } |
271 |
| |
272 |
1122031
| void notifyAfterCompletion(int status)
|
273 |
| { |
274 |
1122031
| for (Synchronization s : participants)
|
275 |
| { |
276 |
1124965
| if (log.isDebugEnabled())
|
277 |
| { |
278 |
0
| log.debug("processing afterCompletion for " + s);
|
279 |
| } |
280 |
1124965
| try
|
281 |
| { |
282 |
1124965
| s.afterCompletion(status);
|
283 |
| } |
284 |
| catch (Throwable t) |
285 |
| { |
286 |
0
| log.error("afterCompletion() failed for " + s, t);
|
287 |
| } |
288 |
| } |
289 |
1122020
| participants.clear();
|
290 |
| } |
291 |
| |
292 |
| } |