]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.cms/src/org/argeo/api/cms/transaction/SimpleTransactionManager.java
1 package org
.argeo
.api
.cms
.transaction
;
3 import java
.util
.Collections
;
4 import java
.util
.HashMap
;
6 import java
.util
.concurrent
.Callable
;
8 import javax
.transaction
.xa
.XAResource
;
9 import javax
.transaction
.xa
.Xid
;
12 * Simple implementation of an XA transaction manager.
14 public class SimpleTransactionManager
15 // implements TransactionManager, UserTransaction
16 implements WorkControl
, WorkTransaction
{
17 private ThreadLocal
<SimpleTransaction
<Integer
>> current
= new ThreadLocal
<SimpleTransaction
<Integer
>>();
19 private Map
<Xid
, SimpleTransaction
<Integer
>> knownTransactions
= Collections
20 .synchronizedMap(new HashMap
<Xid
, SimpleTransaction
<Integer
>>());
21 private TransactionStatusAdapter
<Integer
> tsa
= new JtaStatusAdapter();
22 // private SyncRegistry syncRegistry = new SyncRegistry();
28 public <T
> T
required(Callable
<T
> work
) {
34 } catch (Exception e
) {
36 throw new SimpleRollbackException(e
);
42 public WorkContext
getWorkContext() {
43 return new WorkContext() {
46 public void registerXAResource(XAResource resource
, String recoveryId
) {
47 getTransaction().enlistResource(resource
);
53 * WORK TRANSACTION IMPLEMENTATION
57 public boolean isNoTransactionStatus() {
58 return tsa
.getNoTransactionStatus().equals(getStatus());
66 // throws NotSupportedException, SystemException
68 if (getCurrent() != null)
69 throw new UnsupportedOperationException("Nested transactions are not supported");
70 SimpleTransaction
<Integer
> transaction
= new SimpleTransaction
<Integer
>(this, tsa
);
71 knownTransactions
.put(transaction
.getXid(), transaction
);
72 current
.set(transaction
);
76 // throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
77 // SecurityException, IllegalStateException, SystemException
79 if (getCurrent() == null)
80 throw new IllegalStateException("No transaction registered with the current thread.");
81 getCurrent().commit();
84 public int getStatus()
85 // throws SystemException
87 if (getCurrent() == null)
88 return tsa
.getNoTransactionStatus();
89 return getTransaction().getStatus();
92 public SimpleTransaction
<Integer
> getTransaction()
93 // throws SystemException
98 protected SimpleTransaction
<Integer
> getCurrent()
99 // throws SystemException
101 SimpleTransaction
<Integer
> transaction
= current
.get();
102 if (transaction
== null)
104 Integer status
= transaction
.getStatus();
105 if (status
.equals(tsa
.getCommittedStatus()) || status
.equals(tsa
.getRolledBackStatus())) {
112 void unregister(Xid xid
) {
113 knownTransactions
.remove(xid
);
116 public void resume(SimpleTransaction
<Integer
> tobj
)
117 // throws InvalidTransactionException, IllegalStateException, SystemException
119 if (getCurrent() != null)
120 throw new IllegalStateException("Transaction " + current
.get() + " already registered");
124 public void rollback()
125 // throws IllegalStateException, SecurityException, SystemException
127 if (getCurrent() == null)
128 throw new IllegalStateException("No transaction registered with the current thread.");
129 getCurrent().rollback();
132 public void setRollbackOnly()
133 // throws IllegalStateException, SystemException
135 if (getCurrent() == null)
136 throw new IllegalStateException("No transaction registered with the current thread.");
137 getCurrent().setRollbackOnly();
140 public void setTransactionTimeout(int seconds
)
141 // throws SystemException
143 throw new UnsupportedOperationException();
146 public SimpleTransaction
<Integer
> suspend()
147 // throws SystemException
149 SimpleTransaction
<Integer
> transaction
= getCurrent();
154 // public TransactionSynchronizationRegistry getTsr() {
155 // return syncRegistry;
158 // private class SyncRegistry implements TransactionSynchronizationRegistry {
160 // public Object getTransactionKey() {
162 // SimpleTransaction transaction = getCurrent();
163 // if (transaction == null)
165 // return getCurrent().getXid();
166 // } catch (SystemException e) {
167 // throw new IllegalStateException("Cannot get transaction key", e);
172 // public void putResource(Object key, Object value) {
173 // throw new UnsupportedOperationException();
177 // public Object getResource(Object key) {
178 // throw new UnsupportedOperationException();
182 // public void registerInterposedSynchronization(Synchronization sync) {
183 // throw new UnsupportedOperationException();
187 // public int getTransactionStatus() {
189 // return getStatus();
190 // } catch (SystemException e) {
191 // throw new IllegalStateException("Cannot get status", e);
196 // public boolean getRollbackOnly() {
198 // return getStatus() == Status.STATUS_MARKED_ROLLBACK;
199 // } catch (SystemException e) {
200 // throw new IllegalStateException("Cannot get status", e);
205 // public void setRollbackOnly() {
207 // getCurrent().setRollbackOnly();
208 // } catch (Exception e) {
209 // throw new IllegalStateException("Cannot set rollback only", e);