Search in sources :

Example 61 with TransactionOptions

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

the class TransactionImpl_TwoPhaseTest method rollback_whenAlreadyRolledBack.

@Test(expected = IllegalStateException.class)
public void rollback_whenAlreadyRolledBack() {
    TransactionOptions options = new TransactionOptions().setTransactionType(TWO_PHASE).setDurability(0);
    TransactionImpl tx = new TransactionImpl(txManagerService, nodeEngine, options, UUID.randomUUID());
    tx.begin();
    tx.rollback();
    tx.rollback();
}
Also used : TransactionOptions(com.hazelcast.transaction.TransactionOptions) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 62 with TransactionOptions

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

the class MapTransactionTest method testTransactionAtomicity_withMapAndQueue.

@Test
public void testTransactionAtomicity_withMapAndQueue() throws ExecutionException, InterruptedException {
    final HazelcastInstance instance = createHazelcastInstance();
    Future<Object> future = spawn(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            IQueue<Object> queue = instance.getQueue("queue");
            IMap<Object, Object> map = instance.getMap("map");
            Object item = queue.take();
            return map.get(item);
        }
    });
    TransactionOptions options = new TransactionOptions().setTransactionType(TransactionOptions.TransactionType.ONE_PHASE);
    TransactionContext context = instance.newTransactionContext(options);
    context.beginTransaction();
    TransactionalQueue<Object> queue = context.getQueue("queue");
    TransactionalMap<Object, Object> map = context.getMap("map");
    queue.offer("item-99");
    for (int i = 0; i < 100; i++) {
        map.put("item-" + i, "value");
    }
    context.commitTransaction();
    assertEquals("value", future.get());
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TransactionException(com.hazelcast.transaction.TransactionException) ExecutionException(java.util.concurrent.ExecutionException) ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) TransactionNotActiveException(com.hazelcast.transaction.TransactionNotActiveException) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IQueue(com.hazelcast.collection.IQueue) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionContext(com.hazelcast.transaction.TransactionContext) Mockito.anyObject(org.mockito.Mockito.anyObject) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 63 with TransactionOptions

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

the class TxnMapNearCacheInvalidationTest method after_txn_commit_near_cache_should_be_invalidated.

@Test
public void after_txn_commit_near_cache_should_be_invalidated() {
    Config cfg = getConfig();
    String mapName = "cache";
    MapConfig cacheConfig = cfg.getMapConfig(mapName);
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setInvalidateOnChange(true).setCacheLocalEntries(true).setInMemoryFormat(inMemoryFormat).setSerializeKeys(serializeKeys);
    cacheConfig.setNearCacheConfig(nearCacheConfig);
    HazelcastInstance server = createHazelcastInstance(cfg);
    IMap map = server.getMap(mapName);
    String key = "key";
    String oldValue = "oldValue";
    String updatedValue = "updatedValue";
    // populate imap
    map.put(key, oldValue);
    // populate near cache
    Object valueReadBeforeTxnFromNonTxnMap = map.get(key);
    // begin txn
    TransactionOptions opts = new TransactionOptions();
    opts.setTransactionType(TransactionOptions.TransactionType.TWO_PHASE);
    TransactionContext ctx = server.newTransactionContext(opts);
    ctx.beginTransaction();
    TransactionalMap txnMap = ctx.getMap(mapName);
    Object valueReadInsideTxnFromTxnMapBeforeUpdate = txnMap.get(key);
    txnMap.put(key, updatedValue);
    Object valueReadInsideTxnFromTxnMapAfterUpdate = txnMap.get(key);
    Object valueReadInsideTxnFromNonTxnMapAfterUpdate = map.get(key);
    ctx.commitTransaction();
    // check values read from txn map
    assertEquals(oldValue, valueReadInsideTxnFromTxnMapBeforeUpdate);
    assertEquals(updatedValue, valueReadInsideTxnFromTxnMapAfterUpdate);
    // check values read from non-txn map
    assertEquals(oldValue, valueReadBeforeTxnFromNonTxnMap);
    assertEquals(oldValue, valueReadInsideTxnFromNonTxnMapAfterUpdate);
    Object valueReadAfterTxnFromNonTxnMap = map.get(key);
    assertEquals(updatedValue, valueReadAfterTxnFromNonTxnMap);
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionContext(com.hazelcast.transaction.TransactionContext) NearCacheConfig(com.hazelcast.config.NearCacheConfig) MapConfig(com.hazelcast.config.MapConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 64 with TransactionOptions

use of com.hazelcast.transaction.TransactionOptions in project hazelcast-simulator by hazelcast.

the class MapTransactionContextTest method timestep.

@TimeStep
public void timestep(ThreadState state) {
    int key = state.nextRandom(0, range / 2);
    TransactionOptions transactionOptions = new TransactionOptions().setTransactionType(transactionType).setDurability(durability);
    TransactionContext transactionContext = targetInstance.newTransactionContext(transactionOptions);
    transactionContext.beginTransaction();
    TransactionalMap<Object, Object> txMap = transactionContext.getMap("map");
    try {
        Object val = txMap.getForUpdate(key);
        if (val != null) {
            key = state.nextRandom(range / 2, range);
        }
        txMap.put(key, (long) key);
        transactionContext.commitTransaction();
    } catch (Exception e) {
        logger.fatal("----------------------tx exception -------------------------", e);
        if (failOnException) {
            throw rethrow(e);
        }
        transactionContext.rollbackTransaction();
    }
}
Also used : TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionContext(com.hazelcast.transaction.TransactionContext) TimeStep(com.hazelcast.simulator.test.annotations.TimeStep)

Aggregations

TransactionOptions (com.hazelcast.transaction.TransactionOptions)64 Test (org.junit.Test)41 QuickTest (com.hazelcast.test.annotation.QuickTest)33 HazelcastInstance (com.hazelcast.core.HazelcastInstance)20 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)19 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)13 TransactionException (com.hazelcast.transaction.TransactionException)13 TransactionContext (com.hazelcast.transaction.TransactionContext)10 Parameters (org.junit.runners.Parameterized.Parameters)10 TransactionManagerServiceImpl (com.hazelcast.transaction.impl.TransactionManagerServiceImpl)9 SlowTest (com.hazelcast.test.annotation.SlowTest)8 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)5 Config (com.hazelcast.config.Config)4 AssertTask (com.hazelcast.test.AssertTask)4 NightlyTest (com.hazelcast.test.annotation.NightlyTest)3 TransactionalMap (com.hazelcast.transaction.TransactionalMap)3 Address (com.hazelcast.cluster.Address)2 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)2 MapStoreConfig (com.hazelcast.config.MapStoreConfig)2 ILogger (com.hazelcast.logging.ILogger)2