use of org.apache.geode.internal.cache.TXRegionState 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");
}
}
Aggregations