Search in sources :

Example 11 with TransactionEvent

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

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

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

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

the class TXJUnitTest method testListener.

@Test
public void testListener() {
    assertTrue(this.txMgr.getListener() == null);
    TransactionListener oldListener = this.txMgr.setListener(new TransactionListener() {

        @Override
        public void afterCommit(TransactionEvent event) {
            listenerAfterCommit = 1;
            te = event;
        }

        @Override
        public void afterFailedCommit(TransactionEvent event) {
            listenerAfterFailedCommit = 1;
            te = event;
        }

        @Override
        public void afterRollback(TransactionEvent event) {
            listenerAfterRollback = 1;
            te = event;
        }

        @Override
        public void close() {
            listenerClose = 1;
        }
    });
    assertTrue(oldListener == null);
    this.txMgr.begin();
    TransactionId myTxId = this.txMgr.getTransactionId();
    assertEquals(0, this.listenerAfterRollback);
    this.txMgr.rollback();
    assertEquals(1, this.listenerAfterRollback);
    assertEquals(0, this.te.getCreateEvents().size());
    assertEquals(0, this.te.getPutEvents().size());
    assertEquals(0, this.te.getInvalidateEvents().size());
    assertEquals(0, this.te.getDestroyEvents().size());
    assertEquals(0, this.te.getEvents().size());
    assertEquals(myTxId, this.te.getTransactionId());
    this.txMgr.begin();
    myTxId = this.txMgr.getTransactionId();
    try {
        assertEquals(0, this.listenerAfterCommit);
        this.txMgr.commit();
    } catch (CommitConflictException unexpected) {
        fail("did not expect " + unexpected);
    }
    assertEquals(1, this.listenerAfterCommit);
    assertEquals(0, this.te.getCreateEvents().size());
    assertEquals(0, this.te.getPutEvents().size());
    assertEquals(0, this.te.getInvalidateEvents().size());
    assertEquals(0, this.te.getDestroyEvents().size());
    assertEquals(0, this.te.getEvents().size());
    assertEquals(myTxId, this.te.getTransactionId());
    assertEquals(0, this.listenerClose);
    oldListener = this.txMgr.setListener(new TransactionListener() {

        @Override
        public void afterCommit(TransactionEvent event) {
            listenerAfterCommit = 2;
            te = event;
        }

        @Override
        public void afterFailedCommit(TransactionEvent event) {
            listenerAfterFailedCommit = 2;
        }

        @Override
        public void afterRollback(TransactionEvent event) {
            listenerAfterRollback = 2;
            te = event;
        }

        @Override
        public void close() {
            listenerClose = 2;
        }
    });
    assertEquals(1, this.listenerClose);
    this.txMgr.begin();
    assertEquals(1, this.listenerAfterRollback);
    this.txMgr.rollback();
    assertEquals(2, this.listenerAfterRollback);
    this.txMgr.begin();
    this.txMgr.setListener(oldListener);
    assertEquals(2, this.listenerClose);
    this.txMgr.rollback();
    assertEquals(1, this.listenerAfterRollback);
    closeCache();
    assertEquals(1, this.listenerClose);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionEvent(org.apache.geode.cache.TransactionEvent) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionId(org.apache.geode.cache.TransactionId) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 15 with TransactionEvent

use of org.apache.geode.cache.TransactionEvent 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);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) 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) LoaderHelper(org.apache.geode.cache.LoaderHelper) TransactionEvent(org.apache.geode.cache.TransactionEvent) FailedSynchronizationException(org.apache.geode.cache.FailedSynchronizationException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

TransactionEvent (org.apache.geode.cache.TransactionEvent)16 Test (org.junit.Test)14 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)12 TransactionListener (org.apache.geode.cache.TransactionListener)10 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)9 CommitConflictException (org.apache.geode.cache.CommitConflictException)7 EntryEvent (org.apache.geode.cache.EntryEvent)6 TransactionWriter (org.apache.geode.cache.TransactionWriter)6 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)6 ArrayList (java.util.ArrayList)5 AttributesFactory (org.apache.geode.cache.AttributesFactory)4 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)4 Region (org.apache.geode.cache.Region)4 SynchronizationCommitConflictException (org.apache.geode.cache.SynchronizationCommitConflictException)4 TransactionId (org.apache.geode.cache.TransactionId)4 TransactionListenerAdapter (org.apache.geode.cache.util.TransactionListenerAdapter)4 Iterator (java.util.Iterator)3 List (java.util.List)3 CacheException (org.apache.geode.cache.CacheException)3 CacheListener (org.apache.geode.cache.CacheListener)3