Search in sources :

Example 76 with TransactionContext

use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.

the class XAResourceProxy 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 77 with TransactionContext

use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.

the class XAResourceProxy method end.

@Override
public void end(Xid xid, int flags) throws XAException {
    long threadId = currentThreadId();
    TransactionContext threadContext = threadContextMap.remove(threadId);
    ILogger logger = getContext().getLoggingService().getLogger(this.getClass());
    if (threadContext == null && logger.isFinestEnabled()) {
        logger.finest("There is no TransactionContext for the current thread: " + threadId);
    }
    List<TransactionContext> contexts = xidContextMap.get(xid);
    if (contexts == null && logger.isFinestEnabled()) {
        logger.finest("There is no TransactionContexts for the given xid: " + xid);
    }
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext) ILogger(com.hazelcast.logging.ILogger)

Example 78 with TransactionContext

use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.

the class ClientTransactionManagerServiceImpl method executeTransaction.

@Override
public <T> T executeTransaction(TransactionOptions options, TransactionalTask<T> task) throws TransactionException {
    final TransactionContext context = newTransactionContext(options);
    context.beginTransaction();
    try {
        final T value = task.execute(context);
        context.commitTransaction();
        return value;
    } catch (Throwable e) {
        context.rollbackTransaction();
        if (e instanceof TransactionException) {
            throw (TransactionException) e;
        }
        if (e.getCause() instanceof TransactionException) {
            throw (TransactionException) e.getCause();
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new TransactionException(e);
    }
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 79 with TransactionContext

use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.

the class ClientMapUnboundReturnValuesTestSupport method internalRunTxn.

/**
     * Calls {@link TransactionalMap} methods once which are expected to throw {@link QueryResultSizeExceededException}.
     * <p/>
     * This method requires the map to be filled to an amount where the exception is safely triggered.
     * <p/>
     * This methods fails if any of the called methods does not trigger the exception.
     */
private void internalRunTxn(String mapName) {
    TransactionContext transactionContext = instance.newTransactionContext();
    try {
        transactionContext.beginTransaction();
        TransactionalMap<Object, Integer> txnMap = transactionContext.getMap(mapName);
        try {
            txnMap.values(TruePredicate.INSTANCE);
            failExpectedException("TransactionalMap.values(predicate)");
        } catch (QueryResultSizeExceededException e) {
            checkException(e);
        }
        try {
            txnMap.keySet(TruePredicate.INSTANCE);
            failExpectedException("TransactionalMap.keySet(predicate)");
        } catch (QueryResultSizeExceededException e) {
            checkException(e);
        }
        try {
            txnMap.values();
            failExpectedException("TransactionalMap.values()");
        } catch (QueryResultSizeExceededException e) {
            checkException(e);
        }
        try {
            txnMap.keySet();
            failExpectedException("TransactionalMap.keySet()");
        } catch (QueryResultSizeExceededException e) {
            checkException(e);
        }
    } finally {
        transactionContext.rollbackTransaction();
    }
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext) QueryResultSizeExceededException(com.hazelcast.map.QueryResultSizeExceededException)

Example 80 with TransactionContext

use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.

the class ClientTransactionalMapQuorumTest method testTxContainsKeySucceedsWhenQuorumSizeMet.

@Test
public void testTxContainsKeySucceedsWhenQuorumSizeMet() {
    TransactionContext transaction = getTransactionFromMajority();
    TransactionalMap<Object, Object> map = getMap(transaction);
    map.containsKey("foo");
    transaction.commitTransaction();
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext) 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