1 package org
.argeo
.transaction
.simple
;
3 import java
.util
.ArrayList
;
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
.XAException
;
14 import javax
.transaction
.xa
.XAResource
;
15 import javax
.transaction
.xa
.Xid
;
17 /** Simple implementation of an XA {@link Transaction}. */
18 class SimpleTransaction
implements Transaction
, Status
{
19 private final Xid xid
;
20 private int status
= Status
.STATUS_ACTIVE
;
21 private final List
<XAResource
> xaResources
= new ArrayList
<XAResource
>();
23 private final SimpleTransactionManager transactionManager
;
25 public SimpleTransaction(SimpleTransactionManager transactionManager
) {
26 this.xid
= new UuidXid();
27 this.transactionManager
= transactionManager
;
31 public synchronized void commit() throws RollbackException
, HeuristicMixedException
, HeuristicRollbackException
,
32 SecurityException
, IllegalStateException
, SystemException
{
33 status
= STATUS_PREPARING
;
34 for (XAResource xaRes
: xaResources
) {
35 if (status
== STATUS_MARKED_ROLLBACK
)
39 } catch (XAException e
) {
40 status
= STATUS_MARKED_ROLLBACK
;
41 error("Cannot prepare " + xaRes
+ " for " + xid
, e
);
44 if (status
== STATUS_MARKED_ROLLBACK
) {
46 throw new RollbackException();
48 status
= STATUS_PREPARED
;
50 status
= STATUS_COMMITTING
;
51 for (XAResource xaRes
: xaResources
) {
52 if (status
== STATUS_MARKED_ROLLBACK
)
55 xaRes
.commit(xid
, false);
56 } catch (XAException e
) {
57 status
= STATUS_MARKED_ROLLBACK
;
58 error("Cannot prepare " + xaRes
+ " for " + xid
, e
);
61 if (status
== STATUS_MARKED_ROLLBACK
) {
63 throw new RollbackException();
67 status
= STATUS_COMMITTED
;
68 clearResources(XAResource
.TMSUCCESS
);
69 transactionManager
.unregister(xid
);
73 public synchronized void rollback() throws IllegalStateException
, SystemException
{
74 status
= STATUS_ROLLING_BACK
;
75 for (XAResource xaRes
: xaResources
) {
78 } catch (XAException e
) {
79 error("Cannot rollback " + xaRes
+ " for " + xid
, e
);
84 status
= STATUS_ROLLEDBACK
;
85 clearResources(XAResource
.TMFAIL
);
86 transactionManager
.unregister(xid
);
90 public synchronized boolean enlistResource(XAResource xaRes
)
91 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
);
105 public synchronized boolean delistResource(XAResource xaRes
, int flag
)
106 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
);
135 public synchronized int getStatus() throws SystemException
{
140 public void registerSynchronization(Synchronization sync
)
141 throws RollbackException
, IllegalStateException
, SystemException
{
142 throw new UnsupportedOperationException();
146 public void setRollbackOnly() throws IllegalStateException
, SystemException
{
147 status
= STATUS_MARKED_ROLLBACK
;
151 public int hashCode() {
152 return xid
.hashCode();