@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")); } }
@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()); }
@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()); } }
@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; } }
@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(); }
@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(); }
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"); }
@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(); }
@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(); }
@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(); }
@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(); }
@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())); } }
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); }
@Test(expected = TransactionalException.class) public void should_mandatory_throw_exception() { bean.mandatory(); }
@Test(expected = TransactionalException.class) public void mandatoryKO() { for (int i = 0; i < 2; i++) { bean.withoutATxIllThrowAnException(); } }