Search in sources :

Example 16 with NotSupportedException

use of javax.transaction.NotSupportedException in project narayana by jbosstm.

the class EnlistDelistEnlistImpl01 method begin_begin.

public void begin_begin() throws InvocationException {
    boolean correct = true;
    try {
        XAConnection xaConnection = _xaDataSource.getXAConnection(_databaseUser, _databasePassword);
        XAResource xaResource = xaConnection.getXAResource();
        javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
        tm.begin();
        Transaction transaction = tm.getTransaction();
        correct = correct && transaction.enlistResource(xaResource);
        if (correct) {
            try {
                tm.begin();
                correct = false;
            } catch (NotSupportedException notSupportedException) {
            // correct behaviour for nested XA transaction
            }
        }
        tm.rollback();
    } catch (Exception e) {
        e.printStackTrace();
        correct = false;
    }
    _isCorrect = _isCorrect && correct;
    return;
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) NotSupportedException(javax.transaction.NotSupportedException) NotSupportedException(javax.transaction.NotSupportedException) XAConnection(javax.sql.XAConnection)

Example 17 with NotSupportedException

use of javax.transaction.NotSupportedException in project tests by datanucleus.

the class GeneralTest method testEmptyTxTimeout.

/**
 * Test that pm.currentTransaction.isActive() is correct when marked for rollback before
 * JTATransactionImpl had a chance to join
 */
