Java Code Examples for javax.transaction.TransactionRequiredException

The following are top voted examples for showing how to use javax.transaction.TransactionRequiredException. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: javaee-samples   File: BeanManager.java   Source Code and License 6 votes vote down vote up
private static boolean canContinueWithTransaction(Transactional config, EntityTransaction transaction, Method m) {
    switch (config.value()) {
        case REQUIRED:
            return true;
        case MANDATORY:
            if (!transaction.isActive()) {
                throwTransactionalException(config.value(), m,
                        new TransactionRequiredException("called outside a transaction context"));
            }
            return false;
        case NEVER:
            if (transaction.isActive()) {
                throwTransactionalException(config.value(), m,
                        new InvalidTransactionException("called inside a transaction context"));
            }
            return false;
        case SUPPORTS:
            return false;
        case REQUIRES_NEW: // not fully supported
            return true;
        case NOT_SUPPORTED: // not fully supported
            return false;
        default:
            throw new UnsupportedOperationException("Unknown enum " + config.value());
    }
}
 
Example 2
Project: openjdk-jdk10   File: TransactionRequiredExceptionTests.java   Source Code and License 5 votes vote down vote up
/**
 * Create TransactionRequiredException with no-arg constructor
 */
@Test
public void test1() {
    TransactionRequiredException ex = new TransactionRequiredException();
    assertTrue(ex.getMessage() == null
            && ex.getCause() == null);
}
 
Example 3
Project: openjdk-jdk10   File: TransactionRequiredExceptionTests.java   Source Code and License 5 votes vote down vote up
/**
 * Create TransactionRequiredException with message
 */
@Test
public void test2() {
    TransactionRequiredException ex = new TransactionRequiredException(reason);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getCause() == null);
}
 
Example 4
Project: openjdk-jdk10   File: TransactionRequiredExceptionTests.java   Source Code and License 5 votes vote down vote up
/**
 * De-Serialize a TransactionRequiredException from JDBC 4.0 and make sure
 * you can read it back properly
 */
@Test
public void test3() throws Exception {

    ObjectInputStream ois = new ObjectInputStream(
            new ByteArrayInputStream(SerializedTransactionExceptions.TRE_DATA));
    TransactionRequiredException ex = (TransactionRequiredException) ois.readObject();
    assertTrue(reason.equals(ex.getMessage())
            && ex.getCause() == null);
}
 
Example 5
Project: openjdk9   File: TransactionRequiredExceptionTests.java   Source Code and License 5 votes vote down vote up
/**
 * Create TransactionRequiredException with no-arg constructor
 */
@Test
public void test1() {
    TransactionRequiredException ex = new TransactionRequiredException();
    assertTrue(ex.getMessage() == null
            && ex.getCause() == null);
}
 
Example 6
Project: openjdk9   File: TransactionRequiredExceptionTests.java   Source Code and License 5 votes vote down vote up
/**
 * Create TransactionRequiredException with message
 */
@Test
public void test2() {
    TransactionRequiredException ex = new TransactionRequiredException(reason);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getCause() == null);
}
 
Example 7
Project: openjdk9   File: TransactionRequiredExceptionTests.java   Source Code and License 5 votes vote down vote up
/**
 * De-Serialize a TransactionRequiredException from JDBC 4.0 and make sure
 * you can read it back properly
 */
@Test
public void test3() throws Exception {

    ObjectInputStream ois = new ObjectInputStream(
            new ByteArrayInputStream(SerializedTransactionExceptions.TRE_DATA));
    TransactionRequiredException ex = (TransactionRequiredException) ois.readObject();
    assertTrue(reason.equals(ex.getMessage())
            && ex.getCause() == null);
}
 
Example 8
Project: tomee   File: TxMandatory.java   Source Code and License 5 votes vote down vote up
public TxMandatory(final TransactionManager transactionManager) throws SystemException, ApplicationException {
    super(TransactionType.Mandatory, transactionManager);

    clientTx = getTransaction();
    if (clientTx == null) {
        throw new ApplicationException(new TransactionRequiredException());
    }
}
 
