Search in sources :

Example 1 with TransactionWriterException

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

the class DistTXState method precommit.

/*
   * (non-Javadoc)
   * 
   * @see org.apache.geode.internal.cache.TXStateInterface#commit()
   * 
   * Take Locks Does conflict check on primary ([DISTTX] TODO on primary only) Invoke TxWriter
   */
@Override
public void precommit() throws CommitConflictException, UnsupportedOperationInTransactionException {
    if (logger.isDebugEnabled()) {
        logger.debug("DistTXState.precommit transaction {} is closed {} ", getTransactionId(), this.closed, new Throwable());
    }
    if (this.closed) {
        return;
    }
    synchronized (this.completionGuard) {
        this.completionStarted = true;
    }
    if (onBehalfOfRemoteStub && !proxy.isCommitOnBehalfOfRemoteStub()) {
        throw new UnsupportedOperationInTransactionException(LocalizedStrings.TXState_CANNOT_COMMIT_REMOTED_TRANSACTION.toLocalizedString());
    }
    cleanupNonDirtyRegions();
    /*
     * Lock buckets so they can't be rebalanced then perform the conflict check to fix #43489
     */
    try {
        lockBucketRegions();
    } catch (PrimaryBucketException pbe) {
        // not sure what to do here yet
        RuntimeException re = new TransactionDataRebalancedException(LocalizedStrings.PartitionedRegion_TRANSACTIONAL_DATA_MOVED_DUE_TO_REBALANCING.toLocalizedString());
        re.initCause(pbe);
        throw re;
    }
    if (this.locks == null) {
        reserveAndCheck();
    }
    // For internal testing
    if (this.internalAfterConflictCheck != null) {
        this.internalAfterConflictCheck.run();
    }
    updateRegionVersions();
    generateTailKeysForParallelDispatcherEvents();
    /*
     * 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 (!firedWriter && writer != null) {
        try {
            firedWriter = true;
            writer.beforeCommit(getEvent());
        } 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);
        }
    }
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException)

Example 2 with TransactionWriterException

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

the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrow.

/**
   * make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
   * writer throw
   */
@Test
public void testAfterCommitFailedOnTransactionWriterThrow() 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 expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() instanceof TransactionWriterException);
    }
    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 3 with TransactionWriterException

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

the class TXWriterOOMEJUnitTest method testAfterCommitFailedOnThrowOOM.

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

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

        public void close() {
        }
    });
    installTransactionListener();
    try {
        SystemFailureTestHook.setExpectedFailureClass(OutOfMemoryError.class);
        this.txMgr.begin();
        this.region.create("key1", "value1");
        this.cbCount = 0;
        try {
            this.txMgr.commit();
            fail("Commit should have thrown OOME");
        } catch (OutOfMemoryError expected) {
        // this is what we expect
        }
        // no callbacks were invoked
        assertEquals(0, this.cbCount);
        assertEquals(0, this.failedCommits);
        assertEquals(0, this.afterCommits);
        assertEquals(0, this.afterRollbacks);
    } finally {
        SystemFailureTestHook.setExpectedFailureClass(null);
    }
}
Also used : TransactionEvent(org.apache.geode.cache.TransactionEvent) 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 4 with TransactionWriterException

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

the class RemoteTransactionDUnitTest method testRemoteExceptionThrown.

@Test
public void testRemoteExceptionThrown() {
    Host host = Host.getHost(0);
    VM acc = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(acc, datastore, 0);
    VM accessor = getVMForTransactions(acc, datastore);
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            getGemfireCache().getTxManager().setWriter(new TransactionWriter() {

                public void close() {
                }

                public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
                    throw new TransactionWriterException("AssertionError");
                }
            });
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            getGemfireCache().getTxManager().begin();
            Region r = getCache().getRegion(CUSTOMER);
            r.put(new CustId(8), new Customer("name8", "address8"));
            try {
                getGemfireCache().getTxManager().commit();
                fail("Expected exception not thrown");
            } catch (Exception e) {
                assertEquals("AssertionError", e.getCause().getMessage());
            }
            return null;
        }
    });
}
Also used : TransactionEvent(org.apache.geode.cache.TransactionEvent) TransactionWriter(org.apache.geode.cache.TransactionWriter) CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 5 with TransactionWriterException

use of org.apache.geode.cache.TransactionWriterException 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)

Aggregations

TransactionWriter (org.apache.geode.cache.TransactionWriter)9 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)9 CommitConflictException (org.apache.geode.cache.CommitConflictException)7 SynchronizationCommitConflictException (org.apache.geode.cache.SynchronizationCommitConflictException)6 TransactionEvent (org.apache.geode.cache.TransactionEvent)6 Test (org.junit.Test)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 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)1 CacheWriterException (org.apache.geode.cache.CacheWriterException)1 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)1 Region (org.apache.geode.cache.Region)1