Search in sources :

Example 6 with TransactionWriter

use of org.apache.geode.cache.TransactionWriter 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 7 with TransactionWriter

use of org.apache.geode.cache.TransactionWriter 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);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionEvent(org.apache.geode.cache.TransactionEvent) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) RollbackException(javax.transaction.RollbackException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with TransactionWriter

use of org.apache.geode.cache.TransactionWriter in project geode by apache.

the class TXWriterJUnitTest method testNoCallbacksOnTransactionWriterThrow.

/**
   * make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
   * writer throw
   */
@Test
public void testNoCallbacksOnTransactionWriterThrow() 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();
    this.txMgr.begin();
    this.region.create("key1", "value1");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException cce) {
        assertNotNull(cce.getCause());
        assertTrue(cce.getCause() instanceof TransactionWriterException);
    }
    assertEquals(0, this.cbCount);
    this.cbCount = 0;
    this.region.create("key1", "value1");
    // do a sanity check to make sure callbacks are installed
    // 2 -> 1writer + 1listener
    assertEquals(2, this.cbCount);
    this.txMgr.begin();
    this.region.put("key1", "value2");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof TransactionWriterException);
    }
    assertEquals(0, this.cbCount);
    this.region.localDestroy("key1");
    this.region.create("key1", "value1");
    this.txMgr.begin();
    this.region.localDestroy("key1");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof TransactionWriterException);
    }
    assertEquals(0, this.cbCount);
    this.region.put("key1", "value1");
    this.txMgr.begin();
    this.region.destroy("key1");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof TransactionWriterException);
    }
    assertEquals(0, this.cbCount);
    this.region.put("key1", "value1");
    this.txMgr.begin();
    this.region.localInvalidate("key1");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof TransactionWriterException);
    }
    assertEquals(0, this.cbCount);
    this.region.localDestroy("key1");
    this.region.put("key1", "value1");
    this.txMgr.begin();
    this.region.invalidate("key1");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof TransactionWriterException);
    }
    assertEquals(0, this.cbCount);
    this.region.localDestroy("key1");
}
Also used : TransactionEvent(org.apache.geode.cache.TransactionEvent) CommitConflictException(org.apache.geode.cache.CommitConflictException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with TransactionWriter

use of org.apache.geode.cache.TransactionWriter in project geode by apache.

the class TXWriterJUnitTest method testAfterCommitFailedOnThrowNPE.

@Test
public void testAfterCommitFailedOnThrowNPE() throws Exception {
    installCacheListenerAndWriter();
    ((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {

        public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
            throw new NullPointerException("this is expected!");
        }

        public void close() {
        }
    });
    installTransactionListener();
    this.txMgr.begin();
    this.region.create("key1", "value1");
    this.cbCount = 0;
    try {
        this.txMgr.commit();
        fail("Commit should have thrown CommitConflictException");
    } catch (CommitConflictException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof NullPointerException);
    }
    assertEquals(0, this.cbCount);
    assertEquals(1, this.failedCommits);
    assertEquals(0, this.afterCommits);
    assertEquals(0, this.afterRollbacks);
}
Also used : TransactionEvent(org.apache.geode.cache.TransactionEvent) CommitConflictException(org.apache.geode.cache.CommitConflictException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with TransactionWriter

use of org.apache.geode.cache.TransactionWriter in project geode by apache.

the class CacheXmlParser method endTransactionWriter.

/**
   * Create a <code>transaction-writer</code> using the declarable interface and set the transaction
   * manager with the newly instantiated writer.
   */
private void endTransactionWriter() {
    Declarable d = createDeclarable();
    if (!(d instanceof TransactionWriter)) {
        throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_TRANSACTION_WRITER.toLocalizedString(d.getClass().getName()));
    }
    CacheTransactionManagerCreation txMgrCreation = (CacheTransactionManagerCreation) stack.peek();
    txMgrCreation.setWriter((TransactionWriter) d);
}
Also used : Declarable(org.apache.geode.cache.Declarable) CacheXmlException(org.apache.geode.cache.CacheXmlException) TransactionWriter(org.apache.geode.cache.TransactionWriter)

Aggregations

TransactionWriter (org.apache.geode.cache.TransactionWriter)12 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)9 Test (org.junit.Test)8 CommitConflictException (org.apache.geode.cache.CommitConflictException)7 SynchronizationCommitConflictException (org.apache.geode.cache.SynchronizationCommitConflictException)6 TransactionEvent (org.apache.geode.cache.TransactionEvent)6 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)5 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)5 TransactionDataRebalancedException (org.apache.geode.cache.TransactionDataRebalancedException)3 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)3 RollbackException (javax.transaction.RollbackException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NamingException (javax.naming.NamingException)1 UserTransaction (javax.transaction.UserTransaction)1 TXExpiryJUnitTest (org.apache.geode.TXExpiryJUnitTest)1 Cache (org.apache.geode.cache.Cache)1 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)1 CacheWriterException (org.apache.geode.cache.CacheWriterException)1 CacheXmlException (org.apache.geode.cache.CacheXmlException)1