Example 9
Project: tomee   File: EJBInvocationHandler.java   Source Code and License 4 votes vote down vote up
/**
 * Renamed method so it shows up with a much more understandable purpose as it
 * will be the top element in the stacktrace
 *
 * @param e      Throwable
 * @param method Method
 */
protected Throwable convertException(final Throwable e, final Method method) {
    if (!remote && e instanceof RemoteException) {
        if (e instanceof TransactionRequiredException) {
            return new TransactionRequiredLocalException(e.getMessage()).initCause(getCause(e));
        }
        if (e instanceof TransactionRolledbackException) {
            return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e));
        }

        /**
         * If a client attempts to invoke a method on a removed bean's business interface,
         * we must throw a javax.ejb.NoSuchEJBException. If the business interface is a
         * remote business interface that extends java.rmi.Remote, the
         * java.rmi.NoSuchObjectException is thrown to the client instead.
         * See EJB 3.0, section 4.4
         */
        if (e instanceof NoSuchObjectException) {
            if (java.rmi.Remote.class.isAssignableFrom(method.getDeclaringClass())) {
                return e;
            } else {
                return new NoSuchEJBException(e.getMessage()).initCause(getCause(e));
            }
        }
        if (e instanceof AccessException) {
            return new AccessLocalException(e.getMessage()).initCause(getCause(e));
        }

        return new EJBException(e.getMessage()).initCause(getCause(e));
    }

    if (remote && e instanceof EJBAccessException) {
        if (e.getCause() instanceof Exception) {
            return new AccessException(e.getMessage(), (Exception) e.getCause());
        } else {
            return new AccessException(e.getMessage());
        }
    }
    if (!remote && e instanceof EJBTransactionRolledbackException) {
        return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e));
    }
    return e;
}
 
Example 10
Project: tomee   File: BaseEjbProxyHandler.java   Source Code and License 4 votes vote down vote up
/**
 * Renamed method so it shows up with a much more understandable purpose as it
 * will be the top element in the stacktrace
 *
 * @param e        Throwable
 * @param method   Method
 * @param interfce Class
 */
protected Throwable convertException(Throwable e, final Method method, final Class interfce) {
    final boolean rmiRemote = Remote.class.isAssignableFrom(interfce);
    if (e instanceof TransactionRequiredException) {
        if (!rmiRemote && interfaceType.isBusiness()) {
            return new EJBTransactionRequiredException(e.getMessage()).initCause(getCause(e));
        } else if (interfaceType.isLocal()) {
            return new TransactionRequiredLocalException(e.getMessage()).initCause(getCause(e));
        } else {
            return e;
        }
    }
    if (e instanceof TransactionRolledbackException) {
        if (!rmiRemote && interfaceType.isBusiness()) {
            return new EJBTransactionRolledbackException(e.getMessage()).initCause(getCause(e));
        } else if (interfaceType.isLocal()) {
            return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e));
        } else {
            return e;
        }
    }
    if (e instanceof NoSuchObjectException) {
        if (!rmiRemote && interfaceType.isBusiness()) {
            return new NoSuchEJBException(e.getMessage()).initCause(getCause(e));
        } else if (interfaceType.isLocal()) {
            return new NoSuchObjectLocalException(e.getMessage()).initCause(getCause(e));
        } else {
            return e;
        }
    }
    if (e instanceof AccessException) {
        if (!rmiRemote && interfaceType.isBusiness()) {
            return new AccessLocalException(e.getMessage()).initCause(getCause(e));
        } else if (interfaceType.isLocal()) {
            return new AccessLocalException(e.getMessage()).initCause(getCause(e));
        } else {
            return e;
        }
    }
    if (e instanceof RemoteException) {
        if (!rmiRemote && interfaceType.isBusiness()) {
            return new EJBException(e.getMessage()).initCause(getCause(e));
        } else if (interfaceType.isLocal()) {
            return new EJBException(e.getMessage()).initCause(getCause(e));
        } else {
            return e;
        }
    }

    for (final Class<?> type : method.getExceptionTypes()) {
        if (type.isAssignableFrom(e.getClass())) {
            return e;
        }
    }

    // Exception is undeclared
    // Try and find a runtime exception in there
    while (e.getCause() != null && !(e instanceof RuntimeException)) {
        e = e.getCause();
    }
    return e;
}