1 package org
.argeo
.cms
.internal
.transaction
;
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 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
20 class SimpleTransaction
implements Transaction
, Status
{
21 private final static Log log
= LogFactory
.getLog(SimpleTransaction
.class);
23 private final Xid xid
;
24 private int status
= Status
.STATUS_ACTIVE
;
25 private final List
<XAResource
> xaResources
= new ArrayList
<XAResource
>();
27 private final SimpleTransactionManager transactionManager
;
29 public SimpleTransaction(SimpleTransactionManager transactionManager
) {
30 this.xid
= new UuidXid();
31 this.transactionManager
= transactionManager
;
35 public synchronized void commit() throws RollbackException
,
36 HeuristicMixedException
, HeuristicRollbackException
,
37 SecurityException
, IllegalStateException
, SystemException
{
38 status
= STATUS_PREPARING
;
39 for (XAResource xaRes
: xaResources
) {
40 if (status
== STATUS_MARKED_ROLLBACK
)
44 } catch (XAException e
) {
45 status
= STATUS_MARKED_ROLLBACK
;
46 log
.error("Cannot prepare " + xaRes
+ " for " + xid
, e
);
49 if (status
== STATUS_MARKED_ROLLBACK
) {
51 throw new RollbackException();
53 status
= STATUS_PREPARED
;
55 status
= STATUS_COMMITTING
;
56 for (XAResource xaRes
: xaResources
) {
57 if (status
== STATUS_MARKED_ROLLBACK
)
60 xaRes
.commit(xid
, false);
61 } catch (XAException e
) {
62 status
= STATUS_MARKED_ROLLBACK
;
63 log
.error("Cannot prepare " + xaRes
+ " for " + xid
, e
);
66 if (status
== STATUS_MARKED_ROLLBACK
) {
68 throw new RollbackException();
72 status
= STATUS_COMMITTED
;
73 if (log
.isDebugEnabled())
74 log
.debug("COMMITTED " + xid
);
75 clearResources(XAResource
.TMSUCCESS
);
76 transactionManager
.unregister(xid
);
80 public synchronized void rollback() throws IllegalStateException
,
82 status
= STATUS_ROLLING_BACK
;
83 for (XAResource xaRes
: xaResources
) {
86 } catch (XAException e
) {
87 log
.error("Cannot rollback " + xaRes
+ " for " + xid
, e
);
92 status
= STATUS_ROLLEDBACK
;
93 if (log
.isDebugEnabled())
94 log
.debug("ROLLEDBACK " + xid
);
95 clearResources(XAResource
.TMFAIL
);
96 transactionManager
.unregister(xid
);
100 public synchronized boolean enlistResource(XAResource xaRes
)
101 throws RollbackException
, IllegalStateException
, SystemException
{
102 if (xaResources
.add(xaRes
)) {
104 xaRes
.start(getXid(), XAResource
.TMNOFLAGS
);
106 } catch (XAException e
) {
107 log
.error("Cannot enlist " + xaRes
, e
);
115 public synchronized boolean delistResource(XAResource xaRes
, int flag
)
116 throws IllegalStateException
, SystemException
{
117 if (xaResources
.remove(xaRes
)) {
119 xaRes
.end(getXid(), flag
);
120 } catch (XAException e
) {
121 log
.error("Cannot delist " + xaRes
, e
);
129 protected void clearResources(int flag
) {
130 for (XAResource xaRes
: xaResources
)
132 xaRes
.end(getXid(), flag
);
133 } catch (XAException e
) {
134 log
.error("Cannot end " + xaRes
, e
);
140 public synchronized int getStatus() throws SystemException
{
145 public void registerSynchronization(Synchronization sync
)
146 throws RollbackException
, IllegalStateException
, SystemException
{
147 throw new UnsupportedOperationException();
151 public void setRollbackOnly() throws IllegalStateException
, SystemException
{
152 status
= STATUS_MARKED_ROLLBACK
;
156 public int hashCode() {
157 return xid
.hashCode();