Search in sources :

Example 1 with TransactionListener

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();
        }
    });
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region)

Example 2 with TransactionListener

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);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionWriter(org.apache.geode.cache.TransactionWriter) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 3 with TransactionListener

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;
    }
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener)

Example 4 with TransactionListener

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;
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener)

Example 5 with TransactionListener

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);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) Synchronization(javax.transaction.Synchronization) TimeoutException(org.apache.geode.cache.TimeoutException) EntryExistsException(org.apache.geode.cache.EntryExistsException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) CacheWriterException(org.apache.geode.cache.CacheWriterException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) FailedSynchronizationException(org.apache.geode.cache.FailedSynchronizationException) NoSuchElementException(java.util.NoSuchElementException) CacheException(org.apache.geode.cache.CacheException) CommitConflictException(org.apache.geode.cache.CommitConflictException) QueryException(org.apache.geode.cache.query.QueryException) TransactionEvent(org.apache.geode.cache.TransactionEvent) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

TransactionListener (org.apache.geode.cache.TransactionListener)18 TransactionEvent (org.apache.geode.cache.TransactionEvent)10 Test (org.junit.Test)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)7 EntryEvent (org.apache.geode.cache.EntryEvent)6 ArrayList (java.util.ArrayList)5 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)5 Iterator (java.util.Iterator)4 AttributesFactory (org.apache.geode.cache.AttributesFactory)4 Region (org.apache.geode.cache.Region)4 TransactionId (org.apache.geode.cache.TransactionId)4 TransactionListenerAdapter (org.apache.geode.cache.util.TransactionListenerAdapter)4 List (java.util.List)3 CacheException (org.apache.geode.cache.CacheException)3 CacheListener (org.apache.geode.cache.CacheListener)3 CommitConflictException (org.apache.geode.cache.CommitConflictException)3 EntryExistsException (org.apache.geode.cache.EntryExistsException)3 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)3 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)3 NoSuchElementException (java.util.NoSuchElementException)2