/
DurableTwoPhaseCommitParticipant.java
384 lines (353 loc) · 12 KB
/
DurableTwoPhaseCommitParticipant.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* JBoss, Home of Professional Open Source
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags.
* See the copyright.txt in the distribution for a full listing
* of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* (C) 2005-2006,
* @author JBoss Inc.
*/
/*
* Copyright (C) 2002, 2003, 2004,
*
* Arjuna Technologies Limited,
* Newcastle upon Tyne,
* Tyne and Wear,
* UK.
*
* $Id: DurableTwoPhaseCommitParticipant.java,v 1.1.2.2 2004/08/09 12:34:24 nmcl Exp $
*/
package com.arjuna.mwlabs.wst.at.participants;
import com.arjuna.wst.Durable2PCParticipant;
import com.arjuna.ats.arjuna.state.InputObjectState;
import com.arjuna.ats.arjuna.state.OutputObjectState;
import com.arjuna.mw.wscf.model.twophase.participants.*;
import com.arjuna.mw.wscf.model.twophase.exceptions.*;
import com.arjuna.mw.wscf.model.twophase.vote.*;
import com.arjuna.mw.wscf.exceptions.*;
import com.arjuna.mw.wsas.exceptions.SystemException;
import com.arjuna.mw.wsas.exceptions.WrongStateException;
import com.arjuna.mw.wsas.exceptions.SystemCommunicationException;
import com.arjuna.mw.wstx.logging.wstxLogger;
import com.arjuna.mwlabs.wst.util.PersistableParticipantHelper;
/**
* @author Mark Little (mark.little@arjuna.com)
* @version $Id: DurableTwoPhaseCommitParticipant.java,v 1.1.2.2 2004/08/09
* 12:34:24 nmcl Exp $
* @since 1.0.
*/
public class DurableTwoPhaseCommitParticipant implements Participant
{
// default ctor for crash recovery
public DurableTwoPhaseCommitParticipant() {
}
public DurableTwoPhaseCommitParticipant (Durable2PCParticipant resource, String identifier)
{
_resource = resource;
_id = identifier;
}
/**
* tell the participant to prepare
*
* @return the participant's vote or a cancel vote the aprticipant is null
*
*/
public Vote prepare () throws InvalidParticipantException,
WrongStateException, HeuristicHazardException,
HeuristicMixedException, SystemException
{
try
{
if (_resource != null)
{
com.arjuna.wst.Vote vt = _resource.prepare();
if (vt instanceof com.arjuna.wst.ReadOnly)
{
_readonly = true;
return new VoteReadOnly();
}
else
{
if (vt instanceof com.arjuna.wst.Prepared)
return new VoteConfirm();
else
{
_rolledback = true;
return new VoteCancel();
}
}
}
else
return new VoteCancel();
}
// catch (com.arjuna.mw.wst.exceptions.WrongStateException ex)
catch (com.arjuna.wst.WrongStateException ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_prepare(_id, _resource, ex);
WrongStateException wse = new WrongStateException(ex.toString());
wse.addSuppressed(ex);
throw wse;
}
/*
* catch (com.arjuna.mw.wst.exceptions.HeuristicHazardException ex {
* throw new HeuristicHazardException(ex.toString()); } catch
* (com.arjuna.mw.wst.exceptions.HeuristicMixedException ex) { throw new
* HeuristicMixedException(ex.toString()); }
*/
// catch (com.arjuna.mw.wst.exceptions.SystemException ex)
catch (com.arjuna.wst.SystemException ex)
{
if(ex instanceof com.arjuna.wst.stub.SystemCommunicationException) {
wstxLogger.i18NLogger.warn_mwlabs_wst_at_participants_DurableTwoPhaseCommitParticipant_prepare_1(_id, _resource);
SystemCommunicationException sce = new SystemCommunicationException(ex.toString());
sce.addSuppressed(ex);
throw sce;
}
else
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_prepare(_id, _resource, ex);
SystemException se = new SystemException(ex.toString());
se.addSuppressed(ex);
throw se;
}
}
}
/**
* attempt to commit the participant
*
*/
public void confirm () throws InvalidParticipantException,
WrongStateException, HeuristicHazardException,
HeuristicMixedException, HeuristicCancelException, SystemException
{
if (_resource != null)
{
try
{
if (!_readonly)
{
_resource.commit();
}
}
// catch (com.arjuna.mw.wst.exceptions.WrongStateException ex)
catch (com.arjuna.wst.WrongStateException ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_confirm(_id, _resource, ex);
WrongStateException wse = new WrongStateException(ex.toString());
wse.addSuppressed(wse);
throw wse;
}
/*
* catch (com.arjuna.mw.wst.exceptions.HeuristicHazardException ex) {
* throw new HeuristicHazardException(ex.toString()); } catch
* (com.arjuna.mw.wst.exceptions.HeuristicMixedException ex) { throw
* new HeuristicMixedException(ex.toString()); } catch
* (com.arjuna.mw.wst.exceptions.HeuristicRollbackException ex) {
* throw new HeuristicCancelException(ex.toString()); }
*/
// catch (com.arjuna.mw.wst.exceptions.SystemException ex)
catch (com.arjuna.wst.SystemException ex)
{
if(ex instanceof com.arjuna.wst.stub.SystemCommunicationException)
{
// log an error here -- we will end up writing a heuristic transaction record too
wstxLogger.i18NLogger.warn_mwlabs_wst_at_participants_DurableTwoPhaseCommitParticipant_confirm_1(_id, _resource);
SystemCommunicationException sce = new SystemCommunicationException(ex.toString());
sce.addSuppressed(ex);
throw sce;
} else {
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_confirm(_id, _resource, ex);
SystemException se = new SystemException(ex.toString());
se.addSuppressed(ex);
throw se;
}
}
}
else
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_confirm_is_null(_id);
throw new InvalidParticipantException("participant " + _id + " to confirm is null");
}
}
/**
* attempt to cancel the participant
*
*/
public void cancel () throws InvalidParticipantException,
WrongStateException, HeuristicHazardException,
HeuristicMixedException, HeuristicConfirmException, SystemException
{
if (_resource != null)
{
try
{
if (!_rolledback)
_resource.rollback();
}
// catch (com.arjuna.mw.wst.exceptions.WrongStateException ex)
catch (com.arjuna.wst.WrongStateException ex)
{
WrongStateException wse = new WrongStateException(ex.toString());
wse.addSuppressed(ex);
throw wse;
}
/*
* catch (com.arjuna.mw.wst.exceptions.HeuristicHazardException ex) {
* throw new HeuristicHazardException(ex.toString()); } catch
* (com.arjuna.mw.wst.exceptions.HeuristicMixedException ex) { throw
* new HeuristicMixedException(ex.toString()); } catch
* (com.arjuna.mw.wst.exceptions.HeuristicCommitException ex) {
* throw new HeuristicConfirmException(ex.toString()); }
*/
// catch (com.arjuna.mw.wst.exceptions.SystemException ex)
catch (com.arjuna.wst.SystemException ex)
{
if(ex instanceof com.arjuna.wst.stub.SystemCommunicationException)
{
// log an error here -- if the participant is dead it will retry anyway
wstxLogger.i18NLogger.error_mwlabs_wst_at_participants_DurableTwoPhaseCommitParticipant_cancel_1(_id);
SystemCommunicationException sce = new SystemCommunicationException(ex.toString());
sce.addSuppressed(ex);
throw sce;
} else {
SystemException se = new SystemException(ex.toString());
se.addSuppressed(ex);
throw se;
}
}
}
else
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_cancel_is_null(_id);
throw new InvalidParticipantException();
}
}
// TODO mark ParticipantCancelledException explicitly?
public void confirmOnePhase () throws InvalidParticipantException,
WrongStateException, HeuristicHazardException,
HeuristicMixedException, HeuristicCancelException, SystemException
{
if (_resource != null)
{
Vote v = null;
try
{
v = prepare();
}
catch (Exception ex)
{
// either the prepare timed out or the participant was invalid or in an
// invalid state
wstxLogger.i18NLogger.warn_wst_at_participants_Durable2PC_commit_one_phase(_id, _resource, ex);
v = new VoteCancel();
}
if (v instanceof VoteReadOnly)
{
_readonly = true;
}
else if (v instanceof VoteCancel)
{
_rolledback = false;
// TODO only do this if we didn't return VoteCancel
try {
cancel();
} catch (SystemCommunicationException sce) {
// if the rollback times out as well as the prepare we
// return an exception which indicates a failed transaction
}
wstxLogger.i18NLogger.warn_wst_at_participants_Durable2PC_canceled(_id, _resource);
throw new ParticipantCancelledException();
}
else
{
if (v instanceof VoteConfirm)
{
try
{
confirm();
}
catch (HeuristicHazardException ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_one_phase_failed(_id, _resource, ex);
throw ex;
}
catch (HeuristicMixedException ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_one_phase_failed(_id, _resource, ex);
throw ex;
}
catch (HeuristicCancelException ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_one_phase_failed(_id, _resource, ex);
throw ex;
}
catch (Exception ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_one_phase_failed(_id, _resource, ex);
HeuristicHazardException hhe = new HeuristicHazardException();
hhe.addSuppressed(ex);
throw ex;
}
}
else
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_one_phase_wrong_vote(_id, _resource, v.toString());
cancel(); // TODO error
throw new HeuristicHazardException();
}
}
}
else
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_confirm_one_phase_is_null(_id);
throw new InvalidParticipantException();
}
}
public void forget () throws InvalidParticipantException,
WrongStateException, SystemException
{
}
public void unknown () throws SystemException
{
/*
* If the transaction is unknown, then we assume it rolled back.
*/
try
{
cancel();
}
catch (Exception ex)
{
wstxLogger.i18NLogger.error_wst_at_participants_Durable2PC_unknown(_id, _resource, ex);
// TODO
}
}
public String id () throws SystemException
{
return _id;
}
public boolean save_state (OutputObjectState os)
{
return PersistableParticipantHelper.save_state(os, _resource) ;
}
public boolean restore_state (InputObjectState os)
{
_resource = (Durable2PCParticipant)PersistableParticipantHelper.restore_state(os) ;
return true ;
}
private Durable2PCParticipant _resource;
private String _id;
private boolean _readonly;
private boolean _rolledback;
}