]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/transaction/SimpleTransaction.java
Cosmetic improvements
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / transaction / SimpleTransaction.java
1 package org.argeo.util.transaction;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.transaction.xa.XAException;
7 import javax.transaction.xa.XAResource;
8 import javax.transaction.xa.Xid;
9
10 /** Simple implementation of an XA transaction. */
11 class SimpleTransaction<T>
12 //implements Transaction, Status
13 {
14 private final Xid xid;
15 private T status;
16 private final List<XAResource> xaResources = new ArrayList<XAResource>();
17
18 private final SimpleTransactionManager transactionManager;
19 private TransactionStatusAdapter<T> tsa;
20
21 public SimpleTransaction(SimpleTransactionManager transactionManager, TransactionStatusAdapter<T> tsa) {
22 this.tsa = tsa;
23 this.status = tsa.getActiveStatus();
24 this.xid = new UuidXid();
25 this.transactionManager = transactionManager;
26 }
27
28 public synchronized void commit()
29 // throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
30 // SecurityException, IllegalStateException, SystemException
31 {
32 status = tsa.getPreparingStatus();
33 for (XAResource xaRes : xaResources) {
34 if (status.equals(tsa.getMarkedRollbackStatus()))
35 break;
36 try {
37 xaRes.prepare(xid);
38 } catch (XAException e) {
39 status = tsa.getMarkedRollbackStatus();
40 error("Cannot prepare " + xaRes + " for " + xid, e);
41 }
42 }
43 if (status.equals(tsa.getMarkedRollbackStatus())) {
44 rollback();
45 throw new SimpleRollbackException();
46 }
47 status = tsa.getPreparedStatus();
48
49 status = tsa.getCommittingStatus();
50 for (XAResource xaRes : xaResources) {
51 if (status.equals(tsa.getMarkedRollbackStatus()))
52 break;
53 try {
54 xaRes.commit(xid, false);
55 } catch (XAException e) {
56 status = tsa.getMarkedRollbackStatus();
57 error("Cannot prepare " + xaRes + " for " + xid, e);
58 }
59 }
60 if (status.equals(tsa.getMarkedRollbackStatus())) {
61 rollback();
62 throw new SimpleRollbackException();
63 }
64
65 // complete
66 status = tsa.getCommittedStatus();
67 clearResources(XAResource.TMSUCCESS);
68 transactionManager.unregister(xid);
69 }
70
71 public synchronized void rollback()
72 // throws IllegalStateException, SystemException
73 {
74 status = tsa.getRollingBackStatus();
75 for (XAResource xaRes : xaResources) {
76 try {
77 xaRes.rollback(xid);
78 } catch (XAException e) {
79 error("Cannot rollback " + xaRes + " for " + xid, e);
80 }
81 }
82
83 // complete
84 status = tsa.getRolledBackStatus();
85 clearResources(XAResource.TMFAIL);
86 transactionManager.unregister(xid);
87 }
88
89 public synchronized boolean enlistResource(XAResource xaRes)
90 // throws RollbackException, IllegalStateException, SystemException
91 {
92 if (xaResources.add(xaRes)) {
93 try {
94 xaRes.start(getXid(), XAResource.TMNOFLAGS);
95 return true;
96 } catch (XAException e) {
97 error("Cannot enlist " + xaRes, e);
98 return false;
99 }
100 } else
101 return false;
102 }
103
104 public synchronized boolean delistResource(XAResource xaRes, int flag)
105 // throws IllegalStateException, SystemException
106 {
107 if (xaResources.remove(xaRes)) {
108 try {
109 xaRes.end(getXid(), flag);
110 } catch (XAException e) {
111 error("Cannot delist " + xaRes, e);
112 return false;
113 }
114 return true;
115 } else
116 return false;
117 }
118
119 protected void clearResources(int flag) {
120 for (XAResource xaRes : xaResources)
121 try {
122 xaRes.end(getXid(), flag);
123 } catch (XAException e) {
124 error("Cannot end " + xaRes, e);
125 }
126 xaResources.clear();
127 }
128
129 protected void error(Object obj, Exception e) {
130 System.err.println(obj);
131 e.printStackTrace();
132 }
133
134 public synchronized T getStatus()
135 // throws SystemException
136 {
137 return status;
138 }
139
140 // public void registerSynchronization(Synchronization sync)
141 // throws RollbackException, IllegalStateException, SystemException {
142 // throw new UnsupportedOperationException();
143 // }
144
145 public void setRollbackOnly()
146 // throws IllegalStateException, SystemException
147 {
148 status = tsa.getMarkedRollbackStatus();
149 }
150
151 @Override
152 public int hashCode() {
153 return xid.hashCode();
154 }
155
156 Xid getXid() {
157 return xid;
158 }
159
160 }