1 | |
package org.apache.ojb.odmg; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import javax.transaction.Status; |
19 | |
import javax.transaction.Synchronization; |
20 | |
|
21 | |
import org.apache.ojb.broker.PersistenceBroker; |
22 | |
import org.apache.ojb.broker.accesslayer.ConnectionManagerIF; |
23 | |
import org.apache.ojb.broker.util.logging.Logger; |
24 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
25 | |
import org.odmg.LockNotGrantedException; |
26 | |
import org.odmg.ODMGRuntimeException; |
27 | |
import org.odmg.TransactionAbortedException; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class J2EETransactionImpl extends TransactionImpl implements Synchronization |
36 | |
{ |
37 | |
private Logger log = LoggerFactory.getLogger(J2EETransactionImpl.class); |
38 | |
private boolean isInExternTransaction; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
private boolean beforeCompletionCall = false; |
46 | |
private boolean afterCompletionCall = false; |
47 | |
|
48 | |
public J2EETransactionImpl(ImplementationImpl implementation) |
49 | |
{ |
50 | |
super(implementation); |
51 | |
isInExternTransaction = false; |
52 | |
} |
53 | |
|
54 | |
public void setInExternTransaction(boolean mode) |
55 | |
{ |
56 | |
isInExternTransaction = mode; |
57 | |
} |
58 | |
|
59 | |
public boolean isInExternTransaction() |
60 | |
{ |
61 | |
return isInExternTransaction; |
62 | |
} |
63 | |
|
64 | |
public void join() |
65 | |
{ |
66 | |
throw new UnsupportedOperationException("Not supported in managed enviroment"); |
67 | |
} |
68 | |
|
69 | |
public void leave() |
70 | |
{ |
71 | |
throw new UnsupportedOperationException("Not supported in managed enviroment"); |
72 | |
} |
73 | |
|
74 | |
public void checkpoint() |
75 | |
{ |
76 | |
throw new UnsupportedOperationException("Not supported in managed enviroment"); |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public void afterCompletion(int status) |
85 | |
{ |
86 | |
if(afterCompletionCall) return; |
87 | |
|
88 | |
log.info("Method afterCompletion was called"); |
89 | |
try |
90 | |
{ |
91 | |
switch(status) |
92 | |
{ |
93 | |
case Status.STATUS_COMMITTED: |
94 | |
if(log.isDebugEnabled()) |
95 | |
{ |
96 | |
log.debug("Method afterCompletion: Do commit internal odmg-tx, status of JTA-tx is " + TxUtil.getStatusString(status)); |
97 | |
} |
98 | |
commit(); |
99 | |
break; |
100 | |
default: |
101 | |
log.error("Method afterCompletion: Do abort call on internal odmg-tx, status of JTA-tx is " + TxUtil.getStatusString(status)); |
102 | |
abort(); |
103 | |
} |
104 | |
} |
105 | |
finally |
106 | |
{ |
107 | |
afterCompletionCall = true; |
108 | |
log.info("Method afterCompletion finished"); |
109 | |
} |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void beforeCompletion() |
123 | |
{ |
124 | |
|
125 | |
if(beforeCompletionCall) return; |
126 | |
|
127 | |
log.info("Method beforeCompletion was called"); |
128 | |
int status = Status.STATUS_UNKNOWN; |
129 | |
try |
130 | |
{ |
131 | |
JTATxManager mgr = (JTATxManager) getImplementation().getTxManager(); |
132 | |
status = mgr.getJTATransaction().getStatus(); |
133 | |
|
134 | |
|
135 | |
if(status == Status.STATUS_MARKED_ROLLBACK |
136 | |
|| status == Status.STATUS_ROLLEDBACK |
137 | |
|| status == Status.STATUS_ROLLING_BACK |
138 | |
|| status == Status.STATUS_UNKNOWN |
139 | |
|| status == Status.STATUS_NO_TRANSACTION) |
140 | |
{ |
141 | |
log.error("Synchronization#beforeCompletion: Can't prepare for commit, because tx status was " |
142 | |
+ TxUtil.getStatusString(status) + ". Do internal cleanup only."); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | |
if(log.isDebugEnabled()) |
147 | |
{ |
148 | |
log.debug("Synchronization#beforeCompletion: Prepare for commit"); |
149 | |
} |
150 | |
|
151 | |
prepareCommit(); |
152 | |
} |
153 | |
} |
154 | |
catch(Exception e) |
155 | |
{ |
156 | |
log.error("Synchronization#beforeCompletion: Error while prepare for commit", e); |
157 | |
if(e instanceof LockNotGrantedException) |
158 | |
{ |
159 | |
throw (LockNotGrantedException) e; |
160 | |
} |
161 | |
else if(e instanceof TransactionAbortedException) |
162 | |
{ |
163 | |
throw (TransactionAbortedException) e; |
164 | |
} |
165 | |
else if(e instanceof ODMGRuntimeException) |
166 | |
{ |
167 | |
throw (ODMGRuntimeException) e; |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | |
throw new ODMGRuntimeException("Method beforeCompletion() fails, status of JTA-tx was " |
172 | |
+ TxUtil.getStatusString(status) + ", message: " + e.getMessage()); |
173 | |
} |
174 | |
|
175 | |
} |
176 | |
finally |
177 | |
{ |
178 | |
beforeCompletionCall = true; |
179 | |
setInExternTransaction(false); |
180 | |
internalCleanup(); |
181 | |
} |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
private void internalCleanup() |
188 | |
{ |
189 | |
if(hasBroker()) |
190 | |
{ |
191 | |
PersistenceBroker broker = getBroker(); |
192 | |
if(log.isDebugEnabled()) |
193 | |
{ |
194 | |
log.debug("Do internal cleanup and close the internal used connection without" + |
195 | |
" closing the used broker"); |
196 | |
} |
197 | |
ConnectionManagerIF cm = broker.serviceConnectionManager(); |
198 | |
if(cm.isInLocalTransaction()) |
199 | |
{ |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
cm.localCommit(); |
208 | |
} |
209 | |
cm.releaseConnection(); |
210 | |
} |
211 | |
} |
212 | |
|
213 | |
public void commit() |
214 | |
{ |
215 | |
try |
216 | |
{ |
217 | |
|
218 | |
if(log.isDebugEnabled()) log.debug("Commit transaction " + this + ", commit on broker " + broker); |
219 | |
if(hasBroker()) |
220 | |
{ |
221 | |
getBroker().commitTransaction(); |
222 | |
doClose(); |
223 | |
} |
224 | |
setStatus(Status.STATUS_COMMITTED); |
225 | |
|
226 | |
performTransactionAwareAfterCommit(); |
227 | |
} |
228 | |
catch(Exception ex) |
229 | |
{ |
230 | |
|
231 | |
log.error("Unexpected error while do commit on used PB-Instance and close resources", ex); |
232 | |
abort(); |
233 | |
} |
234 | |
} |
235 | |
|
236 | |
public void abort() |
237 | |
{ |
238 | |
if(getStatus() == Status.STATUS_ROLLEDBACK) return; |
239 | |
|
240 | |
try |
241 | |
{ |
242 | |
try |
243 | |
{ |
244 | |
doAbort(); |
245 | |
} |
246 | |
catch(Exception ignore) |
247 | |
{ |
248 | |
log.error("Failure while do abort call", ignore); |
249 | |
} |
250 | |
|
251 | |
getImplementation().getTxManager().abortExternalTx(this); |
252 | |
|
253 | |
try |
254 | |
{ |
255 | |
doClose(); |
256 | |
} |
257 | |
catch(Exception e) |
258 | |
{ |
259 | |
log.error("Failure while do abort call", e); |
260 | |
} |
261 | |
setStatus(Status.STATUS_ROLLEDBACK); |
262 | |
} |
263 | |
finally |
264 | |
{ |
265 | |
setInExternTransaction(false); |
266 | |
} |
267 | |
} |
268 | |
} |