Search in sources :

Example 81 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class DistributedTransactionDUnitTest method testBasicDistributedTX.

@Test
public void testBasicDistributedTX() throws Exception {
    Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    createPersistentPR(new VM[] { server1, server2 });
    execute(server2, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.setDistributed(true);
            getGemfireCache().getLoggerI18n().fine("TEST:DISTTX=" + mgr.isDistributed());
            getGemfireCache().getLoggerI18n().fine("TEST:TX BEGIN");
            mgr.begin();
            Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
            CustId custIdOne = new CustId(1);
            Customer customerOne = new Customer("name1", "addr1");
            getGemfireCache().getLoggerI18n().fine("TEST:TX PUT 1");
            prRegion.put(custIdOne, customerOne);
            CustId custIdTwo = new CustId(2);
            Customer customerTwo = new Customer("name2", "addr2");
            getGemfireCache().getLoggerI18n().fine("TEST:TX PUT 2");
            prRegion.put(custIdTwo, customerTwo);
            getGemfireCache().getLoggerI18n().fine("TEST:TX COMMIT");
            mgr.commit();
            return null;
        }
    });
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Host(org.apache.geode.test.dunit.Host) CommitConflictException(org.apache.geode.cache.CommitConflictException) CommitIncompleteException(org.apache.geode.cache.CommitIncompleteException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 82 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class DistributedTransactionDUnitTest method testRemoveAllWithTransactions.

@Test
public void testRemoveAllWithTransactions() throws Exception {
    Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM server3 = host.getVM(2);
    createRegions(new VM[] { server1, server2, server3 });
    execute(server1, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region custRegion = getCache().getRegion(CUSTOMER_PR);
            Region orderRegion = getCache().getRegion(ORDER_PR);
            Map custMap = new HashMap();
            Map orderMap = new HashMap();
            for (int i = 0; i < 15; i++) {
                CustId custId = new CustId(i);
                Customer customer = new Customer("customer" + i, "address" + i);
                OrderId orderId = new OrderId(i, custId);
                Order order = new Order("order" + i);
                custMap.put(custId, customer);
                orderMap.put(orderId, order);
            }
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.setDistributed(true);
            mgr.begin();
            custRegion.putAll(custMap);
            orderRegion.putAll(orderMap);
            mgr.commit();
            mgr.begin();
            assertEquals(15, custRegion.size());
            assertEquals(15, orderRegion.size());
            custMap = new HashMap();
            orderMap = new HashMap();
            for (int i = 5; i < 10; i++) {
                CustId custId = new CustId(i);
                Customer customer = new Customer("customer" + i, "address" + i);
                OrderId orderId = new OrderId(i, custId);
                Order order = new Order("order" + i);
                custMap.put(custId, customer);
                orderMap.put(orderId, order);
            }
            custRegion.removeAll(custMap.keySet());
            orderRegion.removeAll(orderMap.keySet());
            mgr.rollback();
            mgr.begin();
            assertEquals(15, custRegion.size());
            assertEquals(15, orderRegion.size());
            custRegion.removeAll(custMap.keySet());
            orderRegion.removeAll(orderMap.keySet());
            assertEquals(10, custRegion.size());
            assertEquals(10, orderRegion.size());
            mgr.commit();
            assertEquals(10, custRegion.size());
            assertEquals(10, orderRegion.size());
            return null;
        }
    });
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) HashMap(java.util.HashMap) Customer(org.apache.geode.internal.cache.execute.data.Customer) Host(org.apache.geode.test.dunit.Host) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) CommitConflictException(org.apache.geode.cache.CommitConflictException) CommitIncompleteException(org.apache.geode.cache.CommitIncompleteException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CustId(org.apache.geode.internal.cache.execute.data.CustId) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 83 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class DistributedTransactionDUnitTest method testCommitOnPartitionedAndReplicatedRegions.

@SuppressWarnings("serial")
@Test
public void testCommitOnPartitionedAndReplicatedRegions() throws Exception {
    Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM server3 = host.getVM(2);
    createPR(new VM[] { server1, server2, server3 });
    createRR(new VM[] { server1, server2, server3 });
    execute(server1, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.setDistributed(true);
            mgr.begin();
            Region<CustId, Customer> rrRegion = getCache().getRegion(CUSTOMER_RR);
            Region<CustId, Customer> prRegion = getCache().getRegion(CUSTOMER_PR);
            CustId custIdOne = new CustId(1);
            Customer customerOne = new Customer("name1", "addr1");
            prRegion.put(custIdOne, customerOne);
            rrRegion.put(custIdOne, customerOne);
            CustId custIdTwo = new CustId(2);
            Customer customerTwo = new Customer("name2", "addr2");
            rrRegion.put(custIdTwo, customerTwo);
            mgr.commit();
            // Verify
            assertEquals(2, rrRegion.size());
            assertTrue(rrRegion.containsKey(custIdOne));
            assertTrue(rrRegion.containsKey(custIdTwo));
            assertEquals(customerOne, rrRegion.get(custIdOne));
            assertEquals(customerTwo, rrRegion.get(custIdTwo));
            assertEquals(1, prRegion.size());
            assertTrue(prRegion.containsKey(custIdOne));
            assertEquals(customerOne, rrRegion.get(custIdOne));
            return null;
        }
    });
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Host(org.apache.geode.test.dunit.Host) CommitConflictException(org.apache.geode.cache.CommitConflictException) CommitIncompleteException(org.apache.geode.cache.CommitIncompleteException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 84 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class DistributedTransactionDUnitTest method testTxWithSingleDataStore.

@Test
public void testTxWithSingleDataStore() throws Exception {
    Host host = Host.getHost(0);
    // datastore
    VM server1 = host.getVM(0);
    final String CUSTOMER_PR1 = "CUSTOMER_PR1";
    final String CUSTOMER_PR2 = "CUSTOMER_PR2";
    // Create CUSTOMER_PR1 on server1
    execute(server1, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setConcurrencyChecksEnabled(getConcurrencyChecksEnabled());
            af.setPartitionAttributes(new PartitionAttributesFactory<CustId, Customer>().setTotalNumBuckets(4).setLocalMaxMemory(1).setPartitionResolver(new CustomerIDPartitionResolver("resolver1")).setRedundantCopies(0).create());
            getCache().createRegion(CUSTOMER_PR1, af.create());
            return null;
        }
    });
    // Create CUSTOMER_PR2 on server2
    execute(server1, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setConcurrencyChecksEnabled(getConcurrencyChecksEnabled());
            af.setPartitionAttributes(new PartitionAttributesFactory<CustId, Customer>().setTotalNumBuckets(4).setLocalMaxMemory(1).setPartitionResolver(new CustomerIDPartitionResolver("resolver2")).setRedundantCopies(0).create());
            getCache().createRegion(CUSTOMER_PR2, af.create());
            return null;
        }
    });
    // Now perform tx ops on accessor
    execute(server1, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.setDistributed(true);
            mgr.begin();
            Region<CustId, Customer> custPR1 = getCache().getRegion(CUSTOMER_PR1);
            CustId custIdOne = new CustId(1);
            Customer customerOne = new Customer("name1", "addr1");
            custPR1.put(custIdOne, customerOne);
            Region<CustId, Customer> custPR2 = getCache().getRegion(CUSTOMER_PR2);
            custPR2.put(custIdOne, customerOne);
            mgr.commit();
            // Verify
            assertEquals(1, custPR1.size());
            assertEquals(1, custPR2.size());
            return null;
        }
    });
    execute(server1, new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region<CustId, Customer> custPR1 = getCache().getRegion(CUSTOMER_PR1);
            assertEquals(1, custPR1.size());
            CustId custIdOne = new CustId(1);
            Customer customerOne = new Customer("name1", "addr1");
            assertEquals(customerOne, custPR1.get(custIdOne));
            return null;
        }
    });
}
Also used : Customer(org.apache.geode.internal.cache.execute.data.Customer) CustomerIDPartitionResolver(org.apache.geode.internal.cache.execute.CustomerIDPartitionResolver) Host(org.apache.geode.test.dunit.Host) CommitConflictException(org.apache.geode.cache.CommitConflictException) CommitIncompleteException(org.apache.geode.cache.CommitIncompleteException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CustId(org.apache.geode.internal.cache.execute.data.CustId) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 85 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class PRTXJUnitTest method testTxId.

@Test
public void testTxId() {
    AttributesFactory<String, Integer> attributesFactory = new AttributesFactory<>();
    attributesFactory.setPartitionAttributes(new PartitionAttributesFactory<String, Integer>().setTotalNumBuckets(2).create());
    Region<String, Integer> region = this.cache.createRegion("testTxId", attributesFactory.create());
    region.put("one", 1);
    CacheTransactionManager txManager = this.cache.getTxManager();
    txManager.begin();
    region.put("two", 2);
    txManager.getTransactionId();
    txManager.rollback();
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) TXJUnitTest(org.apache.geode.TXJUnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)120 Region (org.apache.geode.cache.Region)81 Test (org.junit.Test)77 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)53 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)52 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)51 VM (org.apache.geode.test.dunit.VM)51 LocalRegion (org.apache.geode.internal.cache.LocalRegion)46 CommitConflictException (org.apache.geode.cache.CommitConflictException)45 Host (org.apache.geode.test.dunit.Host)45 CustId (org.apache.geode.internal.cache.execute.data.CustId)37 Customer (org.apache.geode.internal.cache.execute.data.Customer)34 BucketRegion (org.apache.geode.internal.cache.BucketRegion)27 AttributesFactory (org.apache.geode.cache.AttributesFactory)26 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)24 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)23 IgnoredException (org.apache.geode.test.dunit.IgnoredException)23 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)22 CacheWriterException (org.apache.geode.cache.CacheWriterException)21 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)21