1 package org
.argeo
.osgi
.transaction
;
3 import java
.util
.ArrayList
;
6 import javax
.transaction
.xa
.XAException
;
7 import javax
.transaction
.xa
.XAResource
;
8 import javax
.transaction
.xa
.Xid
;
10 /** Simple implementation of an XA transaction. */
11 class SimpleTransaction
<T
>
12 //implements Transaction, Status
14 private final Xid xid
;
16 private final List
<XAResource
> xaResources
= new ArrayList
<XAResource
>();
18 private final SimpleTransactionManager transactionManager
;
19 private TransactionStatusAdapter
<T
> tsa
;
21 public SimpleTransaction(SimpleTransactionManager transactionManager
, TransactionStatusAdapter
<T
> tsa
) {
23 this.status
= tsa
.getActiveStatus();
24 this.xid
= new UuidXid();
25 this.transactionManager
= transactionManager
;
28 public synchronized void commit()
29 // throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
30 // SecurityException, IllegalStateException, SystemException
32 status
= tsa
.getPreparingStatus();
33 for (XAResource xaRes
: xaResources
) {
34 if (status
.equals(tsa
.getMarkedRollbackStatus()))
38 } catch (XAException e
) {
39 status
= tsa
.getMarkedRollbackStatus();
40 error("Cannot prepare " + xaRes
+ " for " + xid
, e
);
43 if (status
.equals(tsa
.getMarkedRollbackStatus())) {
45 throw new SimpleRollbackException();
47 status
= tsa
.getPreparedStatus();
49 status
= tsa
.getCommittingStatus();
50 for (XAResource xaRes
: xaResources
) {
51 if (status
.equals(tsa
.getMarkedRollbackStatus()))
54 xaRes
.commit(xid
, false);
55 } catch (XAException e
) {
56 status
= tsa
.getMarkedRollbackStatus();
57 error("Cannot prepare " + xaRes
+ " for " + xid
, e
);
60 if (status
.equals(tsa
.getMarkedRollbackStatus())) {
62 throw new SimpleRollbackException();
66 status
= tsa
.getCommittedStatus();
67 clearResources(XAResource
.TMSUCCESS
);
68 transactionManager
.unregister(xid
);
71 public synchronized void rollback()
72 // throws IllegalStateException, SystemException
74 status
= tsa
.getRollingBackStatus();
75 for (XAResource xaRes
: xaResources
) {
78 } catch (XAException e
) {
79 error("Cannot rollback " + xaRes
+ " for " + xid
, e
);
84 status
= tsa
.getRolledBackStatus();
85 clearResources(XAResource
.TMFAIL
);
86 transactionManager
.unregister(xid
);
89 public synchronized boolean enlistResource(XAResource xaRes
)
90 // throws RollbackException, IllegalStateException, SystemException
92 if (xaResources
.add(xaRes
)) {
94 xaRes
.start(getXid(), XAResource
.TMNOFLAGS
);
96 } catch (XAException e
) {
97 error("Cannot enlist " + xaRes
, e
);
104 public synchronized boolean delistResource(XAResource xaRes
, int flag
)
105 // throws IllegalStateException, SystemException
107 if (xaResources
.remove(xaRes
)) {
109 xaRes
.end(getXid(), flag
);
110 } catch (XAException e
) {
111 error("Cannot delist " + xaRes
, e
);
119 protected void clearResources(int flag
) {
120 for (XAResource xaRes
: xaResources
)
122 xaRes
.end(getXid(), flag
);
123 } catch (XAException e
) {
124 error("Cannot end " + xaRes
, e
);
129 protected void error(Object obj
, Exception e
) {
130 System
.err
.println(obj
);
134 public synchronized T
getStatus()
135 // throws SystemException
140 // public void registerSynchronization(Synchronization sync)
141 // throws RollbackException, IllegalStateException, SystemException {
142 // throw new UnsupportedOperationException();
145 public void setRollbackOnly()
146 // throws IllegalStateException, SystemException
148 status
= tsa
.getMarkedRollbackStatus();
152 public int hashCode() {
153 return xid
.hashCode();