javax.ejb.EJBTransactionRolledbackException Java Examples

The following examples show how to use javax.ejb.EJBTransactionRolledbackException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BillingAdapterBean.java    From development with Apache License 2.0 6 votes vote down vote up
@Override
public Response setDefaultBillingAdapter(POBillingAdapter poBillingAdapter)
        throws SaaSApplicationException {
    Response response = new Response();

    BillingAdapter adapter = billingAdapter.get(poBillingAdapter
            .getBillingIdentifier());

    if (adapter == null) {
        sessionCtx.setRollbackOnly();
        throw new ObjectNotFoundException(ClassEnum.BILLING_ADAPTER,
                poBillingAdapter.getBillingIdentifier());
    } else {

        BasePOAssembler.verifyVersionAndKey(adapter, poBillingAdapter);

        try {
            billingAdapter.setDefaultAdapter(adapter);
        } catch (EJBTransactionRolledbackException e) {
            sessionCtx.setRollbackOnly();
            throw new SaaSApplicationException(e);
        }
    }

    return response;
}
 
Example #2
Source File: TransactionInvocationHandlers.java    From development with Apache License 2.0 6 votes vote down vote up
public Object call(Callable<Object> callable,
        IInvocationHandler.IInvocationCtx ctx) throws Exception {
    try {
        return callable.call();
    } catch (Exception ex) {
        if (ctx.isApplicationException(ex)) {
            if (hasRollbackAnnotation(ex)) {
                ctx.getTransactionManager().setRollbackOnly();
            }
            throw ex;
        } else {
            ctx.getTransactionManager().setRollbackOnly();
            throw setCause(new EJBTransactionRolledbackException(
                    "Rollback due to exception.", ex));
        }
    }
}
 
Example #3
Source File: ReviewServiceLocalBeanTest.java    From development with Apache License 2.0 6 votes vote down vote up
@Test(expected = ConcurrentModificationException.class)
public void writeReview_ConcurrentModificationException() throws Exception {
    // given
    initProduct(1l);
    initProductReview(2l);
    productReview.setPlatformUser(currentUser);
    doReturn(product).when(dm).getReference(Product.class, 1l);
    doThrow(new EJBTransactionRolledbackException()).when(dm).flush();
    // when
    try {
        reviewBean.writeReview(productReview,
                Long.valueOf(product.getKey()));
        // then
        fail();
    } catch (Exception e) {
        throw e;
    }
}
 
Example #4
Source File: BillingAdapterDAO.java    From development with Apache License 2.0 6 votes vote down vote up
@RolesAllowed("PLATFORM_OPERATOR")
public void setDefaultAdapter(BillingAdapter billingAdapter)
        throws EJBTransactionRolledbackException {

    ArgumentValidator.notNull("Billing adapter", billingAdapter);

    List<BillingAdapter> adapters = getAll();
    for (BillingAdapter ba : adapters) {

        if (ba.getBillingIdentifier().equals(
                billingAdapter.getBillingIdentifier())) {
            ba.setDefaultAdapter(true);
        } else {
            ba.setDefaultAdapter(false);
        }
    }

    ds.flush();
}
 
Example #5
Source File: PasswordRecoveryServiceNoDBTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void completePasswordRecovery_EJBTransactionFailed()
        throws Exception {
    // given
    doThrow(new EJBTransactionRolledbackException()).when(
            passwordRecoverybean.dm).flush();
    // when
    boolean result = passwordRecoverybean.completePasswordRecovery(userId,
            password_6letter);
    // then
    verify(passwordRecoverybean, never()).sendPasswordRecoveryMails(
            any(PlatformUser.class), any(EmailType.class), anyString(),
            any(Object[].class));
    assertEquals(false, result);
}
 
Example #6
Source File: PetTypeController.java    From javaee7-petclinic with GNU General Public License v2.0 5 votes vote down vote up
public String delete(long id){
    try {
        petTypeDao.delete(id);
    } catch (EJBTransactionRolledbackException e) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ctx.addMessage(null, new FacesMessage("cannot delete, object still in use"));
    }
    return "petTypeList.jsf";
}
 
Example #7
Source File: ReviewServiceLocalBean.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Update new review to the database
 * 
 * @param productReview
 *            review need to be updated
 * @throws ObjectNotFoundException
 *             if the review is not found
 * @throws ConcurrentModificationException
 *             if the review has been modified concurrently
 * @throws OperationNotPermittedException
 *             if the caller is not allowed to perform the operation.
 */
private void updatedReview(ProductReview productReview)
        throws ObjectNotFoundException, ConcurrentModificationException,
        OperationNotPermittedException {
    checkIfAllowedToModify(productReview);
    try {
        dm.flush();
    } catch (EJBTransactionRolledbackException e) {
        ConcurrentModificationException cme = new ConcurrentModificationException(
                productReview.getClass().getSimpleName(),
                productReview.getVersion());
        cme.fillInStackTrace();
        throw cme;
    }
}
 
