use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class DistTXManagerImplJUnitTest method createCache.
@Override
protected void createCache() {
Properties props = new Properties();
props.put(MCAST_PORT, "0");
props.put(LOCATORS, "");
props.put(ConfigurationProperties.DISTRIBUTED_TRANSACTIONS, "true");
cache = new CacheFactory(props).create();
region = cache.createRegionFactory(RegionShortcut.REPLICATE).create("testRegion");
CacheTransactionManager txmgr = cache.getCacheTransactionManager();
assert (txmgr.isDistributed());
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class CommitFunction method execute.
public void execute(FunctionContext context) {
Cache cache = CacheFactory.getAnyInstance();
TXId txId = null;
try {
txId = (TXId) context.getArguments();
} catch (ClassCastException e) {
logger.info("CommitFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
throw e;
}
DistributedMember member = txId.getMemberId();
Boolean result = false;
final boolean isDebugEnabled = logger.isDebugEnabled();
if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
if (isDebugEnabled) {
logger.debug("CommitFunction: for transaction: {} committing locally", txId);
}
CacheTransactionManager txMgr = cache.getCacheTransactionManager();
if (txMgr.tryResume(txId)) {
if (isDebugEnabled) {
logger.debug("CommitFunction: resumed transaction: {}", txId);
}
txMgr.commit();
result = true;
}
} else {
ArrayList args = new ArrayList();
args.add(txId);
args.add(NestedTransactionFunction.COMMIT);
Execution ex = FunctionService.onMember(member).setArguments(args);
if (isDebugEnabled) {
logger.debug("CommitFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
}
try {
List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
result = (Boolean) list.get(0);
} catch (FunctionException fe) {
if (fe.getCause() instanceof FunctionInvocationTargetException) {
throw new TransactionDataNodeHasDepartedException("Could not commit on member:" + member);
} else {
throw fe;
}
}
}
if (isDebugEnabled) {
logger.debug("CommitFunction: for transaction: {} returning result: {}", txId, result);
}
context.getResultSender().lastResult(result);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class RemoteTransactionDUnitTest method testPRTXGet.
@Test
public void testPRTXGet() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
final TXId txId = (TXId) accessor.invoke(new DoOpsInTX(OP.GET));
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
assertTrue(mgr.isHostedTxInProgress(txId));
TXStateProxy tx = mgr.getHostedTXState(txId);
System.out.println("TXRS:" + tx.getRegions());
// 2 buckets for the two puts we
assertEquals(3, tx.getRegions().size());
// plus the dist. region
for (LocalRegion r : tx.getRegions()) {
assertTrue(r instanceof BucketRegion || r instanceof DistributedRegion);
TXRegionState rs = tx.readRegion(r);
for (Object key : rs.getEntryKeys()) {
TXEntryState es = rs.readEntry(key);
assertNotNull(es.getValue(key, r, false));
assertFalse(es.isDirty());
}
}
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.commit();
verifyAfterCommit(OP.GET);
return null;
}
});
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class RollbackFunction method execute.
public void execute(FunctionContext context) {
Cache cache = CacheFactory.getAnyInstance();
TXId txId = null;
try {
txId = (TXId) context.getArguments();
} catch (ClassCastException e) {
logger.info("RollbackFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
throw e;
}
DistributedMember member = txId.getMemberId();
Boolean result = false;
final boolean isDebugEnabled = logger.isDebugEnabled();
if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
if (isDebugEnabled) {
logger.debug("RollbackFunction: for transaction: {} rolling back locally", txId);
}
CacheTransactionManager txMgr = cache.getCacheTransactionManager();
if (txMgr.tryResume(txId)) {
if (isDebugEnabled) {
logger.debug("RollbackFunction: resumed transaction: {}", txId);
}
txMgr.rollback();
result = true;
}
} else {
ArrayList args = new ArrayList();
args.add(txId);
args.add(NestedTransactionFunction.ROLLBACK);
Execution ex = FunctionService.onMember(member).setArguments(args);
if (isDebugEnabled) {
logger.debug("RollbackFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
}
try {
List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
result = (Boolean) list.get(0);
} catch (FunctionException fe) {
throw new TransactionDataNodeHasDepartedException("Could not Rollback on member:" + member);
}
}
if (isDebugEnabled) {
logger.debug("RollbackFunction: for transaction: {} returning result: {}", txId, result);
}
context.getResultSender().lastResult(result);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class MyTransactionFunction method verifyTransactionRollback.
private void verifyTransactionRollback(RegionFunctionContext ctx) {
Region custPR = ctx.getDataSet();
Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.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);
Order oldOrder = (Order) orderPR.get(orderId);
mgr.begin();
custPR.put(custId, newCus);
Customer txCust = (Customer) custPR.get(custId);
orderPR.put(orderId, order);
Order txOrder = (Order) orderPR.get(orderId);
Assert.assertTrue(newCus.equals(txCust), "Expected Customer to be:" + newCus + " but was:" + txCust);
Assert.assertTrue(txOrder.equals(order), "Expected Order to be:" + order + " but was:" + txOrder);
mgr.rollback();
Customer commitedCust = (Customer) custPR.get(custId);
Assert.assertTrue(oldCustomer.equals(commitedCust), "Expected Customer to be:" + oldCustomer + " but was:" + commitedCust);
Order commitedOrder = (Order) orderPR.get(orderId);
Assert.assertTrue(oldOrder.equals(commitedOrder), "Expected Order to be:" + oldOrder + " but was:" + commitedOrder);
mgr.begin();
Assert.assertTrue(custPR.remove(custId, oldCustomer));
orderPR.replace(orderId, order);
mgr.rollback();
Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
mgr.begin();
Assert.assertTrue(custPR.replace(custId, oldCustomer, newCus));
orderPR.remove(orderId, oldOrder);
Assert.assertTrue(null == orderPR.putIfAbsent(orderId, order));
mgr.rollback();
Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
}
Aggregations