use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class CacheXml66DUnitTest method testMultipleTXListener.
/**
* Tests multiple transaction listeners
*
* @since GemFire 5.0
*/
@Test
public void testMultipleTXListener() throws Exception {
CacheCreation cache = new CacheCreation();
CacheTransactionManagerCreation txMgrCreation = new CacheTransactionManagerCreation();
TransactionListener l1 = new MyTestTransactionListener();
TransactionListener l2 = new MySecondTestTransactionListener();
txMgrCreation.addListener(l1);
txMgrCreation.addListener(l2);
cache.addCacheTransactionManagerCreation(txMgrCreation);
testXml(cache);
{
CacheTransactionManager tm = getCache().getCacheTransactionManager();
assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l2);
assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] {}), Arrays.asList(tm.getListeners()));
tm.addListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
tm.addListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
tm.addListener(l2);
assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l2 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l2 }), Arrays.asList(tm.getListeners()));
tm.initListeners(new TransactionListener[] { l1, l2 });
assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
}
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class CallbackArgDUnitTest method doTest.
private void doTest() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(getCurrentExpectedKey(), e.getKey());
assertEquals(callbackArg, e.getCallbackArgument());
assertEquals(true, e.isCallbackArgumentAvailable());
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
r2.createSubregion("r3", af.create());
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
ArrayList keys = new ArrayList();
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
keys.add(ee.getKey());
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
CallbackArgDUnitTest.this.invokeCount = 1;
}
};
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ctm.addListener(tl1);
this.invokeCount = 0;
this.clCount = 0;
this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
doCommitOtherVm();
assertEquals(1, this.invokeCount);
assertEquals(6, this.clCount);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class ExecutionHandlerContext method executeWithTransaction.
private void executeWithTransaction(ChannelHandlerContext ctx, final Executor exec, Command command) throws Exception {
CacheTransactionManager txm = cache.getCacheTransactionManager();
TransactionId transactionId = getTransactionID();
txm.resume(transactionId);
try {
exec.executeCommand(command, this);
} catch (UnsupportedOperationInTransactionException e) {
command.setResponse(Coder.getErrorResponse(this.byteBufAllocator, RedisConstants.ERROR_UNSUPPORTED_OPERATION_IN_TRANSACTION));
} catch (TransactionException e) {
command.setResponse(Coder.getErrorResponse(this.byteBufAllocator, RedisConstants.ERROR_TRANSACTION_EXCEPTION));
} catch (Exception e) {
ByteBuf response = getExceptionResponse(ctx, e);
command.setResponse(response);
}
getTransactionQueue().add(command);
transactionId = txm.suspend();
setTransactionID(transactionId);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class RegionProvider method getOrCreateRegion0.
private Region<?, ?> getOrCreateRegion0(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context, boolean addToMeta) {
checkDataType(key, type);
Region<?, ?> r = this.regions.get(key);
if (r != null && r.isDestroyed()) {
removeKey(key, type);
r = null;
}
if (r == null) {
String stringKey = key.toString();
Lock lock = this.locks.get(stringKey);
if (lock == null) {
this.locks.putIfAbsent(stringKey, new ReentrantLock());
lock = this.locks.get(stringKey);
}
try {
lock.lock();
r = regions.get(key);
if (r == null) {
// Can create
boolean hasTransaction = context != null && context.hasTransaction();
// without context
CacheTransactionManager txm = null;
TransactionId transactionId = null;
try {
if (hasTransaction) {
txm = cache.getCacheTransactionManager();
transactionId = txm.suspend();
}
Exception concurrentCreateDestroyException = null;
do {
concurrentCreateDestroyException = null;
r = createRegionGlobally(stringKey);
try {
if (type == RedisDataType.REDIS_LIST) {
doInitializeList(key, r);
} else if (type == RedisDataType.REDIS_SORTEDSET) {
try {
doInitializeSortedSet(key, r);
} catch (RegionNotFoundException | IndexInvalidException e) {
concurrentCreateDestroyException = e;
}
}
} catch (QueryInvalidException e) {
if (e.getCause() instanceof RegionNotFoundException) {
concurrentCreateDestroyException = e;
}
}
} while (concurrentCreateDestroyException != null);
this.regions.put(key, r);
if (addToMeta) {
RedisDataType existingType = metaPutIfAbsent(key, type);
if (existingType != null && existingType != type)
throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + existingType.toString());
}
} finally {
if (hasTransaction)
txm.resume(transactionId);
}
}
} finally {
lock.unlock();
}
}
return r;
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrowWithJTA.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testAfterCommitFailedOnTransactionWriterThrowWithJTA() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("Rollback now!");
}
public void close() {
}
});
installTransactionListener();
UserTransaction userTx = (UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
userTx.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
userTx.commit();
fail("Commit should have thrown RollbackException");
} catch (RollbackException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() + " is not a SynchronizationCommitConflictException", expected.getCause() instanceof SynchronizationCommitConflictException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(1, this.afterRollbacks);
}
Aggregations