Example #8
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_NEWTX_CommitFailed2() throws Exception {
    failCommit = true;
    ctxStubIsApplicationException = true;
    Exception root = new IOException();
    try {
        TransactionInvocationHandlers.HANDLER_NEWTX.call(
                callableException(root), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ex) {
    }
    assertEquals(Arrays.asList("begin()", "call()", "commit()"), stubCalls);
}
 
Example #9
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_NEWTX_CommitFailed1() throws Exception {
    failCommit = true;
    try {
        TransactionInvocationHandlers.HANDLER_NEWTX.call(
                callableReturns(new Object()), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ejbex) {
    }
    assertEquals(Arrays.asList("begin()", "call()", "commit()"), stubCalls);
}
 
Example #10
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_NEWTX_Positive_MarkedRollback() throws Exception {
    try {
        TransactionInvocationHandlers.HANDLER_NEWTX.call(
                callableMarkRollback(), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ejbex) {
        assertNull(ejbex.getCause());
        assertNull(ejbex.getCausedByException());
    }
    assertEquals(Arrays.asList("begin()", "call()", "setRollbackOnly()",
            "rollback()"), stubCalls);
}
 
Example #11
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_WITHINTX_Exception() throws Exception {
    final Exception root = new IOException();
    try {
        TransactionInvocationHandlers.HANDLER_WITHINTX.call(
                callableException(root), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ejbex) {
        assertSame(root, ejbex.getCause());
        assertSame(root, ejbex.getCausedByException());
    }
    assertEquals(Arrays.asList("call()", "setRollbackOnly()"), stubCalls);
}
 
Example #12
Source File: SubscriptionIT.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * <b>Testcase:</b> Try to insert Subscription without a related Product<br>
 * <b>ExpectedResult:</b> PersistenceException
 * 
 * @throws Throwable
 */
@Test(expected = EJBTransactionRolledbackException.class)
public void testSubscriptionWithoutProduct() throws Throwable {
    runTX(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            doTestSubscriptionWithoutProduct();
            return null;
        }
    });
}
 
Example #13
Source File: SubscriptionIT.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * <b>Testcase:</b> Try to insert Subscription without a related
 * Organization<br>
 * <b>ExpectedResult:</b> PersistenceException
 * 
 * @throws Throwable
 */
@Test(expected = EJBTransactionRolledbackException.class)
public void testSubscriptionWithoutOrganization() throws Throwable {
    runTX(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            doTestSubscriptionWithoutOrganization();
            return null;
        }
    });
}
 
Example #14
Source File: OrganizationRoleIT.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to create a organization/role reference that already exists in the
 * same constellation. This should end up in an exception.
 */
@Test
public void testCreateOrganizationToRoleDuplicate() throws Exception {
    final OrganizationRole role = new OrganizationRole();
    role.setRoleName(OrganizationRoleType.SUPPLIER);
    persistDO(role);
    roles.add(role);

    doCreateCustomerOrgs();

    final OrganizationToRole orgToRole1 = new OrganizationToRole();
    orgToRole1.setOrganization(orgs.get(0));
    orgToRole1.setOrganizationRole(role);
    persistDO(orgToRole1);
    orgToRoles.add(orgToRole1);

    final OrganizationToRole orgToRole2 = new OrganizationToRole();
    orgToRole2.setOrganization(orgs.get(0));
    orgToRole2.setOrganizationRole(role);
    try {
        persistDO(orgToRole2);
        Assert.fail("Object must not be stored due to unique constraint violation");
    } catch (EJBTransactionRolledbackException e) {
        // now ensure that the second store operation did not pass
        OrganizationToRole storedOrgToRole = runTX(new Callable<OrganizationToRole>() {
            public OrganizationToRole call() throws Exception {
                return doGetStoredOrgToRoleviaTK(orgToRole2);
            }
        });
        Assert.assertNull("Transaction has not been rolled back",
                storedOrgToRole);

        // also ensure that no history has been created
        List<?> hist = getHistoryForDO(orgToRole2);
        Assert.assertEquals(
                "Transaction rollback did not remove history entries", 0,
                hist.size());
    }
    orgToRoles.add(orgToRole2);
}
 
Example #15
Source File: PlatformUserIT.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * <b>Testcase:</b> Try to insert User without a related Organization<br>
 * <b>ExpectedResult:</b> PersistenceException
 * 
 * @throws Throwable
 */
@Test(expected = EJBTransactionRolledbackException.class)
public void testUserWithoutOrganization() throws Throwable {
    runTX(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            doTestUserWithoutOrganization();
            return null;
        }
    });
}
 
Example #16
Source File: BillingAdapterDAO.java    From development with Apache License 2.0 5 votes vote down vote up
@RolesAllowed("PLATFORM_OPERATOR")
public void save(BillingAdapter billingAdapter)
        throws ObjectNotFoundException, NonUniqueBusinessKeyException,
        DuplicateAdapterException {

    ArgumentValidator.notNull("Billing adapter", billingAdapter);

    try {

        if (billingAdapter.getKey() == 0L) {
            ds.persist(billingAdapter);
        } else {
            BillingAdapter ba = ds.getReference(BillingAdapter.class,
                    billingAdapter.getKey());
            ba.setBillingIdentifier(billingAdapter.getBillingIdentifier());
            ba.setName(billingAdapter.getName());
            ba.setDefaultAdapter(billingAdapter.isDefaultAdapter());
            ba.setConnectionProperties(billingAdapter
                    .getConnectionProperties());

            ds.flush();
        }
    } catch (EJBTransactionRolledbackException e) {

        if (isEntityExistsException(e)) {
            throw new DuplicateAdapterException(
                    "Duplicate adapter with unique id "
                            + billingAdapter.getBillingIdentifier());
        } else {
            throw e;
        }
    }

}
 
