Search in sources :

Example 1 with TransactionException

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

the class TxnLockAndGetOperation method run.

@Override
public void run() throws Exception {
    if (!recordStore.txnLock(getKey(), ownerUuid, getThreadId(), getCallId(), ttl, blockReads)) {
        throw new TransactionException("Transaction couldn't obtain lock.");
    }
    Record record = recordStore.getRecordOrNull(dataKey);
    if (record == null && shouldLoad) {
        record = recordStore.loadRecordOrNull(dataKey, false);
    }
    Data value = record == null ? null : mapServiceContext.toData(record.getValue());
    response = new VersionedValue(value, record == null ? 0 : record.getVersion());
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) Record(com.hazelcast.map.impl.record.Record) Data(com.hazelcast.nio.serialization.Data)

Example 2 with TransactionException

use of com.hazelcast.transaction.TransactionException 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 3 with TransactionException

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

the class ClientTxnTest method testTxnRollbackOnServerCrash.

@Test
public void testTxnRollbackOnServerCrash() throws Exception {
    final String queueName = randomString();
    final TransactionContext context = client.newTransactionContext();
    CountDownLatch txnRollbackLatch = new CountDownLatch(1);
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    context.beginTransaction();
    final TransactionalQueue queue = context.getQueue(queueName);
    queue.offer(randomString());
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    server.getLifecycleService().terminate();
    try {
        context.commitTransaction();
        fail("commit should throw exception !");
    } catch (TransactionException e) {
        context.rollbackTransaction();
        txnRollbackLatch.countDown();
    }
    assertOpenEventually(txnRollbackLatch);
    assertOpenEventually(memberRemovedLatch);
    final IQueue<Object> q = client.getQueue(queueName);
    assertNull(q.poll());
    assertEquals(0, q.size());
}
Also used : TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.cluster.MembershipEvent) MembershipAdapter(com.hazelcast.cluster.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with TransactionException

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

the class ClientTxnReconnectModeTest method testNewTransactionContext_ReconnectMode_ASYNC.

@Test(expected = HazelcastClientOfflineException.class)
public void testNewTransactionContext_ReconnectMode_ASYNC() throws Throwable {
    ClientConfig config = new ClientConfig();
    config.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    config.getNetworkConfig().setSmartRouting(smartRouting);
    config.getConnectionStrategyConfig().setAsyncStart(true);
    config.getConnectionStrategyConfig().setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
    try {
        client.newTransactionContext();
    } catch (TransactionException e) {
        throw e.getCause();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) ClientConfig(com.hazelcast.client.config.ClientConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with TransactionException

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

the class ClientTxnReconnectModeTest method testNewTransactionContext_After_shutdown.

@Test(expected = HazelcastClientNotActiveException.class)
public void testNewTransactionContext_After_shutdown() throws Throwable {
    ClientConfig config = new ClientConfig();
    config.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    config.getNetworkConfig().setSmartRouting(smartRouting);
    config.getConnectionStrategyConfig().setAsyncStart(true);
    config.getConnectionStrategyConfig().setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
    client.shutdown();
    try {
        client.newTransactionContext();
    } catch (TransactionException e) {
        throw e.getCause();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) ClientConfig(com.hazelcast.client.config.ClientConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TransactionException (com.hazelcast.transaction.TransactionException)106 Test (org.junit.Test)82 QuickTest (com.hazelcast.test.annotation.QuickTest)76 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)72 HazelcastInstance (com.hazelcast.core.HazelcastInstance)70 TransactionalMap (com.hazelcast.transaction.TransactionalMap)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)57 NightlyTest (com.hazelcast.test.annotation.NightlyTest)48 Config (com.hazelcast.config.Config)38 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)34 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TransactionContext (com.hazelcast.transaction.TransactionContext)22 IMap (com.hazelcast.map.IMap)16 TransactionOptions (com.hazelcast.transaction.TransactionOptions)8 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)6 Data (com.hazelcast.internal.serialization.Data)5