Search in sources :

Example 6 with TransactionException

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

the class TransactionQueueTest method testQueueWithMap.

@Test
public void testQueueWithMap() {
    int insCount = 4;
    String queueName = "defQueue";
    String mapName = "defMap";
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
    HazelcastInstance[] instances = factory.newInstances(getConfig());
    instances[0].getMap(mapName).lock("lock1");
    TransactionContext context = instances[1].newTransactionContext(new TransactionOptions().setTimeout(5, SECONDS));
    context.beginTransaction();
    try {
        boolean offered = context.getQueue(queueName).offer(new VersionedObject<>("item1"));
        assertTrue(offered);
        context.getMap(mapName).put("lock1", "value1");
        fail();
    } catch (TransactionException ex) {
        // expected
        context.rollbackTransaction();
    }
    assertEquals(0, instances[0].getQueue(queueName).size());
    assertNull(instances[0].getMap(mapName).get("lock1"));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with TransactionException

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

the class TransactionQueueTest method testPeekMethod.

@Test
public void testPeekMethod() throws Exception {
    int insCount = 4;
    String name = "defQueue";
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
    HazelcastInstance[] instances = factory.newInstances(getConfig());
    TransactionContext context = instances[0].newTransactionContext();
    context.beginTransaction();
    try {
        TransactionalQueue<VersionedObject<String>> q = context.getQueue(name);
        VersionedObject<String> response1 = q.peek(10, SECONDS);
        assertNull(response1);
        assertTrue(q.offer(new VersionedObject<>("ali")));
        VersionedObject<String> response2 = q.peek();
        assertEquals(new VersionedObject<>("ali"), response2);
        context.commitTransaction();
    } catch (TransactionException e) {
        context.rollbackTransaction();
        throw e;
    }
    assertEquals(1, getQueue(instances, name).size());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with TransactionException

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

the class QueueContainer method txnCommitOffer.

/**
 * Sets the data of a reserved item and commits the change so it can be
 * visible outside a transaction.
 * The commit means that the item is offered to the queue if
 * {@code backup} is false or saved into a backup map if {@code backup} is {@code true}.
 * This is because a node can hold backups for queues on other nodes.
 * Cancels the queue eviction if one is scheduled.
 *
 * @param itemId the ID of the reserved item
 * @param data   the data to be associated with the reserved item
 * @param backup if the item is to be offered to the underlying queue or stored as a backup
 * @return {@code true} if the commit succeeded
 * @throws TransactionException if there is no reserved item with the {@code itemId}
 */
public boolean txnCommitOffer(long itemId, Data data, boolean backup) {
    QueueItem item = txMap.remove(itemId);
    if (item == null && !backup) {
        throw new TransactionException("No reserve: " + itemId);
    } else if (item == null) {
        item = new QueueItem(this, itemId, data);
    }
    item.setSerializedObject(data);
    if (!backup) {
        getItemQueue().offer(item);
        cancelEvictionIfExists();
    } else {
        getBackupMap().put(itemId, item);
    }
    if (store.isEnabled() && !backup) {
        try {
            store.store(item.getItemId(), data);
        } catch (Exception e) {
            logger.warning("Exception during store", e);
        }
    }
    return true;
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TransactionException(com.hazelcast.transaction.TransactionException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException)

Example 9 with TransactionException

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

the class ClusterStateManager method commitClusterState.

public void commitClusterState(ClusterStateChange stateChange, Address initiator, UUID txnId, boolean isTransient) {
    Preconditions.checkNotNull(stateChange);
    stateChange.validate();
    clusterServiceLock.lock();
    try {
        final LockGuard stateLock = getStateLock();
        if (!stateLock.allowsUnlock(txnId)) {
            throw new TransactionException("Cluster state change [" + state + " -> " + stateChange + "] failed for " + initiator + ", current state: " + stateToString());
        }
        if (stateChange.isOfType(ClusterState.class)) {
            ClusterState newState = (ClusterState) stateChange.getNewState();
            doSetClusterState(newState, isTransient);
            // if state is changed to allow joins, then remove all members which left while not active.
            if (newState.isJoinAllowed()) {
                node.getClusterService().getMembershipManager().removeAllMissingMembers();
            }
        } else if (stateChange.isOfType(Version.class)) {
            // version is validated on cluster-state-lock, thus we can commit without checking compatibility
            Version newVersion = (Version) stateChange.getNewState();
            logger.info("Cluster version set to " + newVersion);
            doSetClusterVersion(newVersion);
        } else {
            throw new IllegalArgumentException("Illegal ClusterStateChange of type " + stateChange.getType() + ".");
        }
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : ClusterState(com.hazelcast.cluster.ClusterState) TransactionException(com.hazelcast.transaction.TransactionException) Version(com.hazelcast.version.Version) LockGuard(com.hazelcast.internal.util.LockGuard)

Example 10 with TransactionException

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

the class ClientTransactionUtil method invoke.

/**
 * Handles the invocation exception for transactions so that users will not see internal exceptions.
 * <p>
 * More specifically IOException, because in case of a IO problem in ClientInvocation that send to a connection
 * sends IOException to user. This wraps that exception into a TransactionException.
 */
public static ClientMessage invoke(ClientMessage request, Object objectName, HazelcastClientInstanceImpl client, Connection connection) {
    try {
        final ClientInvocation clientInvocation = new ClientInvocation(client, request, objectName, connection);
        final Future<ClientMessage> future = clientInvocation.invoke();
        return future.get();
    } catch (Exception e) {
        throw rethrow(e, TRANSACTION_EXCEPTION_WRAPPER);
    }
}
Also used : ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) TransactionException(com.hazelcast.transaction.TransactionException)

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