Search in sources :

Example 16 with TransactionException

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

the class TransactionalQueueProxySupport method offerInternal.

/**
 * Tries to accomodate one more item in the queue in addition to the
 * already offered items. If it succeeds by getting an item ID, it will
 * add the item to the
 * Makes a reservation for a {@link TransactionalQueue#offer} operation
 * and adds the commit operation to the transaction log.
 *
 * @param data    the serialised item being offered
 * @param timeout the wait timeout in milliseconds for the offer reservation
 * @return {@code true} if the item reservation was made
 * @see TxnReserveOfferOperation
 * @see TxnOfferOperation
 */
boolean offerInternal(Data data, long timeout) {
    TxnReserveOfferOperation operation = new TxnReserveOfferOperation(name, timeout, offeredQueue.size(), tx.getTxnId());
    operation.setCallerUuid(tx.getOwnerUuid());
    try {
        Future<Long> future = invoke(operation);
        Long itemId = future.get();
        if (itemId != null) {
            if (!itemIdSet.add(itemId)) {
                throw new TransactionException("Duplicate itemId: " + itemId);
            }
            offeredQueue.offer(new QueueItem(null, itemId, data));
            TxnOfferOperation txnOfferOperation = new TxnOfferOperation(name, itemId, data);
            putToRecord(txnOfferOperation);
            return true;
        }
    } catch (Throwable t) {
        throw ExceptionUtil.rethrow(t);
    }
    return false;
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) TxnReserveOfferOperation(com.hazelcast.collection.impl.txnqueue.operations.TxnReserveOfferOperation) QueueItem(com.hazelcast.collection.impl.queue.QueueItem) TxnOfferOperation(com.hazelcast.collection.impl.txnqueue.operations.TxnOfferOperation)

Example 17 with TransactionException

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

the class MapTransactionRegressionTest method test_Issue615_KeysetWithPredicate.

@Test
public void test_Issue615_KeysetWithPredicate() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map = h2.getMap("default");
    final SampleTestObjects.Employee employee1 = new SampleTestObjects.Employee("abc-123-xvz", 34, true, 10D);
    final SampleTestObjects.Employee employee2 = new SampleTestObjects.Employee("abc-1xvz", 4, true, 7D);
    final SampleTestObjects.Employee employee3 = new SampleTestObjects.Employee("abc-1xasda...vz", 7, true, 1D);
    final SampleTestObjects.Employee employee4 = new SampleTestObjects.Employee("abc-1asdsaxvz", 2, true, 2D);
    map.put(1, employee1);
    try {
        h1.executeTransaction(options, (context) -> {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            assertEquals(0, txMap.keySet(Predicates.sql("age <= 10")).size());
            // put
            txMap.put(2, employee2);
            Set keys = txMap.keySet(Predicates.sql("age <= 10"));
            Iterator iterator = keys.iterator();
            assertEquals(1, keys.size());
            while (iterator.hasNext()) {
                assertEquals(2, ((Integer) iterator.next()).intValue());
            }
            txMap.put(3, employee3);
            txMap.put(4, employee4);
            keys = txMap.keySet(Predicates.sql("age <= 10"));
            assertEquals(3, keys.size());
            // force rollback.
            throw new DummyUncheckedHazelcastTestException();
        });
    } catch (Exception e) {
        if (!(e instanceof DummyUncheckedHazelcastTestException)) {
            throw new RuntimeException(e);
        }
    }
    assertEquals(1, map.size());
    assertEquals(1, map.keySet().size());
    assertEquals(0, map.keySet(Predicates.sql("age <= 10")).size());
    h1.shutdown();
    h2.shutdown();
}
Also used : Set(java.util.Set) Config(com.hazelcast.config.Config) TransactionException(com.hazelcast.transaction.TransactionException) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) SampleTestObjects(com.hazelcast.query.SampleTestObjects) Iterator(java.util.Iterator) EntryObject(com.hazelcast.query.PredicateBuilder.EntryObject) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 18 with TransactionException

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

the class MapTransactionTest method testValues_resultSetContainsUpdatedEntry.

@Test
public void testValues_resultSetContainsUpdatedEntry() throws TransactionException {
    final int nodeCount = 1;
    final String mapName = randomMapName();
    final Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance node = factory.newHazelcastInstance(config);
    final IMap map = node.getMap(mapName);
    final Employee emp = new Employee("name", 77, true, 10D);
    map.put(1, emp);
    node.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Integer, Employee> txMap = context.getMap(mapName);
            emp.setAge(30);
            txMap.put(1, emp);
            Collection<Employee> coll = txMap.values();
            assertEquals(1, coll.size());
            Employee employee = coll.iterator().next();
            assertEquals(30, employee.getAge());
            return true;
        }
    });
    node.shutdown();
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleTestObjects.Employee) TransactionException(com.hazelcast.transaction.TransactionException) Collection(java.util.Collection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 19 with TransactionException

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

the class MapTransactionTest method testDelete_whenNullKey.

// ========================= delete =====================
@Test(expected = NullPointerException.class)
public void testDelete_whenNullKey() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.delete(null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 20 with TransactionException

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

the class MapTransactionTest method testRemove_whenNullKey.

@Test(expected = NullPointerException.class)
public void testRemove_whenNullKey() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.remove(null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) 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