use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXManagerImpl method noteCommitSuccess.
void noteCommitSuccess(long opStart, long lifeTime, TXStateInterface tx) {
long opEnd = CachePerfStats.getStatTime();
this.cachePerfStats.txSuccess(opEnd - opStart, lifeTime, tx.getChanges());
TransactionListener[] listeners = getListeners();
if (tx.isFireCallbacks() && listeners.length > 0) {
final TXEvent e = tx.getEvent();
try {
for (final TransactionListener listener : listeners) {
try {
listener.afterCommit(e);
} catch (VirtualMachineError err) {
SystemFailure.initiateFailure(err);
// now, so don't let this thread continue.
throw err;
} catch (Throwable t) {
// Whenever you catch Error or Throwable, you must also
// catch VirtualMachineError (see above). However, there is
// _still_ a possibility that you are dealing with a cascading
// error condition, so you also need to check to see if the JVM
// is still usable:
SystemFailure.checkFailure();
logger.error(LocalizedMessage.create(LocalizedStrings.TXManagerImpl_EXCEPTION_OCCURRED_IN_TRANSACTIONLISTENER), t);
}
}
} finally {
e.release();
}
}
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXJUnitTest method testJTAEnlistment.
@Test
public void testJTAEnlistment() throws CacheException, javax.transaction.NotSupportedException, javax.transaction.RollbackException, javax.transaction.SystemException, javax.transaction.HeuristicMixedException, javax.transaction.HeuristicRollbackException {
TransactionListener tl = new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
++listenerAfterCommit;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
++listenerAfterFailedCommit;
te = event;
}
@Override
public void afterRollback(TransactionEvent event) {
++listenerAfterRollback;
te = event;
}
@Override
public void close() {
++listenerClose;
}
};
this.txMgr.addListener(tl);
javax.transaction.UserTransaction userTx = null;
try {
userTx = (javax.transaction.UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable badDog) {
fail("Expected to get a healthy UserTransaction!");
}
// Test enlistment for put
// Test enlisted rollback
// Test prevention of rollback/commit for enlisted transaction
assertEquals(0, this.listenerAfterRollback);
userTx.begin();
this.region.put("enlistKey", "enlistVal");
assertEquals("enlistVal", this.region.getEntry("enlistKey").getValue());
assertNotNull(this.txMgr.getTransactionId());
try {
this.txMgr.rollback();
fail("Should not allow a CacheTransactionManager.rollback call once the GF Tx is enlisted");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable ok) {
}
try {
this.txMgr.commit();
fail("Should not allow a CacheTransactionManager.commit() call once the GF Tx is enlisted");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable alsoOk) {
}
userTx.rollback();
assertNull(this.txMgr.getTransactionId());
assertTrue(!this.region.containsKey("enlistKey"));
assertEquals(1, this.listenerAfterRollback);
// Test enlistment for create
// Test commit
assertEquals(0, this.listenerAfterCommit);
userTx.begin();
this.region.create("enlistKey", "enlistVal");
assertEquals("enlistVal", this.region.getEntry("enlistKey").getValue());
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(this.region.containsKey("enlistKey"));
assertEquals("enlistVal", this.region.getEntry("enlistKey").getValue());
assertEquals(1, this.listenerAfterCommit);
// Test enlistment for get
assertEquals(1, this.listenerAfterCommit);
userTx.begin();
assertEquals("enlistVal", this.region.get("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertEquals(2, this.listenerAfterCommit);
// Test enlistment for invalidate
assertEquals(2, this.listenerAfterCommit);
userTx.begin();
this.region.invalidate("enlistKey");
assertTrue(this.region.containsKey("enlistKey"));
assertTrue(!this.region.containsValueForKey("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(this.region.containsKey("enlistKey"));
assertTrue(!this.region.containsValueForKey("enlistKey"));
assertEquals(3, this.listenerAfterCommit);
// Test enlistment for destroy
assertEquals(3, this.listenerAfterCommit);
userTx.begin();
this.region.destroy("enlistKey");
assertTrue(!this.region.containsKey("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(!this.region.containsKey("enlistKey"));
assertEquals(4, this.listenerAfterCommit);
// Test enlistment for load
AttributesMutator<String, String> mutator = this.region.getAttributesMutator();
mutator.setCacheLoader(new CacheLoader<String, String>() {
int count = 0;
@Override
public String load(LoaderHelper helper) throws CacheLoaderException {
return String.valueOf(count++);
}
@Override
public void close() {
}
});
assertEquals(4, this.listenerAfterCommit);
userTx.begin();
assertEquals("0", this.region.get("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(this.region.containsKey("enlistKey"));
assertEquals("0", this.region.getEntry("enlistKey").getValue());
assertEquals(5, this.listenerAfterCommit);
mutator.setCacheLoader(null);
// Test enlisted failed commit
assertEquals(0, this.listenerAfterFailedCommit);
userTx.begin();
this.region.put("enlistKey", "enlistVal");
assertEquals("enlistVal", this.region.get("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
TXStateProxy gfTx = gfTxMgrImpl.internalSuspend();
javax.transaction.TransactionManager jtaTxMgr = this.cache.getJTATransactionManager();
javax.transaction.Transaction jtaTx = jtaTxMgr.suspend();
this.region.put("enlistKey", "conflictVal");
assertEquals("conflictVal", this.region.get("enlistKey"));
try {
jtaTxMgr.resume(jtaTx);
} catch (Exception failure) {
fail("JTA resume failed");
}
gfTxMgrImpl.internalResume(gfTx);
}
assertEquals("enlistVal", this.region.get("enlistKey"));
try {
userTx.commit();
fail("Expected JTA commit exception!");
} catch (javax.transaction.HeuristicRollbackException expected) {
} catch (javax.transaction.RollbackException alsoExpected) {
} catch (Exception yuk) {
fail("Did not expect this exception from JTA commit: " + yuk);
}
assertNull(this.txMgr.getTransactionId());
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
assertEquals(1, this.listenerAfterFailedCommit);
// Test rollbackOnly UserTransaction enlistment
userTx.begin();
assertNull(this.txMgr.getTransactionId());
userTx.setRollbackOnly();
assertEquals(javax.transaction.Status.STATUS_MARKED_ROLLBACK, userTx.getStatus());
try {
this.region.put("enlistKey", "enlistVal2");
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertNull(this.txMgr.getTransactionId());
try {
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertTrue(!this.region.containsKey("enlistKey2"));
try {
this.region.put("enlistKey2", "enlistVal3");
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertNull(this.txMgr.getTransactionId());
try {
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertTrue(!this.region.containsKey("enlistKey2"));
userTx.rollback();
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
assertTrue(!this.region.containsKey("enlistKey2"));
this.txMgr.removeListener(tl);
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXOrderDUnitTest method testFarSideOrder.
/**
* make sure listeners get invoked in correct order on far side of tx
*/
@Test
public void testFarSideOrder() 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());
}
};
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(null, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
assertEquals(TXOrderDUnitTest.this.expectedKeys, keys);
TXOrderDUnitTest.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);
}
Aggregations