Search in sources :

Example 1 with TXEntryState

use of org.apache.geode.internal.cache.TXEntryState 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)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)1 CommitConflictException (org.apache.geode.cache.CommitConflictException)1 Region (org.apache.geode.cache.Region)1 BucketRegion (org.apache.geode.internal.cache.BucketRegion)1 LocalRegion (org.apache.geode.internal.cache.LocalRegion)1 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)1 TXEntryState (org.apache.geode.internal.cache.TXEntryState)1 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)1 TXRegionState (org.apache.geode.internal.cache.TXRegionState)1 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)1 CustId (org.apache.geode.internal.cache.execute.data.CustId)1 Order (org.apache.geode.internal.cache.execute.data.Order)1 OrderId (org.apache.geode.internal.cache.execute.data.OrderId)1 TestException (util.TestException)1