use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class XAResourceImpl method createTransactionContext.
private TransactionContext createTransactionContext(Xid xid) {
XAService xaService = getService();
TransactionContext context = xaService.newXATransactionContext(xid, null, timeoutInSeconds.get(), false);
getTransaction(context).begin();
return context;
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class XAResourceImpl method start.
@Override
public void start(Xid xid, int flags) throws XAException {
long threadId = currentThreadId();
TransactionContext threadContext = threadContextMap.get(currentThreadId());
switch(flags) {
case TMNOFLAGS:
List<TransactionContext> contexts = new CopyOnWriteArrayList<TransactionContext>();
List<TransactionContext> currentContexts = xidContextMap.putIfAbsent(xid, contexts);
if (currentContexts != null) {
throw new XAException("There is already TransactionContexts for the given xid: " + xid);
}
TransactionContext context = createTransactionContext(xid);
contexts.add(context);
threadContextMap.put(threadId, context);
break;
case TMRESUME:
case TMJOIN:
List<TransactionContext> contextList = xidContextMap.get(xid);
if (contextList == null) {
throw new XAException("There is no TransactionContexts for the given xid: " + xid);
}
if (threadContext == null) {
threadContext = createTransactionContext(xid);
threadContextMap.put(threadId, threadContext);
contextList.add(threadContext);
}
break;
default:
throw new XAException("Unknown flag! " + flags);
}
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class XAResourceImpl method rollback.
@Override
public void rollback(Xid xid) throws XAException {
List<TransactionContext> contexts = xidContextMap.remove(xid);
if (contexts == null) {
finalizeTransactionRemotely(xid, false);
return;
}
for (TransactionContext context : contexts) {
getTransaction(context).rollback();
}
clearRemoteTransactions(xid);
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class XAResourceImpl method commit.
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
List<TransactionContext> contexts = xidContextMap.remove(xid);
if (contexts == null && onePhase) {
throw new XAException("There is no TransactionContexts for the given xid: " + xid);
}
if (contexts == null) {
finalizeTransactionRemotely(xid, true);
return;
}
for (TransactionContext context : contexts) {
Transaction transaction = getTransaction(context);
if (onePhase) {
transaction.prepare();
}
transaction.commit();
}
clearRemoteTransactions(xid);
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class TransactionListTest method testSingleListAtomicity.
@Test
public void testSingleListAtomicity() throws ExecutionException, InterruptedException {
final int itemCount = 200;
final HazelcastInstance instance = createHazelcastInstance();
final String name = randomString();
Future<Integer> f = spawn(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
IList<Object> set = instance.getList(name);
while (!set.remove("item-1")) {
}
return set.size();
}
});
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
TransactionalList<Object> set = context.getList(name);
for (int i = 0; i < itemCount; i++) {
set.add("item-" + i);
}
context.commitTransaction();
int size = f.get();
assertEquals(itemCount - 1, size);
}
Aggregations