Search in sources :

Example 41 with TransactionalMap

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

the class MapTransactionTest method testKeySet_whenPortableKeysetAndValuesWithPredicates.

@Test
public void testKeySet_whenPortableKeysetAndValuesWithPredicates() throws Exception {
    final String mapName = randomString();
    final Config config = getConfig();
    config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {

        public Portable create(int classId) {
            return new SampleTestObjects.PortableEmployee();
        }
    });
    final HazelcastInstance instance = createHazelcastInstance(config);
    IMap map = instance.getMap(mapName);
    final SampleTestObjects.PortableEmployee emp1 = new SampleTestObjects.PortableEmployee(34, "abc-123-xvz");
    final SampleTestObjects.PortableEmployee emp2 = new SampleTestObjects.PortableEmployee(20, "abc-123-xvz");
    map.put(emp1, emp1);
    final TransactionContext context = instance.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    assertNull(txMap.put(emp2, emp2));
    assertEquals(2, txMap.size());
    assertEquals(2, txMap.keySet().size());
    assertEquals(0, txMap.keySet(Predicates.sql("a = 10")).size());
    assertEquals(0, txMap.values(Predicates.sql("a = 10")).size());
    assertEquals(2, txMap.keySet(Predicates.sql("a >= 10")).size());
    assertEquals(2, txMap.values(Predicates.sql("a >= 10")).size());
    context.commitTransaction();
    assertEquals(2, map.size());
    assertEquals(2, map.values().size());
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) Portable(com.hazelcast.nio.serialization.Portable) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) SampleTestObjects(com.hazelcast.query.SampleTestObjects) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 42 with TransactionalMap

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

the class MapTransactionTest method tesPutIfAbsent_whenNullValue.

@Test(expected = NullPointerException.class)
public void tesPutIfAbsent_whenNullValue() 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.putIfAbsent("key", 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 43 with TransactionalMap

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

the class MapTransactionTest method testValues_WithPredicates_notContains_oldValues.

@Test
public void testValues_WithPredicates_notContains_oldValues() throws TransactionException {
    Config config = getConfig();
    final String mapName = "testValuesWithPredicate_notContains_oldValues";
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap<Integer, Employee> map = h1.getMap(mapName);
    final Employee employeeAtAge22 = new Employee("emin", 22, true, 10D);
    final Employee employeeAtAge23 = new Employee("emin", 23, true, 10D);
    map.put(1, employeeAtAge22);
    h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap(mapName);
            assertEquals(1, txMap.values(Predicates.sql("age > 21")).size());
            txMap.put(1, employeeAtAge23);
            Collection coll = txMap.values(Predicates.sql("age > 21"));
            assertEquals(1, coll.size());
            return true;
        }
    });
    h1.shutdown();
    h2.shutdown();
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) 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 44 with TransactionalMap

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

the class MapTransactionTest method testGetForUpdate_whenNullKey.

// =================== getForUpdate ===============================
@Test(expected = NullPointerException.class)
public void testGetForUpdate_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.getForUpdate(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 45 with TransactionalMap

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

the class MapTransactionLockingTest method testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlockedDuringTransaction.

@Test
public void testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlockedDuringTransaction() throws InterruptedException {
    final HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
    final IMap<String, Object> map = hazelcastInstance.getMap(mapName);
    map.put(key, value);
    hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
            TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
            transactionalMap.putIfAbsent(key, "t");
            assertFalse("Key remains locked!", map.isLocked(key));
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TransactionalMap (com.hazelcast.transaction.TransactionalMap)95 Test (org.junit.Test)77 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)76 QuickTest (com.hazelcast.test.annotation.QuickTest)76 HazelcastInstance (com.hazelcast.core.HazelcastInstance)63 TransactionException (com.hazelcast.transaction.TransactionException)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)55 NightlyTest (com.hazelcast.test.annotation.NightlyTest)42 Config (com.hazelcast.config.Config)39 TransactionContext (com.hazelcast.transaction.TransactionContext)38 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)37 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.map.IMap)28 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)27 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)6 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 Collection (java.util.Collection)6 ExecutionException (java.util.concurrent.ExecutionException)6