public void testEmptyTxTimeout() throws NamingException, SystemException, NotSupportedException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            UserTransaction ut = getUserTransaction();
            ut.setTransactionTimeout(1);
            ut.begin();
            synchronized (this) {
                // provoke timeout
                try {
                    wait(1200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            // make sure we were marked for rollback (JOTM + JBoss 4.0.3SP1), or rolled back already (JBoss 4.2.3)
            assertTrue("Expected UserTransaction.getStatus() to be either STATUS_MARKED_ROLLBACK or STATUS_ROLLEDBACK, but was " + ut.getStatus(), ut.getStatus() == Status.STATUS_MARKED_ROLLBACK || ut.getStatus() == Status.STATUS_ROLLEDBACK);
            if (// JOTM + JBoss 4.0.3SP1
            ut.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                // by definition should be active even if marked for rollback
                assertTrue(pm.currentTransaction().isActive());
                ut.rollback();
            } else if (// JBoss 4.2.3
            ut.getStatus() == Status.STATUS_ROLLEDBACK) {
                // by definition should be active (anything but NO_TRANSACTION means active)
                assertTrue(pm.currentTransaction().isActive());
                // workaround for JBoss 4.2.3 bug https://jira.jboss.org/jira/browse/JBAS-6663
                if (cargoContainerId.equals("jboss42x")) {
                    try {
                        ut.rollback();
                    } catch (IllegalStateException e) {
                        // expected, make sure transaction has been disassociated from thread
                        assertTrue("Workaround for https://jira.jboss.org/jira/browse/JBAS-6663 has failed?!", ut.getStatus() == Status.STATUS_NO_TRANSACTION);
                    }
                } else // whatever appserver this may be
                {
                    ut.rollback();
                }
            }
            assertFalse(pm.currentTransaction().isActive());
            // reset timeout to default
            ut.setTransactionTimeout(0);
            // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
            ut.begin();
            LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
            pm.makePersistent(acct2);
            ut.commit();
            assertFalse(pm.currentTransaction().isActive());
        } finally {
            try {
                pm.close();
            } catch (Exception e) {
                // eat exception so test will fail with the underlying exception
                TestHelper.LOG.error("pm.close() failed", e);
            }
        }
    } finally {
        try {
            clean(LoginAccount.class);
        } catch (Exception e) {
            // eat exception so test will fail with the underlying exception
            TestHelper.LOG.error("clean(LoginAccount.class) failed", e);
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 18 with NotSupportedException

use of javax.transaction.NotSupportedException in project tests by datanucleus.

the class GeneralTest method testExceptionDuringBeforeCompletion.

/**
 * Verify that any exceptions thrown during JTATransactionImpl.beforeCompletion() are propagated properly, either
 * caused by flushing, or by invoking user code in a user-provided Synchronization.beforeCompletion() callback.
 */
public void testExceptionDuringBeforeCompletion() throws NotSupportedException, SystemException, NamingException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    pm = pmf.getPersistenceManager();
    new PersistenceManagerDisposer(pm);
    final String msg = "This was expected.";
    pm.currentTransaction().setSynchronization(new Synchronization() {

        public void beforeCompletion() {
            // throw the exception that we want to see during ut.commit()
            throw new RuntimeException(msg);
        }

        public void afterCompletion(int arg0) {
        }
    });
    boolean caughtExpectedException = false;
    UserTransaction ut = getUserTransaction();
    ut.begin();
    try {
        // access the currentTransaction so it joins UserTransaction
        pm.currentTransaction().isActive();
        ut.commit();
    } catch (Exception e) {
        if (e instanceof RollbackException) {
            // JBoss 4.2.3 and JOTM 2.1.4 throw a RollbackException that has no clue about our original exception
            caughtExpectedException = true;
        } else if (cargoContainerId != null && cargoContainerId.equals("jboss4x")) {
            if (!e.getMessage().contains(msg)) {
                e.printStackTrace();
                throw new RuntimeException("With jboss4.0.3 we expect the exception caught to contain the message of our thrown exception", e);
            } else {
                caughtExpectedException = true;
            }
        }
    }
    assertTrue("Exception thrown during beforeCompletion() wasn't propagated properly", caughtExpectedException);
}
Also used : PersistenceManagerDisposer(org.datanucleus.tests.jta.util.PersistenceManagerDisposer) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 19 with NotSupportedException

use of javax.transaction.NotSupportedException in project tests by datanucleus.

the class GeneralTest method testFailedCommit.

/**
 * Test expected behaviour upon failed commit with JTA
 * (copied from test.jdo.general/org.datanucleus.tests.TransactionTest.java)
 * @throws NamingException
 * @throws SystemException
 * @throws NotSupportedException
 * @throws HeuristicRollbackException
 * @throws HeuristicMixedException
 * @throws RollbackException
 * @throws IllegalStateException
 * @throws SecurityException
 */
public void testFailedCommit() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        UserTransaction ut = getUserTransaction();
        LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
        Login login = acct.getLogin();
        try {
            ut.begin();
            pm.makePersistent(acct);
            ut.commit();
            // provoke FK violation
            ut.begin();
            pm.deletePersistent(login);
            boolean exceptionCaught = false;
            try {
                ut.commit();
                assertTrue("Should have caught exception during commit due to FK violation", false);
            }// e.g. JBoss wraps the NucleusDataStoreException
             catch (Exception e) {
                // Expected
                if (isRollbackDueToDatastoreException(e)) {
                    exceptionCaught = true;
                }
            }
            assertTrue("No exception was thrown during commit so couldnt test autoRollback", exceptionCaught);
            // receiving a RollbackException in commmit() by definition means the UT was rolled back
            if (ut.getStatus() == Status.STATUS_NO_TRANSACTION && pm.currentTransaction().isActive() && ut.getClass().getName().startsWith("org.objectweb.jotm")) {
                // see http://www.jpox.org/servlet/jira/browse/NUCCORE-224
                fail("JOTM bug: when an exception is thrown during Synchronization.beforeCompletion(), the UserTransaction's status is " + "STATUS_NO_TRANSCTION, but there was no callback to Synchronization.afterCompletion()");
            }
            assertFalse(pm.currentTransaction().isActive());
            // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
            ut.begin();
            LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
            pm.makePersistent(acct2);
            ut.commit();
            assertFalse(pm.currentTransaction().isActive());
        } finally {
            try {
                if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) {
                    ut.rollback();
                }
                pm.close();
            } catch (Exception e) {
                // eat exception so test will fail with the underlying exception
                TestHelper.LOG.error("failure during finally block", e);
            }
        }
    } finally {
        try {
            clean(LoginAccount.class);
        } catch (Exception e) {
            // eat exception so test will fail with the underlying exception
            TestHelper.LOG.error("clean(LoginAccount.class) failed", e);
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UserTransaction(javax.transaction.UserTransaction) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 20 with NotSupportedException

use of javax.transaction.NotSupportedException in project cals-api by ca-cwds.

the class RFA1aFormService method submitApplication.

/**
 * There is using XA Transaction
 */
private void submitApplication(Long formId, RFAApplicationStatus newStatus) throws NotSupportedException, SystemException, DroolsException {
    RFA1aFormDTO expandedFormDTO = getExpandedFormDTO(formId);
    performSubmissionValidation(expandedFormDTO);
    // Start transaction here
    UserTransaction userTransaction = new UserTransactionImp();
    userTransaction.setTransactionTimeout(3600);
    userTransaction.begin();
    PlacementHome storedPlacementHome = null;
    try {
        storedPlacementHome = storePlaceMentHome(expandedFormDTO);
        updateFormAfterPlacementHomeCreation(formId, storedPlacementHome.getIdentifier(), newStatus);
        userTransaction.commit();
    } catch (BusinessValidationException e) {
        userTransaction.rollback();
        LOG.error("Can not create Placement Home because of BusinessValidationException", e);
        throw e;
    } catch (UnauthorizedException e) {
        userTransaction.rollback();
        LOG.error("Can not create Placement Home because of UnauthorizedException", e);
        throw e;
    } catch (Exception e) {
        try {
            userTransaction.rollback();
        } catch (Exception re) {
            LOG.warn(re.getMessage(), re);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(e.getMessage());
        sb.append('\n');
        Throwable cause = e.getCause();
        while (cause != null) {
            sb.append(" Cause: ");
            sb.append(cause.getMessage());
            sb.append('\n');
            cause = cause.getCause();
        }
        LOG.error("Can not create Placement Home: \n", e);
        throw new SystemException(sb.toString());
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) SystemException(javax.transaction.SystemException) PlacementHome(gov.ca.cwds.data.legacy.cms.entity.PlacementHome) RFA1aFormDTO(gov.ca.cwds.cals.service.dto.rfa.RFA1aFormDTO) BusinessValidationException(gov.ca.cwds.rest.exception.BusinessValidationException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) UserTransactionImp(com.atomikos.icatch.jta.UserTransactionImp) DataAccessServicesException(gov.ca.cwds.cms.data.access.service.DataAccessServicesException) ExpectedException(gov.ca.cwds.rest.exception.ExpectedException) NotSupportedException(javax.transaction.NotSupportedException) DroolsException(gov.ca.cwds.drools.DroolsException) ConstraintViolationException(javax.validation.ConstraintViolationException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) SystemException(javax.transaction.SystemException) BusinessValidationException(gov.ca.cwds.rest.exception.BusinessValidationException)

Aggregations

NotSupportedException (javax.transaction.NotSupportedException)53 SystemException (javax.transaction.SystemException)42 RollbackException (javax.transaction.RollbackException)22 HeuristicMixedException (javax.transaction.HeuristicMixedException)20 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)20 Transaction (javax.transaction.Transaction)17 UserTransaction (javax.transaction.UserTransaction)11 TransactionManager (javax.transaction.TransactionManager)10 SQLException (java.sql.SQLException)8 NamingException (javax.naming.NamingException)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 InvalidTransactionException (javax.transaction.InvalidTransactionException)5 Connection (java.sql.Connection)4 GeneralException (org.apache.openjpa.util.GeneralException)4 File (java.io.File)3 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2