Search in sources :

Example 31 with TransactionContext

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;
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 32 with TransactionContext

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);
    }
}
Also used : XAException(javax.transaction.xa.XAException) TransactionContext(com.hazelcast.transaction.TransactionContext) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 33 with TransactionContext

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);
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 34 with TransactionContext

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);
}
Also used : XAException(javax.transaction.xa.XAException) Transaction(com.hazelcast.transaction.impl.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 35 with TransactionContext

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) ExecutionException(java.util.concurrent.ExecutionException) IList(com.hazelcast.core.IList) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionContext (com.hazelcast.transaction.TransactionContext)280 Test (org.junit.Test)209 QuickTest (com.hazelcast.test.annotation.QuickTest)189 ParallelTest (com.hazelcast.test.annotation.ParallelTest)184 HazelcastInstance (com.hazelcast.core.HazelcastInstance)58 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)48 TransactionalMap (com.hazelcast.core.TransactionalMap)33 TransactionException (com.hazelcast.transaction.TransactionException)22 IMap (com.hazelcast.core.IMap)21 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)19 Config (com.hazelcast.config.Config)17 NightlyTest (com.hazelcast.test.annotation.NightlyTest)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 XAException (javax.transaction.xa.XAException)14 TransactionalQueue (com.hazelcast.core.TransactionalQueue)13 TransactionalMultiMap (com.hazelcast.core.TransactionalMultiMap)9 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)9 ExecutionException (java.util.concurrent.ExecutionException)9 ISet (com.hazelcast.core.ISet)8 MultiMap (com.hazelcast.core.MultiMap)8