Example #17
Source File: TransactionInvocationHandlers.java    From development with Apache License 2.0 5 votes vote down vote up
private void commit(IInvocationHandler.IInvocationCtx ctx) {
    try {
        ctx.getTransactionManager().commit();
    } catch (Exception ex) {
        throw setCause(new EJBTransactionRolledbackException(
                "Commit failed.", ex));
    }
}
 
Example #18
Source File: TransactionInvocationHandlers.java    From development with Apache License 2.0 5 votes vote down vote up
public Object call(Callable<Object> callable,
        IInvocationHandler.IInvocationCtx ctx) throws Exception {
    final Object result;
    ctx.getTransactionManager().begin();
    try {
        result = callable.call();
    } catch (Exception ex) {
        if (ctx.isApplicationException(ex)) {
            if (hasRollbackAnnotation(ex)
                    || ctx.getTransactionManager().getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                ctx.getTransactionManager().rollback();
            } else {
                commit(ctx);
            }
            throw ex;
        }
        ctx.getTransactionManager().rollback();
        throw setCause(new EJBException(ex));
    } catch (Error err) {
        // Required for AssertErrors thrown by JUnit4 assertions:
        ctx.getTransactionManager().rollback();
        throw err;
    }
    if (ctx.getTransactionManager().getStatus() == Status.STATUS_MARKED_ROLLBACK) {
        ctx.getTransactionManager().rollback();
        throw new EJBTransactionRolledbackException();
    }
    commit(ctx);
    return result;
}
 
Example #19
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_NEWTX_CommitFailed2() throws Exception {
    failCommit = true;
    ctxStubIsApplicationException = true;
    Exception root = new IOException();
    try {
        TransactionInvocationHandlers.HANDLER_NEWTX.call(
                callableException(root), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ex) {
    }
    assertEquals(Arrays.asList("begin()", "call()", "commit()"), stubCalls);
}
 
Example #20
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_NEWTX_CommitFailed1() throws Exception {
    failCommit = true;
    try {
        TransactionInvocationHandlers.HANDLER_NEWTX.call(
                callableReturns(new Object()), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ejbex) {
    }
    assertEquals(Arrays.asList("begin()", "call()", "commit()"), stubCalls);
}
 
Example #21
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_NEWTX_Positive_MarkedRollback() throws Exception {
    try {
        TransactionInvocationHandlers.HANDLER_NEWTX.call(
                callableMarkRollback(), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ejbex) {
        assertNull(ejbex.getCause());
        assertNull(ejbex.getCausedByException());
    }
    assertEquals(Arrays.asList("begin()", "call()", "setRollbackOnly()",
            "rollback()"), stubCalls);
}
 
Example #22
Source File: TransactionInvocationHandlersTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void testHANDLER_WITHINTX_Exception() throws Exception {
    final Exception root = new IOException();
    try {
        TransactionInvocationHandlers.HANDLER_WITHINTX.call(
                callableException(root), ctxStub);
        fail("Exception expected");
    } catch (EJBTransactionRolledbackException ejbex) {
        assertSame(root, ejbex.getCause());
        assertSame(root, ejbex.getCausedByException());
    }
    assertEquals(Arrays.asList("call()", "setRollbackOnly()"), stubCalls);
}
 
Example #23
Source File: PaymentServiceBean.java    From development with Apache License 2.0 5 votes vote down vote up
private boolean isTransactionRolledBack(Throwable th) {
    Throwable cause = th.getCause();
    while (cause != null && !cause.equals(th)) {
        if (cause instanceof EJBTransactionRolledbackException) {
            return true;
        }
        return isTransactionRolledBack(cause);
    }
    return false;
}
 
Example #24
Source File: PasswordRecoveryServiceNoDBTest.java    From development with Apache License 2.0 5 votes vote down vote up
@Test
public void startPasswordRecovery_EJBTransactionFailed() throws Exception {
    // given
    doThrow(new EJBTransactionRolledbackException()).when(
            passwordRecoverybean.dm).flush();

    // when
    passwordRecoverybean.startPasswordRecovery(userId, marketplaceId);
    // then
    verify(passwordRecoverybean, never()).sendPasswordRecoveryMails(
            any(PlatformUser.class), any(EmailType.class), anyString(),
            any(Object[].class));
}
 
Example #25
Source File: EJBInvocationHandler.java    From tomee with Apache License 2.0 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 #26
Source File: BaseEjbProxyHandler.java    From tomee with Apache License 2.0 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;
}