use of org.apache.geode.cache.CacheTransactionManager 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();
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class MyTransactionFunction method verifyDestroyOperation.
private void verifyDestroyOperation(RegionFunctionContext ctx) {
Region custPR = ctx.getDataSet();
Region orderPR = custPR.getCache().getRegion(PRColocationDUnitTest.OrderPartitionedRegionName);
CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
ArrayList args = (ArrayList) ctx.getArguments();
CustId custId = (CustId) args.get(1);
Customer newCus = (Customer) args.get(2);
OrderId orderId = (OrderId) args.get(3);
Order order = (Order) args.get(4);
Customer oldCustomer = (Customer) custPR.get(custId);
Customer commitedCust = null;
// test destroy rollback
mgr.begin();
custPR.put(custId, newCus);
custPR.destroy(custId);
orderPR.put(orderId, order);
mgr.rollback();
commitedCust = (Customer) custPR.get(custId);
Assert.assertTrue(oldCustomer.equals(commitedCust), "Expected customer to rollback to:" + oldCustomer + " but was:" + commitedCust);
// test destroy rollback on unmodified entry
mgr.begin();
custPR.destroy(custId);
orderPR.put(orderId, order);
mgr.rollback();
commitedCust = (Customer) custPR.get(custId);
Assert.assertTrue(oldCustomer.equals(commitedCust), "Expected customer to rollback to:" + oldCustomer + " but was:" + commitedCust);
// test remote destroy
boolean caughtEx = false;
try {
mgr.begin();
Customer cust = new Customer("foo", "bar");
custPR.put(custId, cust);
custPR.destroy(custId);
custPR.putIfAbsent(custId, cust);
custPR.remove(custId, cust);
custPR.destroy(new CustId(1));
custPR.destroy(new CustId(3));
custPR.destroy(new CustId(7));
mgr.commit();
} catch (Exception e) {
mgr.rollback();
if (e instanceof TransactionDataNotColocatedException) {
caughtEx = true;
} else if (e instanceof TransactionDataRebalancedException) {
caughtEx = true;
} else if (e instanceof EntryNotFoundException && e.getMessage().matches("Entry not found for key.*1")) {
caughtEx = true;
} else {
throw new TestException("Expected to catch PR remote destroy exception, but caught:" + e.getMessage(), e);
}
}
if (!caughtEx) {
throw new TestException("An Expected exception was not thrown");
}
// test destroy on unmodified entry
mgr.begin();
custPR.destroy(custId);
orderPR.put(orderId, order);
mgr.commit();
commitedCust = (Customer) custPR.get(custId);
Assert.assertTrue(commitedCust == null, "Expected Customer to be null but was:" + commitedCust);
Order commitedOrder = (Order) orderPR.get(orderId);
Assert.assertTrue(order.equals(commitedOrder), "Expected Order to be:" + order + " but was:" + commitedOrder);
// put the customer again for invalidate verification
mgr.begin();
custPR.putIfAbsent(custId, newCus);
mgr.commit();
// test destroy on new entry
// TODO: This throws EntryNotFound
OrderId newOrderId = new OrderId(5000, custId);
mgr.begin();
Order newOrder = new Order("New Order to be destroyed");
orderPR.put(newOrderId, newOrder);
orderPR.destroy(newOrderId);
mgr.commit();
Assert.assertTrue(orderPR.get(newOrderId) == null, "Did not expect orderId to be present");
// test ConcurrentMap operations
mgr.begin();
Order order1 = new Order("New Order to be replaced");
Order order2 = new Order("New Order to be destroyed");
orderPR.putIfAbsent(newOrderId, order1);
Assert.assertTrue(order1.equals(orderPR.replace(newOrderId, order2)));
// value is order2
mgr.commit();
Assert.assertTrue(order2.equals(orderPR.get(newOrderId)));
mgr.begin();
Assert.assertTrue(orderPR.replace(newOrderId, order2, order1));
// value is order1
mgr.commit();
Assert.assertTrue(orderPR.get(newOrderId).equals(order1));
mgr.begin();
// this should return false since the value is order1
Assert.assertTrue(!orderPR.remove(newOrderId, new Object()));
mgr.commit();
Assert.assertTrue(orderPR.get(newOrderId).equals(order1));
mgr.begin();
Assert.assertTrue(orderPR.remove(newOrderId, order1));
// gone now
mgr.commit();
Assert.assertTrue(orderPR.get(newOrderId) == null);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class PersistentPartitionedRegionWithTransactionDUnitTest method createData.
@Override
protected void createData(VM vm, final int startKey, final int endKey, final String value, final String regionName) {
LogWriterUtils.getLogWriter().info("creating runnable to create data for region " + regionName);
SerializableRunnable createData = new SerializableRunnable() {
public void run() {
Cache cache = getCache();
LogWriterUtils.getLogWriter().info("getting region " + regionName);
Region region = cache.getRegion(regionName);
for (int i = startKey; i < endKey; i++) {
CacheTransactionManager tx = cache.getCacheTransactionManager();
tx.begin();
region.put(i, value);
region.put(i + 113, value);
region.put(i + 113 * 2, value);
tx.commit();
}
{
// add a destroy to make sure bug 43063 is fixed
CacheTransactionManager tx = cache.getCacheTransactionManager();
tx.begin();
region.put(endKey + 113 * 3, value);
tx.commit();
tx.begin();
region.remove(endKey + 113 * 3);
tx.commit();
}
}
};
vm.invoke(createData);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class PersistentPartitionedRegionWithTransactionDUnitTest method createDataWithRollback.
private void createDataWithRollback(VM vm, final int startKey, final int endKey, final String value) {
SerializableRunnable createData = new SerializableRunnable() {
public void run() {
Cache cache = getCache();
CacheTransactionManager tx = cache.getCacheTransactionManager();
Region region = cache.getRegion(PR_REGION_NAME);
for (int i = startKey; i < endKey; i++) {
tx.begin();
region.put(i, value);
region.destroy(i + 113, value);
region.invalidate(i + 113 * 2, value);
tx.rollback();
}
}
};
vm.invoke(createData);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class DiskRegionTestingBase method closeCache.
/** Close the cache */
private static synchronized void closeCache() {
if (cache != null) {
try {
if (!cache.isClosed()) {
CacheTransactionManager txMgr = cache.getCacheTransactionManager();
if (txMgr != null) {
if (txMgr.exists()) {
// make sure we cleanup this threads txid stored in a thread local
txMgr.rollback();
}
}
cache.close();
}
} finally {
cache = null;
}
}
}
Aggregations