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");
}
}
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();
}
Aggregations