@Test public void returnImmediatelyWhenReceivedRejectResponse() throws Throwable { MessageSender sender = mock(MessageSender.class); when(sender.send(any())).thenReturn(new AlphaResponse(true)); TransactionAspect aspect = new TransactionAspect(sender, omegaContext); try { aspect.advise(joinPoint, compensable); expectFailing(InvalidTransactionException.class); } catch (InvalidTransactionException e) { System.out.println(e.getMessage()); assertThat(e.getMessage().contains("Abort sub transaction"), is(true)); } verify(sender, times(1)).send(any()); }
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()); } }
public void resume(final Transaction tx) throws InvalidTransactionException { if (tx == null) { throw new InvalidTransactionException("Transaction is null"); } if (!(tx instanceof MyTransaction)) { throw new InvalidTransactionException("Unknown transaction type " + tx.getClass().getName()); } final MyTransaction myTransaction = (MyTransaction) tx; if (threadTransaction.get() != null) { throw new IllegalStateException("A transaction is already active"); } final int status = myTransaction.getStatus(); if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) { throw new InvalidTransactionException("Expected transaction to be STATUS_ACTIVE or STATUS_MARKED_ROLLBACK, but was " + status); } threadTransaction.set(myTransaction); }
@Override public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { if (log.isDebugEnabled()) { log.debug("resuming " + transaction); } if (transaction == null) throw new InvalidTransactionException("resumed transaction cannot be null"); if (!(transaction instanceof BitronixTransaction)) throw new InvalidTransactionException("resumed transaction must be an instance of BitronixTransaction"); BitronixTransaction tx = (BitronixTransaction) transaction; if (getCurrentTransaction() != null) throw new IllegalStateException("a transaction is already running on this thread"); try { XAResourceManager resourceManager = tx.getResourceManager(); resourceManager.resume(); ThreadContext threadContext = ThreadContext.getThreadContext(); threadContext.setTransaction(tx); inFlightTransactions.get(tx).setThreadContext(threadContext); MDC.put(MDC_GTRID_KEY, tx.getGtrid()); } catch (XAException ex) { String extraErrorDetails = TransactionManagerServices.getExceptionAnalyzer().extractExtraXAExceptionDetails(ex); throw new BitronixSystemException("cannot resume " + tx + ", error=" + Decoder.decodeXAExceptionErrorCode(ex) + (extraErrorDetails == null ? "" : ", extra error=" + extraErrorDetails), ex); } }
@Around("execution(@org.apache.servicecomb.saga.omega.transaction.annotations.Compensable * *(..)) && @annotation(compensable)") Object advise(ProceedingJoinPoint joinPoint, Compensable compensable) throws Throwable { Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); LOG.debug("Intercepting compensable method {} with context {}", method.toString(), context); String signature = compensationMethodSignature(joinPoint, compensable, method); String localTxId = context.localTxId(); context.newLocalTxId(); TimeAwareInterceptor interceptor = new TimeAwareInterceptor(this.interceptor); AlphaResponse response = interceptor.preIntercept(localTxId, signature, joinPoint.getArgs()); if (response.aborted()) { String abortedLocalTxId = context.localTxId(); context.setLocalTxId(localTxId); throw new InvalidTransactionException("Abort sub transaction " + abortedLocalTxId + " because global transaction " + context.globalTxId() + " has already aborted."); } LOG.debug("Updated context {} for compensable method {} ", context, method.toString()); scheduleTimeoutTask(interceptor, localTxId, signature, method, compensable.timeout()); try { Object result = joinPoint.proceed(); interceptor.postIntercept(localTxId, signature); return result; } catch (Throwable throwable) { interceptor.onError(localTxId, signature, throwable); throw throwable; } finally { context.setLocalTxId(localTxId); LOG.debug("Restored context back to {}", context); } }
public void resume(Transaction tx) throws InvalidTransactionException, IllegalStateException, SystemException { if (!(tx instanceof SimpleTransaction)) { throw new IllegalStateException("Transaction must be a SimpleTransaction to resume"); } SimpleTransaction.resume((SimpleTransaction) tx); }
/** * Perform a JTA resume on the JTA TransactionManager. * <p>Can be overridden in subclasses, for specific JTA implementations. * @param txObject the JtaTransactionObject containing the UserTransaction * @param suspendedTransaction the suspended JTA Transaction object * @throws InvalidTransactionException if thrown by JTA methods * @throws SystemException if thrown by JTA methods * @see #getTransactionManager() * @see javax.transaction.TransactionManager#resume(javax.transaction.Transaction) */ protected void doJtaResume(JtaTransactionObject txObject, Object suspendedTransaction) throws InvalidTransactionException, SystemException { if (getTransactionManager() == null) { throw new TransactionSuspensionNotSupportedException( "JtaTransactionManager needs a JTA TransactionManager for suspending a transaction: " + "specify the 'transactionManager' or 'transactionManagerName' property"); } getTransactionManager().resume((Transaction) suspendedTransaction); }
/** * Create InvalidTransactionException with no-arg constructor */ @Test public void test1() { InvalidTransactionException ex = new InvalidTransactionException(); assertTrue(ex.getMessage() == null && ex.getCause() == null); }
/** * Create InvalidTransactionException with message */ @Test public void test2() { InvalidTransactionException ex = new InvalidTransactionException(reason); assertTrue(ex.getMessage().equals(reason) && ex.getCause() == null); }
/** * De-Serialize a InvalidTransactionException 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.ITE_DATA)); InvalidTransactionException ex = (InvalidTransactionException) ois.readObject(); assertTrue(reason.equals(ex.getMessage()) && ex.getCause() == null); }
public TransactionManager getTransactionManager() { return new TransactionManager() { public void begin() throws NotSupportedException, SystemException { } public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException { } public int getStatus() throws SystemException { return TransactionUtil.STATUS_NO_TRANSACTION; } public Transaction getTransaction() throws SystemException { return null; } public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { } public void rollback() throws IllegalStateException, SecurityException, SystemException { } public void setRollbackOnly() throws IllegalStateException, SystemException { } public void setTransactionTimeout(int i) throws SystemException { } public Transaction suspend() throws SystemException { return null; } }; }
public void resume(final Transaction resumeTx) throws InvalidTransactionException, IllegalStateException, SystemException { final ExtendedTransaction tx = getCurrent(); if (tx != null) { throw new LjtIllegalStateException("Already assigned the other transaction for resume()."); } ((ExtendedTransaction) resumeTx).resume(); setCurrent((ExtendedTransaction) resumeTx); }
private <T> T suspendTransactionAndExecute(Supplier<T> supplier) { Transaction old; try { old = txm.suspend(); try { return supplier.get(); } finally { txm.resume(old); } } catch (InvalidTransactionException | IllegalStateException | SystemException e) { throw new TransactionException("Error while suspending and resuming transaction", e); } }
@Override public void resume(Transaction tx) throws InvalidTransactionException, IllegalStateException { if (getTransaction() != null) { throw new IllegalStateException("Transaction is already associated"); } if (!(tx instanceof TransactionImpl)) { throw new InvalidTransactionException("Foreign transaction: " + tx); } TRANSACTIONS.set((TransactionImpl) tx); }
public void resume(Transaction tx) throws IllegalStateException, InvalidTransactionException, SystemException { if (TransactionImpl.class.isInstance(tx)) { TransactionImpl transaction = (TransactionImpl) tx; transaction.resumeAllResource(); AssociatedContext bctx = new AssociatedContext(); bctx.setThread(Thread.currentThread()); this.threadToTxnMap.put(Thread.currentThread(), bctx); } else { throw new InvalidTransactionException(); } }
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"); }
@Override public void resume(Transaction transaction) throws InvalidTransactionException, SystemException { if (!(transaction instanceof TransactionAdapter) || this.suspendTx == null || !this.suspendTx.remove(transaction)) { throw new InvalidTransactionException("the transaction is invalid"); } Transaction tx = ((TransactionAdapter) transaction).getTx(); Transaction current = TX_HOLDER.get(); if (current != null) { throw new IllegalStateException("the thread already has a transaction"); } ((TransactionImpl) tx).resume(); TX_HOLDER.set(tx); }
@Override public void doNonTransactionalWork(final Runnable runnable) throws NotSupportedException { try { final Transaction transaction = getTransactionManager().suspend(); try { runnable.run(); } finally { getTransactionManager().resume(transaction); } } catch (final InvalidTransactionException | SystemException e) { log.error("Cannot suspend transaction", e); throw new NotSupportedException(e.getMessage()); } }
/** * {@inheritDoc} */ public void resume(Transaction tobj) throws InvalidTransactionException, IllegalStateException, SystemException { if (!(tobj instanceof TransactionImpl)) throw new SystemException(); registry.assignTransaction((TransactionImpl)tobj); }
/** * {@inheritDoc} */ public void resume( Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { transactionManager.resume(transaction); }
@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())); } }
public void testSimpleAssertions() throws Exception { if (log.isDebugEnabled()) { log.debug("*** getting TM"); } BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager(); assertNull(tm.suspend()); try { tm.resume(null); fail("expected InvalidTransactionException"); } catch (InvalidTransactionException ex) { assertEquals("resumed transaction cannot be null", ex.getMessage()); } if (log.isDebugEnabled()) { log.debug("*** before begin"); } tm.begin(); if (log.isDebugEnabled()) { log.debug("*** after begin"); } if (log.isDebugEnabled()) { log.debug("*** getting connection from DS1"); } Connection connection1 = poolingDataSource1.getConnection(); connection1.createStatement(); if (log.isDebugEnabled()) { log.debug("*** closing connection 1"); } connection1.close(); if (log.isDebugEnabled()) { log.debug("*** committing"); } tm.commit(); if (log.isDebugEnabled()) { log.debug("*** TX is done"); } }
@Test(timeout = 60000) public void testSessionArgsIgnoredWithTm() throws Exception { JmsPoolXAConnectionFactory pcf = new JmsPoolXAConnectionFactory(); pcf.setConnectionFactory(new ActiveMQXAConnectionFactory( "vm://test?broker.persistent=false&broker.useJmx=false")); // simple TM that with no tx pcf.setTransactionManager(new TransactionManager() { @Override public void begin() throws NotSupportedException, SystemException { throw new SystemException("NoTx"); } @Override public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException { throw new IllegalStateException("NoTx"); } @Override public int getStatus() throws SystemException { return Status.STATUS_NO_TRANSACTION; } @Override public Transaction getTransaction() throws SystemException { throw new SystemException("NoTx"); } @Override public void resume(Transaction tobj) throws IllegalStateException, InvalidTransactionException, SystemException { throw new IllegalStateException("NoTx"); } @Override public void rollback() throws IllegalStateException, SecurityException, SystemException { throw new IllegalStateException("NoTx"); } @Override public void setRollbackOnly() throws IllegalStateException, SystemException { throw new IllegalStateException("NoTx"); } @Override public void setTransactionTimeout(int seconds) throws SystemException { } @Override public Transaction suspend() throws SystemException { throw new SystemException("NoTx"); } }); QueueConnection connection = pcf.createQueueConnection(); // like ee tck assertNotNull("can create session(false, 0)", connection.createQueueSession(false, 0)); connection.close(); pcf.stop(); }
private TransactionManager transactionManager() { return new TransactionManager() { private int status = Status.STATUS_NO_TRANSACTION; @Override public void begin() throws NotSupportedException, SystemException { status = Status.STATUS_ACTIVE; } @Override public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException { status = Status.STATUS_NO_TRANSACTION; } @Override public void rollback() throws IllegalStateException, SecurityException, SystemException { status = Status.STATUS_NO_TRANSACTION; } @Override public void setRollbackOnly() throws IllegalStateException, SystemException { // Nothing to do } @Override public int getStatus() throws SystemException { return status; } @Override public Transaction getTransaction() throws SystemException { return transaction(); } @Override public void setTransactionTimeout(int seconds) throws SystemException { throw new AssertionError("setTransactionTimeout"); } @Override public Transaction suspend() throws SystemException { return transaction(); } @Override public void resume(Transaction tobj) throws InvalidTransactionException, IllegalStateException, SystemException { // nothing to do } }; }
@Override public void resume( final Transaction tobj ) throws InvalidTransactionException, IllegalStateException, SystemException { delegate.resume( tobj ); }
@Override public void resume(Transaction arg0) throws IllegalStateException, InvalidTransactionException, SystemException { // not used }
@Override public void resume(Transaction tobj) throws InvalidTransactionException, IllegalStateException, SystemException { // TODO Auto-generated method stub }
public void resume(Transaction tx) throws InvalidTransactionException, IllegalStateException, SystemException { current = (DummyTransaction) tx; }
public void resume(Transaction tx) throws IllegalStateException, InvalidTransactionException, SystemException { throw new UnsupportedOperationException("resume"); }
/** {@inheritDoc} */ @Override public void resume(Transaction tobj) throws InvalidTransactionException, IllegalStateException, SystemException { mgr.resume(tobj); }
/** {@inheritDoc} */ @Override public void resume(Transaction tobj) throws InvalidTransactionException, IllegalStateException, SystemException { }
@Override public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { }