Search in sources :

Example 11 with TransactionException

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

the class AbstractTransactionalCollectionProxy method remove.

public boolean remove(E e) {
    checkTransactionActive();
    checkObjectNotNull(e);
    Data value = getNodeEngine().toData(e);
    Iterator<CollectionItem> iterator = getCollection().iterator();
    long reservedItemId = INVALID_ITEM_ID;
    while (iterator.hasNext()) {
        CollectionItem item = iterator.next();
        if (value.equals(item.getValue())) {
            reservedItemId = item.getItemId();
            break;
        }
    }
    CollectionReserveRemoveOperation operation = new CollectionReserveRemoveOperation(name, reservedItemId, value, tx.getTxnId());
    try {
        Future<CollectionItem> future = operationService.invokeOnPartition(getServiceName(), operation, partitionId);
        CollectionItem item = future.get();
        if (item != null) {
            if (reservedItemId == item.getItemId()) {
                iterator.remove();
                removeFromRecord(reservedItemId);
                itemIdSet.remove(reservedItemId);
                return true;
            }
            if (!itemIdSet.add(item.getItemId())) {
                throw new TransactionException("Duplicate itemId: " + item.getItemId());
            }
            CollectionTxnRemoveOperation op = new CollectionTxnRemoveOperation(name, item.getItemId());
            putToRecord(op);
            return true;
        }
    } catch (Throwable t) {
        throw rethrow(t);
    }
    return false;
}
Also used : CollectionReserveRemoveOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionReserveRemoveOperation) TransactionException(com.hazelcast.transaction.TransactionException) CollectionTxnRemoveOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionTxnRemoveOperation) Data(com.hazelcast.internal.serialization.Data) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem)

Example 12 with TransactionException

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

the class XATransactionRollbackMessageTask method call.

@Override
protected Object call() throws Exception {
    UUID transactionId = parameters;
    TransactionContext transactionContext = endpoint.getTransactionContext(transactionId);
    if (transactionContext == null) {
        throw new TransactionException("No transaction context with given transactionId: " + transactionId);
    }
    Transaction transaction = TransactionAccessor.getTransaction(transactionContext);
    transaction.rollback();
    endpoint.removeTransactionContext(transactionId);
    return null;
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) Transaction(com.hazelcast.transaction.impl.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) UUID(java.util.UUID)

Example 13 with TransactionException

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

the class XATransactionPrepareMessageTask method call.

@Override
protected Object call() throws Exception {
    UUID transactionId = parameters;
    TransactionContext transactionContext = endpoint.getTransactionContext(transactionId);
    if (transactionContext == null) {
        throw new TransactionException("No transaction context with given transactionId: " + transactionId);
    }
    Transaction transaction = TransactionAccessor.getTransaction(transactionContext);
    transaction.prepare();
    return null;
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) Transaction(com.hazelcast.transaction.impl.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) UUID(java.util.UUID)

Example 14 with TransactionException

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

the class ClientTransactionManagerServiceImpl method executeTransaction.

@Override
public <T> T executeTransaction(@Nonnull TransactionOptions options, @Nonnull TransactionalTask<T> task) throws TransactionException {
    checkNotNull(options, "TransactionOptions must not be null!");
    checkNotNull(task, "TransactionalTask is required!");
    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 15 with TransactionException

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

the class TransactionalSetProxy method add.

@Override
public boolean add(E e) {
    checkTransactionActive();
    checkObjectNotNull(e);
    Data value = getNodeEngine().toData(e);
    if (!getCollection().add(new CollectionItem(INVALID_ITEM_ID, value))) {
        return false;
    }
    CollectionReserveAddOperation operation = new CollectionReserveAddOperation(name, tx.getTxnId(), value);
    try {
        Future<Long> future = operationService.invokeOnPartition(getServiceName(), operation, partitionId);
        Long itemId = future.get();
        if (itemId != null) {
            if (!itemIdSet.add(itemId)) {
                throw new TransactionException("Duplicate itemId: " + itemId);
            }
            CollectionTxnAddOperation op = new CollectionTxnAddOperation(name, itemId, value);
            putToRecord(op);
            return true;
        }
    } catch (Throwable t) {
        throw ExceptionUtil.rethrow(t);
    }
    return false;
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) CollectionTxnAddOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionTxnAddOperation) CollectionReserveAddOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionReserveAddOperation) Data(com.hazelcast.internal.serialization.Data) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem)

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