use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class CallbackArgDUnitTest method doCommitOtherVm.
private void doCommitOtherVm() {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
AttributesFactory af = new AttributesFactory();
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(callbackArg, e.getCallbackArgument());
}
};
af.addCacheListener(cl1);
af.setScope(Scope.DISTRIBUTED_ACK);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
Region r3 = r2.createSubregion("r3", af.create());
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
}
};
ctm.addListener(tl1);
ctm.begin();
r2.put("b", "value1", callbackArg);
r3.put("c", "value2", callbackArg);
r1.put("a", "value3", callbackArg);
r1.put("a2", "value4", callbackArg);
r3.put("c2", "value5", callbackArg);
r2.put("b2", "value6", callbackArg);
ctm.commit();
}
});
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class CacheTransactionManagerCreationTest method shouldBeMockable.
@Test
public void shouldBeMockable() throws Exception {
CacheTransactionManagerCreation mockCacheTransactionManagerCreation = mock(CacheTransactionManagerCreation.class);
TransactionListener mockTransactionListener = mock(TransactionListener.class);
TransactionWriter mockTransactionWriter = mock(TransactionWriter.class);
when(mockCacheTransactionManagerCreation.getListener()).thenReturn(mockTransactionListener);
mockCacheTransactionManagerCreation.setWriter(mockTransactionWriter);
verify(mockCacheTransactionManagerCreation, times(1)).setWriter(mockTransactionWriter);
assertThat(mockCacheTransactionManagerCreation.getListener()).isSameAs(mockTransactionListener);
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXManagerImpl method setListener.
public TransactionListener setListener(TransactionListener newListener) {
synchronized (this.txListeners) {
TransactionListener result = getListener();
this.txListeners.clear();
if (newListener != null) {
this.txListeners.add(newListener);
}
if (result != null) {
closeListener(result);
}
return result;
}
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class CacheTransactionManagerCreation method setListener.
public TransactionListener setListener(TransactionListener newListener) {
TransactionListener result = getListener();
this.txListeners.clear();
if (newListener != null) {
this.txListeners.add(newListener);
}
return result;
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXJUnitTest method testJTASynchronization.
@Test
public void testJTASynchronization() throws CacheException, javax.transaction.NotSupportedException, javax.transaction.RollbackException, javax.transaction.SystemException, javax.transaction.HeuristicMixedException, javax.transaction.HeuristicRollbackException {
javax.transaction.TransactionManager jtaTxMgr = this.cache.getJTATransactionManager();
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);
Synchronization gfTXSync;
// Test successful JTA commit
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
}
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(0, this.listenerAfterCommit);
this.cache.getLogger().info("SWAP:doingCreate");
this.region.create("syncKey1", "syncVal1");
jtaTxMgr.commit();
assertEquals(1, this.listenerAfterCommit);
assertEquals("syncVal1", this.region.getEntry("syncKey1").getValue());
try {
this.txMgr.commit();
fail("JTA Cache Manager should have called commit!");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable expected) {
}
// Test JTA rollback
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
}
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(0, this.listenerAfterRollback);
this.region.put("syncKey2", "syncVal2");
jtaTxMgr.rollback();
assertEquals(1, this.listenerAfterRollback);
assertTrue(!this.region.containsKey("syncKey2"));
// Test failed JTA commit with suspend
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(0, this.listenerAfterFailedCommit);
this.region.put("syncKey3", "syncVal3");
assertEquals("syncVal3", this.region.getEntry("syncKey3").getValue());
TXStateProxy gfTx = gfTxMgrImpl.internalSuspend();
javax.transaction.Transaction jtaTx = jtaTxMgr.suspend();
assertNull(jtaTxMgr.getTransaction());
this.region.put("syncKey3", "syncVal4");
assertEquals("syncVal4", this.region.getEntry("syncKey3").getValue());
gfTxMgrImpl.internalResume(gfTx);
try {
jtaTxMgr.resume(jtaTx);
} catch (Exception failure) {
fail("JTA resume failed");
}
assertNotNull(jtaTxMgr.getTransaction());
}
assertEquals("syncVal3", this.region.getEntry("syncKey3").getValue());
try {
jtaTxMgr.commit();
fail("Expected JTA manager conflict exception!");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable expected) {
}
assertEquals(1, this.listenerAfterFailedCommit);
assertEquals("syncVal4", this.region.getEntry("syncKey3").getValue());
// Test failed JTA commit with a new thread
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(1, this.listenerAfterFailedCommit);
this.region.put("syncKey4", "syncVal3");
assertEquals("syncVal3", this.region.getEntry("syncKey4").getValue());
// Create a new thread and have it update the same key, causing
// a conflict
final int[] signal = { 0 };
Thread t = new Thread("non-TX conflict generator") {
@Override
public void run() {
try {
region.put("syncKey4", "syncVal4");
while (true) synchronized (signal) {
signal[0] = 1;
signal.notify();
signal.wait();
if (signal[0] == 0) {
break;
}
}
} catch (Exception error) {
fail("Non-tx thread failure due to: " + error);
}
}
};
t.start();
try {
while (true) synchronized (signal) {
if (signal[0] == 1) {
signal[0] = 0;
signal.notify();
break;
} else {
signal.wait();
}
}
} catch (InterruptedException dangit) {
fail("Tx thread waiting for non-tx thread failed due to : " + dangit);
}
assertEquals("syncVal3", this.region.getEntry("syncKey4").getValue());
}
try {
jtaTxMgr.commit();
fail("Expected JTA manager conflict exception!");
} catch (javax.transaction.HeuristicRollbackException expected) {
} catch (javax.transaction.RollbackException alsoExpected) {
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable yuk) {
fail("Did not expect this throwable from JTA commit: " + yuk);
}
assertEquals(2, this.listenerAfterFailedCommit);
assertEquals("syncVal4", this.region.getEntry("syncKey4").getValue());
this.txMgr.removeListener(tl);
}
Aggregations