Search in sources :

Example 36 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class MyTransactionFunction method verifyTxStateAndConflicts.

private void verifyTxStateAndConflicts(RegionFunctionContext ctx) {
    Region custPR = ctx.getDataSet();
    Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
    OrderId vOrderId = new OrderId(3000, custId);
    Order vOrder = new Order("vOrder");
    TXManagerImpl mImp = (TXManagerImpl) mgr;
    mImp.begin();
    orderPR.put(vOrderId, vOrder);
    TXStateProxy txState = mImp.internalSuspend();
    Iterator it = txState.getRegions().iterator();
    Assert.assertTrue(txState.getRegions().size() == 1, "Expected 1 region; " + "found:" + txState.getRegions().size());
    LocalRegion lr = (LocalRegion) it.next();
    Assert.assertTrue(lr instanceof BucketRegion);
    TXRegionState txRegion = txState.readRegion(lr);
    TXEntryState txEntry = txRegion.readEntry(txRegion.getEntryKeys().iterator().next());
    mImp.internalResume(txState);
    orderPR.put(vOrderId, new Order("foo"));
    txState = mImp.internalSuspend();
    // since both puts were on same key, verify that
    // TxRegionState and TXEntryState are same
    LocalRegion lr1 = (LocalRegion) txState.getRegions().iterator().next();
    Assert.assertTrue(lr == lr1);
    TXRegionState txRegion1 = txState.readRegion(lr);
    TXEntryState txEntry1 = txRegion1.readEntry(txRegion.getEntryKeys().iterator().next());
    Assert.assertTrue(txEntry == txEntry1);
    // to check for conflicts, start a new transaction, operate on same key,
    // commit the second and expect the first to fail
    mImp.begin();
    orderPR.put(vOrderId, new Order("foobar"));
    mImp.commit();
    // now begin the first
    mImp.internalResume(txState);
    boolean caughtException = false;
    try {
        mImp.commit();
    } catch (CommitConflictException e) {
        caughtException = true;
    }
    if (!caughtException) {
        throw new TestException("An expected exception was not thrown");
    }
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TestException(util.TestException) TXRegionState(org.apache.geode.internal.cache.TXRegionState) ArrayList(java.util.ArrayList) TXEntryState(org.apache.geode.internal.cache.TXEntryState) LocalRegion(org.apache.geode.internal.cache.LocalRegion) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CommitConflictException(org.apache.geode.cache.CommitConflictException) BucketRegion(org.apache.geode.internal.cache.BucketRegion) CustId(org.apache.geode.internal.cache.execute.data.CustId) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Iterator(java.util.Iterator) 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)

Example 37 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class MyTransactionFunction method verifyRepeatableRead.

private void verifyRepeatableRead(RegionFunctionContext ctx) {
    Region custPR = ctx.getDataSet();
    Region orderPR = custPR.getCache().getRegion(PRColocationDUnitTest.OrderPartitionedRegionName);
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    Customer cust = (Customer) args.get(2);
    Assert.assertTrue(custPR.get(custId) == null);
    CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
    TXManagerImpl mImp = (TXManagerImpl) mgr;
    mImp.begin();
    custPR.put(custId, cust);
    Assert.assertTrue(cust.equals(custPR.get(custId)));
    TXStateProxy txState = mImp.internalSuspend();
    Assert.assertTrue(custPR.get(custId) == null);
    mImp.internalResume(txState);
    mImp.commit();
    // change value
    mImp.begin();
    Customer oldCust = (Customer) custPR.get(custId);
    Assert.assertTrue(oldCust.equals(cust));
    txState = mImp.internalSuspend();
    Customer newCust = new Customer("fooNew", "barNew");
    custPR.put(custId, newCust);
    mImp.internalResume(txState);
    Assert.assertTrue(oldCust.equals(custPR.get(custId)));
    mImp.commit();
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) ArrayList(java.util.ArrayList) 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) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Aggregations

TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)37 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)20 Test (org.junit.Test)9 CommitConflictException (org.apache.geode.cache.CommitConflictException)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 IOException (java.io.IOException)6 TransactionException (org.apache.geode.cache.TransactionException)6 InternalCache (org.apache.geode.internal.cache.InternalCache)6 LocalRegion (org.apache.geode.internal.cache.LocalRegion)6 InternalGemFireError (org.apache.geode.InternalGemFireError)5 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)5 EntryExistsException (org.apache.geode.cache.EntryExistsException)5 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 CacheWriterException (org.apache.geode.cache.CacheWriterException)4 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)4 Region (org.apache.geode.cache.Region)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ResourceException (javax.resource.ResourceException)3