Search in sources :

Example 1 with SynchronizationCommitConflictException

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

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

the class TXState method beforeCompletion.

//////////////////////////////////////////////////////////////////
// JTA Synchronization implementation //
//////////////////////////////////////////////////////////////////
/*
   * (non-Javadoc)
   * 
   * @see org.apache.geode.internal.cache.TXStateInterface#beforeCompletion()
   */
public void beforeCompletion() throws SynchronizationCommitConflictException {
    if (this.closed) {
        throw new TXManagerCancelledException();
    }
    this.proxy.getTxMgr().setTXState(null);
    final long opStart = CachePerfStats.getStatTime();
    this.jtaLifeTime = opStart - getBeginTime();
    try {
        reserveAndCheck();
        /*
       * If there is a TransactionWriter plugged in, we need to to give it an opportunity to abort
       * the transaction.
       */
        TransactionWriter writer = this.proxy.getTxMgr().getWriter();
        if (writer != null) {
            try {
                // need to mark this so we don't fire again in commit
                firedWriter = true;
                TXEvent event = getEvent();
                if (!event.hasOnlyInternalEvents()) {
                    writer.beforeCommit(event);
                }
            } catch (TransactionWriterException twe) {
                cleanup();
                throw new CommitConflictException(twe);
            } catch (VirtualMachineError err) {
                // cleanup(); this allocates objects so I don't think we can do it - that leaves the TX
                // open, but we are poison pilling so we should be ok??
                SystemFailure.initiateFailure(err);
                // now, so don't let this thread continue.
                throw err;
            } catch (Throwable t) {
                // rollback the transaction!
                cleanup();
                // 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();
                throw new CommitConflictException(t);
            }
        }
    } catch (CommitConflictException commitConflict) {
        this.proxy.getTxMgr().noteCommitFailure(opStart, this.jtaLifeTime, this);
        throw new SynchronizationCommitConflictException(LocalizedStrings.TXState_CONFLICT_DETECTED_IN_GEMFIRE_TRANSACTION_0.toLocalizedString(getTransactionId()), commitConflict);
    }
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) TXManagerCancelledException(org.apache.geode.distributed.TXManagerCancelledException) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException)

Aggregations

SynchronizationCommitConflictException (org.apache.geode.cache.SynchronizationCommitConflictException)2 TransactionWriter (org.apache.geode.cache.TransactionWriter)2 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)2 RollbackException (javax.transaction.RollbackException)1 UserTransaction (javax.transaction.UserTransaction)1 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)1 CommitConflictException (org.apache.geode.cache.CommitConflictException)1 TransactionEvent (org.apache.geode.cache.TransactionEvent)1 TXManagerCancelledException (org.apache.geode.distributed.TXManagerCancelledException)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1 Test (org.junit.Test)1