Java 类javax.transaction.TransactionalException 实例源码

项目:incubator-servicecomb-saga    文件:SagaStartAnnotationProcessorTest.java   
@Test
public void transformInterceptedException() {
  MessageSender sender = mock(MessageSender.class);
  SagaStartAnnotationProcessor sagaStartAnnotationProcessor = new SagaStartAnnotationProcessor(context, sender);

  doThrow(exception).when(sender).send(any());

  try {
    sagaStartAnnotationProcessor.preIntercept(null, null);
    expectFailing(TransactionalException.class);
  } catch (TransactionalException e) {
    assertThat(e.getMessage(), is("exception"));
    assertThat(e.getCause(), instanceOf(RuntimeException.class));
    assertThat(e.getCause().getMessage(), is("runtime exception"));
  }
}
项目:tomee    文件:TransactionalTest.java   
@Test
public void neverInTx() throws SystemException {
    assertNull(OpenEJB.getTransactionManager().getTransaction());
    try {
        bean.createTx(new Runnable() {
            @Override
            public void run() {
                bean.never();
            }
        });
        fail();
    } catch (final TransactionalException e) {
        // expected
    }
    assertNull(OpenEJB.getTransactionManager().getTransaction());
}
项目:incubator-servicecomb-saga    文件:SagaStartAnnotationProcessor.java   
@Override
public AlphaResponse preIntercept(String parentTxId, String compensationMethod, Object... message) {
  try {
    return sender.send(new SagaStartedEvent(omegaContext.globalTxId(), omegaContext.localTxId()));
  } catch (OmegaException e) {
    throw new TransactionalException(e.getMessage(), e.getCause());
  }
}
项目:crnk-framework    文件:CdiTransactionRunner.java   
@Override
public <T> T doInTransaction(Callable<T> callable) {
    try {
        return impl.doInTransaction(callable);
    } catch (TransactionalException e) {
        // unwrap since not usable, cause more interesting
        // (validationException, etc.)
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw e;
    }
}
项目:crnk-framework    文件:CdiTransactionRunnerTest.java   
@Test
public void testTransactionalRuntimeExceptionToBeUnwrapped() throws Exception {
    Callable callable = Mockito.mock(Callable.class);
    Mockito.when(callable.call()).thenThrow(new TransactionalException("a", new IllegalStateException("b")));
    try {
        runner.doInTransaction(callable);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertEquals("b", e.getMessage());
    }
    Mockito.verify(callable, Mockito.times(1)).call();
}
项目:crnk-framework    文件:CdiTransactionRunnerTest.java   
@Test
public void testTransactionalExceptionNotToBeUnwrapped() throws Exception {
    Callable callable = Mockito.mock(Callable.class);
    Mockito.when(callable.call()).thenThrow(new TransactionalException("a", new IOException("b")));
    try {
        runner.doInTransaction(callable);
        Assert.fail();
    } catch (TransactionalException e) {
        Assert.assertEquals("a", e.getMessage());
    }
    Mockito.verify(callable, Mockito.times(1)).call();
}
项目:katharsis-framework    文件:CdiTransactionRunner.java   
@Override
public <T> T doInTransaction(Callable<T> callable) {
    try {
        return impl.doInTransaction(callable);
    }
    catch (TransactionalException e) {
        // unwrap since not usable, cause more interesting
        // (validationException, etc.)
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw e;
    }
}
项目:javaee-samples    文件:JPARule.java   
public <T> T $$(Function<EntityManager, T> block) {
    if (beanManager.isEntityManagerExistsOpen()) {
        beanManager.getCurrentEntityManager().close();
    }
    final EntityManager em = beanManager.getCurrentEntityManager();
    final EntityTransaction transaction = em.getTransaction();
    if (transaction.isActive()) {
        throw new TransactionalException("Transactional block must not be called within current transaction.",
                new InvalidTransactionException("transaction in " + Thread.currentThread()));
    }

    Optional<Throwable> t = empty();

    try {
        transaction.begin();
        return block.apply(em);
    } catch (Throwable e) {
        t = of(e);
        e.printStackTrace();
    } finally {
        try {
            if (t.isPresent()) {
                t = performAndAddSuppressedException(transaction::rollback, t);
            } else {
                t = perform(transaction::commit);
                t.ifPresent(Throwable::printStackTrace);
            }
        } finally {
            boolean noExceptionBefore = !t.isPresent();
            t = performAndAddSuppressedException(em::close, t);
            t.filter(x -> noExceptionBefore).ifPresent(Throwable::printStackTrace);
        }

        t.ifPresent(x -> { throw new TransactionalException(x.getLocalizedMessage(), x); });
    }

    throw new AssertionError("unreachable statement");
}
项目:javaee-samples    文件:TransactionalInterceptorWithFieldInjectionTest.java   
@Test
@Transactional
public void shouldFailOnNeverTransaction() {
    exceptionHandler.expect(TransactionalException.class);
    exceptionHandler.expectMessage("Transaction not applicable to " +
            "@Transactional(value = Transactional.TxType.NEVER) " +
            "in bean method call " +
            "public void javaee.samples.frameworks.injection.transactional.injection.field.NeverOuterService.saveOuter(java.lang.String)");

        outerService.saveOuter("transaction");
        jpa.getCurrentEntityManager()
                .createQuery("select count(e) from MyEntity e", Long.class)
                .getSingleResult();
}
项目:javaee-samples    文件:TransactionalInterceptorWithFieldInjectionTest.java   
@Test
public void shouldFailOnNonExistingMandatoryTransaction() {
    exceptionHandler.expect(TransactionalException.class);
    exceptionHandler.expectMessage("Transaction not applicable to " +
            "@Transactional(value = Transactional.TxType.MANDATORY) " +
            "in bean method call " +
            "public void javaee.samples.frameworks.injection.transactional.injection.field.MandatoryOuterService.saveOuter(java.lang.String)");

    outerService.saveOuter("transaction");
    jpa.getCurrentEntityManager()
            .createQuery("select count(e) from MyEntity e", Long.class)
            .getSingleResult();
}
项目:javaee-samples    文件:TransactionalInterceptorWithConstructorInjectionTest.java   
@Test
@Transactional
public void shouldFailOnNeverTransaction() {
    exceptionHandler.expect(TransactionalException.class);
    exceptionHandler.expectMessage("Transaction not applicable to " +
            "@Transactional(value = Transactional.TxType.NEVER) " +
            "in bean method call " +
            "public void javaee.samples.frameworks.injection.transactional.injection.constructor.NeverOuterService.saveOuter(java.lang.String)");

        outerService.saveOuter("transaction");
        jpa.getCurrentEntityManager()
                .createQuery("select count(e) from MyEntity e", Long.class)
                .getSingleResult();
}
项目:javaee-samples    文件:TransactionalInterceptorWithConstructorInjectionTest.java   
@Test
public void shouldFailOnNonExistingMandatoryTransaction() {
    exceptionHandler.expect(TransactionalException.class);
    exceptionHandler.expectMessage("Transaction not applicable to " +
            "@Transactional(value = Transactional.TxType.MANDATORY) " +
            "in bean method call " +
            "public void javaee.samples.frameworks.injection.transactional.injection.constructor.MandatoryOuterService.saveOuter(java.lang.String)");

    outerService.saveOuter("transaction");
    jpa.getCurrentEntityManager()
            .createQuery("select count(e) from MyEntity e", Long.class)
            .getSingleResult();
}
项目:tomee    文件:NeverInterceptor.java   
@AroundInvoke
public Object intercept(final InvocationContext ic) throws Exception {
    try {
        return super.intercept(ic);
    } catch (final RemoteException re) {
        throw new TransactionalException(re.getMessage(), new InvalidTransactionException(re.getMessage()));
    }
}
项目:javaee-samples    文件:BeanManager.java   
private static void throwTransactionalException(TxType txType, Method m, Throwable cause) {
    throw new TransactionalException("Transaction not applicable to "
            + "@Transactional(value = Transactional.TxType." + txType + ") "
            + "in bean method call "
            + m.toGenericString(), cause);
}
项目:JavaIncrementalParser    文件:MyTransactionalTxTypeBeanTest.java   
@Test(expected = TransactionalException.class)
public void should_mandatory_throw_exception() {
    bean.mandatory();
}
项目:tomee    文件:TransactionalTest.java   
@Test(expected = TransactionalException.class)
public void mandatoryKO() {
    for (int i = 0; i < 2; i++) {
        bean.withoutATxIllThrowAnException();
    }
}