Search in sources :

Example 61 with SystemException

use of javax.transaction.SystemException in project tomee by apache.

the class EjbTimerServiceImpl method ejbTimeout.

/**
     * This method calls the ejbTimeout method and starts a transaction if the timeout is transacted.
     * <p/>
     * This method will retry failed ejbTimeout calls until retryAttempts is exceeded.
     *
     * @param timerData the timer to call.
     */
@SuppressWarnings("ReturnInsideFinallyBlock")
public void ejbTimeout(final TimerData timerData) {
    final Thread thread = Thread.currentThread();
    // container loader
    final ClassLoader loader = thread.getContextClassLoader();
    try {
        Timer timer = getTimer(timerData.getId());
        // quartz can be backed by some advanced config (jdbc for instance)
        if (timer == null && timerStore instanceof MemoryTimerStore && timerData.getTimer() != null) {
            try {
                timerStore.addTimerData(timerData);
                // TODO: replace memoryjobstore by the db one?
                timer = timerData.getTimer();
            } catch (final TimerStoreException e) {
            // shouldn't occur
            }
        // return;
        }
        for (int tries = 0; tries < 1 + retryAttempts; tries++) {
            boolean retry = false;
            // if transacted, begin the transaction
            if (transacted) {
                try {
                    transactionManager.begin();
                } catch (final Exception e) {
                    log.warning("Exception occured while starting container transaction", e);
                    return;
                }
            }
            // call the timeout method
            try {
                final RpcContainer container = (RpcContainer) deployment.getContainer();
                if (container == null) {
                    return;
                }
                final Method ejbTimeout = timerData.getTimeoutMethod();
                if (ejbTimeout == null) {
                    return;
                }
                // if app registered Synchronization we need it for commit()/rollback()
                // so forcing it and not relying on container for it
                thread.setContextClassLoader(deployment.getClassLoader() != null ? deployment.getClassLoader() : loader);
                SetAccessible.on(ejbTimeout);
                container.invoke(deployment.getDeploymentID(), InterfaceType.TIMEOUT, ejbTimeout.getDeclaringClass(), ejbTimeout, new Object[] { timer }, timerData.getPrimaryKey());
            } catch (final RuntimeException e) {
                retry = true;
                // exception from a timer does not necessairly mean failure
                log.warning("RuntimeException from ejbTimeout on " + deployment.getDeploymentID(), e);
                try {
                    transactionManager.setRollbackOnly();
                } catch (final SystemException e1) {
                    log.warning("Exception occured while setting RollbackOnly for container transaction", e1);
                }
            } catch (final OpenEJBException e) {
                retry = true;
                if (ApplicationException.class.isInstance(e)) {
                    // we don't want to pollute logs
                    log.debug("Exception from ejbTimeout on " + deployment.getDeploymentID(), e);
                } else {
                    log.warning("Exception from ejbTimeout on " + deployment.getDeploymentID(), e);
                }
                if (transacted) {
                    try {
                        transactionManager.setRollbackOnly();
                    } catch (final SystemException e1) {
                        log.warning("Exception occured while setting RollbackOnly for container transaction", e1);
                    }
                }
            } finally {
                try {
                    if (!transacted) {
                        if (!retry) {
                            return;
                        }
                    } else if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                        transactionManager.commit();
                        return;
                    } else {
                        // tx was marked rollback, so roll it back and retry.
                        transactionManager.rollback();
                    }
                } catch (final Exception e) {
                    log.warning("Exception occured while completing container transaction", e);
                }
            }
        }
        log.warning("Failed to execute ejbTimeout on " + timerData.getDeploymentId() + " successfully within " + retryAttempts + " attempts");
    } catch (final RuntimeException e) {
        log.warning("RuntimeException occured while calling ejbTimeout", e);
        throw e;
    } catch (final Error e) {
        log.warning("Error occured while calling ejbTimeout", e);
        throw e;
    } finally {
        thread.setContextClassLoader(loader);
        //TODO shall we do all this via Quartz listener ???
        if (timerData.getType() == TimerType.SingleAction) {
            timerStore.removeTimer(timerData.getId());
            timerData.setExpired(true);
        } else if (timerData.getType() == TimerType.Calendar && timerData.getNextTimeout() == null) {
            timerStore.removeTimer(timerData.getId());
            timerData.setExpired(true);
        } else {
            timerStore.updateIntervalTimer(timerData);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Method(java.lang.reflect.Method) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException) SchedulerException(org.apache.openejb.quartz.SchedulerException) ApplicationException(org.apache.openejb.ApplicationException) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) RpcContainer(org.apache.openejb.RpcContainer) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ApplicationException(org.apache.openejb.ApplicationException) Timer(javax.ejb.Timer) SystemException(javax.transaction.SystemException)

Example 62 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class SchemaToolTransactionHandlingTest method testDropCreateDropInExistingJtaTransaction.

// for each case we want to run these tool delegates in a matrix of:
//		1) JTA versus JDBC transaction handling
//		2) existing transaction versus not
//
// cases:
//		1) create-drop
//		2) update
//		3) validate
//
// so:
//		1) create-drop
//			1.1) JTA transaction handling
//				1.1.1) inside an existing transaction
//				1.1.2) outside any transaction
//			1.1) JDBC transaction handling
//				- there really cannot be an "existing transaction" case...
@Test
public void testDropCreateDropInExistingJtaTransaction() {
    // start a JTA transaction...
    try {
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    } catch (Exception e) {
        throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
    }
    // hold reference to Transaction object
    final Transaction jtaTransaction;
    try {
        jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
    } catch (SystemException e) {
        throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
    }
    final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
    // perform the test...
    try {
        final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
        final SchemaDropper schemaDropper = smt.getSchemaDropper(Collections.emptyMap());
        final SchemaCreator schemaCreator = smt.getSchemaCreator(Collections.emptyMap());
        final Metadata mappings = buildMappings(registry);
        try {
            try {
                schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (CommandAcceptanceException e) {
            //ignore may happen if sql drop does not support if exist
            }
            schemaCreator.doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
        } finally {
            try {
                schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (Exception ignore) {
            // ignore
            }
        }
    } finally {
        try {
            jtaTransaction.commit();
            ((StandardServiceRegistryImpl) registry).destroy();
        } catch (Exception e) {
        // not much we can do...
        }
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) SchemaCreator(org.hibernate.tool.schema.spi.SchemaCreator) Metadata(org.hibernate.boot.Metadata) SchemaDropper(org.hibernate.tool.schema.spi.SchemaDropper) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) SystemException(javax.transaction.SystemException) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 63 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class SchemaToolTransactionHandlingTest method testValidateInExistingJtaTransaction.

@Test
public void testValidateInExistingJtaTransaction() {
    // start a JTA transaction...
    try {
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    } catch (Exception e) {
        throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
    }
    // hold reference to Transaction object
    final Transaction jtaTransaction;
    try {
        jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
    } catch (SystemException e) {
        throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
    }
    final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
    // perform the test...
    try {
        final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
        final Metadata mappings = buildMappings(registry);
        // first make the schema exist...
        try {
            smt.getSchemaCreator(Collections.emptyMap()).doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
        } catch (Exception e) {
            throw new RuntimeException("Unable to create schema to validation tests", e);
        }
        try {
            smt.getSchemaValidator(Collections.emptyMap()).doValidation(mappings, ExecutionOptionsTestImpl.INSTANCE);
        } finally {
            try {
                smt.getSchemaDropper(Collections.emptyMap()).doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (Exception ignore) {
            // ignore
            }
        }
    } finally {
        try {
            jtaTransaction.commit();
            ((StandardServiceRegistryImpl) registry).destroy();
        } catch (Exception e) {
        // not much we can do...
        }
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) Metadata(org.hibernate.boot.Metadata) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) SystemException(javax.transaction.SystemException) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 64 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class XaTransactionImpl method rollback.

public void rollback() throws IllegalStateException, SystemException {
    status = Status.STATUS_ROLLING_BACK;
    runXaResourceRollback();
    status = Status.STATUS_ROLLEDBACK;
    if (connection != null) {
        try {
            connection.rollback();
            connection.close();
        } catch (SQLException sqle) {
            status = Status.STATUS_UNKNOWN;
            throw new SystemException();
        }
    }
    if (synchronizations != null) {
        for (int i = 0; i < synchronizations.size(); i++) {
            Synchronization s = (Synchronization) synchronizations.get(i);
            if (s != null)
                s.afterCompletion(status);
        }
    }
    // status = Status.STATUS_NO_TRANSACTION;
    jtaTransactionManager.endCurrent(this);
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 65 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class TxUtil method markRollbackOnly.

public static void markRollbackOnly(boolean useJta, Session s) {
    if (useJta) {
        JtaPlatform jtaPlatform = s.getSessionFactory().getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class);
        TransactionManager tm = jtaPlatform.retrieveTransactionManager();
        try {
            tm.setRollbackOnly();
        } catch (SystemException e) {
            throw new RuntimeException(e);
        }
    } else {
        s.getTransaction().markRollbackOnly();
    }
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager)

Aggregations

SystemException (javax.transaction.SystemException)102 Transaction (javax.transaction.Transaction)34 RollbackException (javax.transaction.RollbackException)29 NotSupportedException (javax.transaction.NotSupportedException)22 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)18 IOException (java.io.IOException)16 HeuristicMixedException (javax.transaction.HeuristicMixedException)16 UserTransaction (javax.transaction.UserTransaction)14 XAException (javax.transaction.xa.XAException)13 Test (org.junit.Test)12 TransactionManager (javax.transaction.TransactionManager)11 SQLException (java.sql.SQLException)10 LogWriterI18n (org.apache.geode.i18n.LogWriterI18n)10 InvalidTransactionException (javax.transaction.InvalidTransactionException)9 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)8 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 XAResource (javax.transaction.xa.XAResource)7 Synchronization (javax.transaction.Synchronization)6 File (java.io.File)5