Search in sources :

Example 1 with ContextTransactionManager

use of org.wildfly.transaction.client.ContextTransactionManager in project wildfly by wildfly.

the class LifecycleCMTTxInterceptor method beginTransaction.

protected Transaction beginTransaction(final TransactionManager tm) throws NotSupportedException, SystemException {
    if (tm instanceof ContextTransactionManager) {
        final ContextTransactionManager contextTransactionManager = (ContextTransactionManager) tm;
        int timeout = contextTransactionManager.getTransactionTimeout();
        final LocalTransaction transaction = LocalTransactionContext.getCurrent().beginTransaction(timeout, false);
        try {
            contextTransactionManager.resume(transaction);
        } catch (InvalidTransactionException e) {
            // should not be possible
            throw new IllegalStateException(e);
        }
        return transaction;
    } else {
        return super.beginTransaction(tm);
    }
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) LocalTransaction(org.wildfly.transaction.client.LocalTransaction) InvalidTransactionException(javax.transaction.InvalidTransactionException)

Example 2 with ContextTransactionManager

use of org.wildfly.transaction.client.ContextTransactionManager in project wildfly by wildfly.

the class CMTTxInterceptor method invokeInOurTx.

protected Object invokeInOurTx(InterceptorContext invocation, final EJBComponent component) throws Exception {
    final ContextTransactionManager tm = ContextTransactionManager.getInstance();
    tm.begin();
    final AbstractTransaction tx = tm.getTransaction();
    Object result = null;
    Exception except = null;
    try {
        result = invocation.proceed();
    } catch (Throwable t) {
        ApplicationExceptionDetails ae = component.getApplicationException(t.getClass(), invocation.getMethod());
        try {
            try {
                throw t;
            } catch (EJBException | RemoteException e) {
                throw e;
            } catch (RuntimeException e) {
                if (ae != null && !ae.isRollback()) {
                    except = e;
                } else if (ae != null && ae.isRollback()) {
                    throw e;
                } else {
                    throw new EJBException(e);
                }
            } catch (Exception e) {
                throw e;
            } catch (Error e) {
                throw EjbLogger.ROOT_LOGGER.unexpectedError(e);
            } catch (Throwable e) {
                throw new EJBException(new UndeclaredThrowableException(e));
            }
        } catch (Throwable t2) {
            if (ae == null || ae.isRollback())
                setRollbackOnly(tx, t2);
            endTransaction(tx, t2);
            throw t2;
        }
    }
    boolean rolledBack = safeGetStatus(tx) == Status.STATUS_MARKED_ROLLBACK;
    endTransaction(tx);
    if (except != null) {
        throw except;
    }
    if (rolledBack)
        ourTxRolledBack();
    return result;
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) AbstractTransaction(org.wildfly.transaction.client.AbstractTransaction) NoSuchEJBException(javax.ejb.NoSuchEJBException) EJBException(javax.ejb.EJBException) NoSuchEJBException(javax.ejb.NoSuchEJBException) RollbackException(javax.transaction.RollbackException) NoSuchEntityException(javax.ejb.NoSuchEntityException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) EJBTransactionRolledbackException(javax.ejb.EJBTransactionRolledbackException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 3 with ContextTransactionManager

use of org.wildfly.transaction.client.ContextTransactionManager in project wildfly by wildfly.

the class CMTTxInterceptor method notSupported.

protected Object notSupported(InterceptorContext invocation, final EJBComponent component) throws Exception {
    final ContextTransactionManager tm = ContextTransactionManager.getInstance();
    Transaction tx = tm.getTransaction();
    if (tx != null) {
        safeSuspend();
        final Object result;
        try {
            result = invokeInNoTx(invocation, component);
        } catch (Throwable t) {
            safeResume(tx, t);
            throw t;
        }
        safeResume(tx);
        return result;
    } else {
        return invokeInNoTx(invocation, component);
    }
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) AbstractTransaction(org.wildfly.transaction.client.AbstractTransaction) Transaction(javax.transaction.Transaction)

Example 4 with ContextTransactionManager

use of org.wildfly.transaction.client.ContextTransactionManager in project wildfly by wildfly.

the class CMTTxInterceptor method processInvocation.

public Object processInvocation(InterceptorContext invocation) throws Exception {
    final EJBComponent component = (EJBComponent) invocation.getPrivateData(Component.class);
    final ContextTransactionManager tm = ContextTransactionManager.getInstance();
    final int oldTimeout = tm.getTransactionTimeout();
    try {
        final MethodIntf methodIntf = MethodIntfHelper.of(invocation);
        final Method method = invocation.getMethod();
        final TransactionAttributeType attr = component.getTransactionAttributeType(methodIntf, method);
        final int timeoutInSeconds = component.getTransactionTimeout(methodIntf, method);
        switch(attr) {
            case MANDATORY:
                return mandatory(invocation, component);
            case NEVER:
                return never(invocation, component);
            case NOT_SUPPORTED:
                return notSupported(invocation, component);
            case REQUIRED:
                final ComponentView view = invocation.getPrivateData(ComponentView.class);
                if (view != null && view.isAsynchronous(method)) {
                    // method are exactly the same as REQUIRES_NEW.
                    return requiresNew(invocation, component, timeoutInSeconds);
                }
                return required(invocation, component, timeoutInSeconds);
            case REQUIRES_NEW:
                return requiresNew(invocation, component, timeoutInSeconds);
            case SUPPORTS:
                return supports(invocation, component);
            default:
                throw EjbLogger.ROOT_LOGGER.unknownTxAttributeOnInvocation(attr, invocation);
        }
    } finally {
        // See also https://issues.jboss.org/browse/WFTC-44
        tm.setTransactionTimeout(oldTimeout == ContextTransactionManager.getGlobalDefaultTransactionTimeout() ? 0 : oldTimeout);
    }
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) ComponentView(org.jboss.as.ee.component.ComponentView) Method(java.lang.reflect.Method) EJBComponent(org.jboss.as.ejb3.component.EJBComponent) Component(org.jboss.as.ee.component.Component) EJBComponent(org.jboss.as.ejb3.component.EJBComponent) MethodIntf(org.jboss.as.ejb3.component.MethodIntf) TransactionAttributeType(javax.ejb.TransactionAttributeType)

Example 5 with ContextTransactionManager

use of org.wildfly.transaction.client.ContextTransactionManager in project wildfly by wildfly.

the class CMTTxInterceptor method requiresNew.

protected Object requiresNew(InterceptorContext invocation, final EJBComponent component, final int timeout) throws Exception {
    final ContextTransactionManager tm = ContextTransactionManager.getInstance();
    if (timeout != -1) {
        tm.setTransactionTimeout(timeout);
    }
    Transaction tx = tm.getTransaction();
    if (tx != null) {
        safeSuspend();
        final Object result;
        try {
            result = invokeInOurTx(invocation, component);
        } catch (Throwable t) {
            safeResume(tx, t);
            throw t;
        }
        safeResume(tx);
        return result;
    } else {
        return invokeInOurTx(invocation, component);
    }
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) AbstractTransaction(org.wildfly.transaction.client.AbstractTransaction) Transaction(javax.transaction.Transaction)

Aggregations

ContextTransactionManager (org.wildfly.transaction.client.ContextTransactionManager)14 Transaction (javax.transaction.Transaction)7 AbstractTransaction (org.wildfly.transaction.client.AbstractTransaction)7 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 StartException (org.jboss.msc.service.StartException)4 EJBException (javax.ejb.EJBException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HeuristicMixedException (javax.transaction.HeuristicMixedException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 EJBComponent (org.jboss.as.ejb3.component.EJBComponent)2 ZeroPortPolicy (com.sun.corba.se.spi.extension.ZeroPortPolicy)1 InvalidClassException (java.io.InvalidClassException)1 Method (java.lang.reflect.Method)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 RemoteException (java.rmi.RemoteException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1