Search in sources :

Example 86 with TransactionException

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

the class TransactionImpl_TwoPhaseIntegrationTest method commit_whenPrepareSkippedButCommitRunsIntoConflict.

@Test
public void commit_whenPrepareSkippedButCommitRunsIntoConflict() {
    TransactionOptions options = new TransactionOptions().setTransactionType(TWO_PHASE).setDurability(1);
    TransactionImpl tx = new TransactionImpl(localTxService, localNodeEngine, options, txOwner);
    tx.begin();
    MockTransactionLogRecord record = new MockTransactionLogRecord().failCommit();
    tx.add(record);
    try {
        tx.commit();
        fail();
    } catch (TransactionException expected) {
    }
    assertCommitFailed(tx);
    assertNoBackupLogOnRemote(tx);
    record.assertPrepareNotCalled().assertCommitCalled().assertRollbackNotCalled();
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) TransactionOptions(com.hazelcast.transaction.TransactionOptions) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 87 with TransactionException

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

the class TransactionImpl_TwoPhaseTest method commit_whenThrowsExceptionDuringCommit.

@Test
public void commit_whenThrowsExceptionDuringCommit() throws Exception {
    TransactionOptions options = new TransactionOptions().setTransactionType(TWO_PHASE).setDurability(0);
    TransactionImpl tx = new TransactionImpl(txManagerService, nodeEngine, options, "dummy-uuid");
    tx.begin();
    tx.add(new MockTransactionLogRecord().failCommit());
    tx.prepare();
    try {
        tx.commit();
        fail();
    } catch (TransactionException expected) {
    }
    assertEquals(COMMIT_FAILED, tx.getState());
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) TransactionOptions(com.hazelcast.transaction.TransactionOptions) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 88 with TransactionException

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

the class MapTransactionTest method testValues_withPagingPredicate.

@Test(expected = IllegalArgumentException.class)
public void testValues_withPagingPredicate() throws TransactionException {
    final int nodeCount = 1;
    final String mapName = randomMapName("testValuesWithPagingPredicate");
    final Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance node = factory.newHazelcastInstance(config);
    final IMap<Integer, Employee> 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);
            PagingPredicate<Integer, Employee> predicate = new PagingPredicate<Integer, Employee>(5);
            txMap.values(predicate);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) PagingPredicate(com.hazelcast.query.PagingPredicate) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) TransactionException(com.hazelcast.transaction.TransactionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 89 with TransactionException

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

the class MapTransactionTest method testReplace.

@Test
public void testReplace() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map1 = h1.getMap("default");
    final IMap map2 = h2.getMap("default");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            assertNull(txMap.replace("1", "value"));
            txMap.put("1", "value2");
            assertEquals("value2", txMap.replace("1", "value3"));
            assertEquals("value3", txMap.get("1"));
            assertNull(map1.get("1"));
            assertNull(map2.get("2"));
            return true;
        }
    });
    assertTrue(b);
    assertEquals("value3", map1.get("1"));
    assertEquals("value3", map2.get("1"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 90 with TransactionException

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

the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb.

@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb() {
    final String mapName = randomMapName();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(null);
    Config config = new Config();
    MapStoreConfig storeConfig = new MapStoreConfig();
    storeConfig.setEnabled(true).setImplementation(mock);
    config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    instance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> map = context.getMap(mapName);
            Object value = map.getForUpdate(1);
            assertNull("value should be null", value);
            verify(mock, times(1)).load(anyObject());
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.core.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionException (com.hazelcast.transaction.TransactionException)91 Test (org.junit.Test)74 HazelcastInstance (com.hazelcast.core.HazelcastInstance)68 QuickTest (com.hazelcast.test.annotation.QuickTest)66 ParallelTest (com.hazelcast.test.annotation.ParallelTest)62 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)62 TransactionalMap (com.hazelcast.core.TransactionalMap)60 NightlyTest (com.hazelcast.test.annotation.NightlyTest)51 Config (com.hazelcast.config.Config)39 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)39 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)35 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.core.IMap)18 TransactionContext (com.hazelcast.transaction.TransactionContext)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 TransactionOptions (com.hazelcast.transaction.TransactionOptions)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5