List of usage examples for org.springframework.transaction IllegalTransactionStateException IllegalTransactionStateException
public IllegalTransactionStateException(String msg)
From source file:org.grails.datastore.mapping.redis.RedisTransaction.java
public void commit() { if (rollbackCalled) { throw new IllegalTransactionStateException( "Cannot call commit after rollback. Start another transaction first!"); }//www . j a v a 2 s .co m try { /*final Object[] objects =*/ redisTemplate.exec(); commitCalled = true; } catch (Exception e) { throw new TransactionSystemException( "Exception occurred committing back Redis transaction: " + e.getMessage()); } }
From source file:org.grails.datastore.mapping.riak.RiakTransaction.java
public void commit() { if (rolledBack) { throw new IllegalTransactionStateException("This transaction has already been rolled back!"); }/* w w w . j a va 2 s . com*/ committed = true; }
From source file:org.grails.datastore.mapping.riak.RiakTransaction.java
public void rollback() { if (committed) { throw new IllegalTransactionStateException("This transaction has already been committed!"); }//from w w w. java 2s . c om rolledBack = true; }
From source file:com.vladmihalcea.concurrent.aop.OptimisticConcurrencyControlAspect.java
private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable { int times = retryAnnotation.times(); Class<? extends Throwable>[] retryOn = retryAnnotation.on(); Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!"); Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!"); if (retryAnnotation.failInTransaction() && TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalTransactionStateException( "You shouldn't retry an operation from withing an existing Transaction." + "This is because we can't retry if the current Transaction was already rollbacked!"); }/*from ww w . j a va 2s . co m*/ LOGGER.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn)); return tryProceeding(pjp, times, retryOn); }
From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java
@Override protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { // Validation on transaction argument ----------------------------------------------------- /* transaction Object should be either * null/* w w w. j a v a2 s.co m*/ * either TransactionDefinition.PROPAGATION_REQUIRED, * TransactionDefinition.PROPAGATION_REQUIRES_NEW or TransactionDefinition.PROPAGATION_NESTED * active GlobalTransactoin object (one before current one) * either TransactionDefinition.PROPAGATION_REQUIRES_NEW or * TransactionDefinition.PROPAGATION_NESTED * if TransactionDefinition.PROPAGATION_NESTED case, then * useSavepointForNestedTransaction method returns false */ GlobalTransaction gtx = (GlobalTransaction) transaction; Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx); GlobalTransaction currentGtx = Datastore.getCurrentGlobalTransaction(); Set<GlobalTransactionState> currentGtxStateSet = getGlobalTransactionStates(currentGtx); if (!currentGtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: getCurrentGlobalTransaction method of Slim3 Datastore " + "returned inactive GlobalTransaction instance (ID:%3$s). Expected it to return " + "active GlobalTransaction instance as new GlobalTransaction instance has begun " + "by newTransactionStatus method.", (currentGtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? currentGtx.getId() : "null"))); } String gtxIdStr = (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"); if (gtxIdStr.equals(currentGtx.getId())) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: the transaction Object argument refers to current " + "active GlobalTransaction instance (ID:%1$s). Expected it not to refer to " + "current active GlobalTransaction instance rather refer to the GlobalTransaction " + "instance available before newTransaction method execution or hold null value.", gtxIdStr)); } if (!gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { switch (definition.getPropagationBehavior()) { case TransactionDefinition.PROPAGATION_REQUIRED: case TransactionDefinition.PROPAGATION_REQUIRES_NEW: case TransactionDefinition.PROPAGATION_NESTED: break; default: throw new IllegalTransactionStateException(String.format( "Unexpected system state: found that the %1$s propagation behavior (%2$d) was " + "specified when the transaction Object argument holds null value. Expected " + "propagation behavior to be either PROPAGATION_REQUIRED (%3$d), " + "PROPAGATION_REQUIRES_NEW (%4$d) or PROPAGATION_NESTED (%5$d) when the " + "transaction Object argument holds null value.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.PROPAGATION_REQUIRES_NEW, TransactionDefinition.PROPAGATION_NESTED)); } // switch } else if (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)) { switch (definition.getPropagationBehavior()) { case TransactionDefinition.PROPAGATION_REQUIRES_NEW: case TransactionDefinition.PROPAGATION_NESTED: break; default: throw new IllegalTransactionStateException(String.format( "Unexpected system state: found that the %1$s propagation behavior (%2$d) was " + "specified when the transaction Object argument holds active " + "GlobalTransaction instance (ID:%3$s). Expected propagation behavior to be " + "either PROPAGATION_REQUIRES_NEW (%4$d) or PROPAGATION_NESTED (%5$d) when " + "the transaction Object argument holds active GlobalTransaction instance.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), gtx.getId(), TransactionDefinition.PROPAGATION_REQUIRES_NEW, TransactionDefinition.PROPAGATION_NESTED)); } // switch } else { throw new IllegalTransactionStateException( String.format("Unexpected system state: the transaction Object argument holds inactive " + "GlobalTransaction instance (ID:%1$s). Expected it to hold either null or " + "active GlobalTransaction instance.", gtx.getId())); } // ---------------------------------------------------------------------------------------- }
From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java
@Override protected void doCommit(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException { GlobalTransaction gtx = (GlobalTransaction) defaultTransactionStatus.getTransaction(); Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx); // Sanity check on precondition ----------------------------------------------------------- /*//www .j a v a2 s . co m * This will be called at outermost transaction boundary (defaultTransactionStatus.isNewTransaction() * will return true). * When defaultTransactionStatus has been set for roll back, roll back doesn't need to be handled * within doCommit method. * When either defaultTransactionStatus's isGlobalRollbackOnly and shouldCommitOnGlobalRollbackOnly * returns true or its isLocalRollbackOnly returns true, then logic flow won't reach here and * roll back should have been performed by doRollback. */ if (defaultTransactionStatus.isRollbackOnly()) { throw new TransactionSystemException(String.format( "Unexpected system state: the transaction for the GlobalTransaction " + "instance (ID:%1$s) has been marked as roll-back only " + "(LocalRollbackOnly:%2$b, GlobalRollbackOnly:%3$b).", (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"), defaultTransactionStatus.isLocalRollbackOnly(), defaultTransactionStatus.isGlobalRollbackOnly())); } if (!defaultTransactionStatus.isNewTransaction()) { throw new TransactionSystemException(String.format( "Unexpected system state: attempted to commit from participation of an existing " + "transaction (of which GlobalTransacion Id is %1$s).", (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } // ---------------------------------------------------------------------------------------- // Sanity check on gtx GlobalTransaction instance ----------------------------------------- if (!gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)) { String message; if (!gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { message = "Unexpected system state: The GlobalTransaction object passed as the transaction " + "Object argument is null."; } else { message = String .format("Unexpected system state: The GlobalTransaction instance (ID:%1$s) passed as the " + "transaction Object argument is inactive.", gtx.getId()); } throw new IllegalTransactionStateException(message); } if (!gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) { /* The one possible case of the control flow reaching here is that GlobalTransaction object * is manually instantiated at the outside of Spring transaction framework, and it is left active. * * In nesting global transaction, easy to yield ConcurrentModificationException without care. * Nesting global transaction should be less necessary, since Slim3 isolate between (Global) * transactions and cannot change that isolation setting, and this uses Slim3 GlobalTransaction, * as GAE/J datastore API is not provide suspend feature as well. */ GlobalTransaction currentGtx = Datastore.getCurrentGlobalTransaction(); if (logger.isWarnEnabled()) { logger.warn(String.format( "Though active GlobalTransaction instance (ID:%1$s) is passed as transaction " + "Object argument, it's not current GlobalTransaction instance (ID:%2$s).", gtx.getId(), ((currentGtx instanceof GlobalTransaction) ? currentGtx.getId() : "null"))); } } // ---------------------------------------------------------------------------------------- boolean exceptionUp = false; try { gtx.commit(); slim3GtxObjMapThreadLocal.get().remove(gtx.getId()); if (logger.isInfoEnabled()) { logger.info(String.format("Slim3 GlobalTransaction (ID:%1$s) committed.", gtx.getId())); } } catch (Throwable throwable) { /* Set rollback only flag so calling processRollback method to roll back will occur at * commit method by AnnotationTransactionAspect, even for other exceptions than ones specified * for rollbackFor or rollbackForClassname attributes of @Transactional annotation. */ defaultTransactionStatus.setRollbackOnly(); exceptionUp = true; String message = String.format("Slim3 GlobalTransaction (ID:%1$s) failed on commit.", gtx.getId()); if ((throwable instanceof ConcurrentModificationException) || (throwable instanceof DeadlineExceededException)) { /* Slim3 should have already rolled back automatically on either * ConcurrentModificationException or DeadlineExceededException. * * About DeadlineExceededException case: It seems like no harm * will be made even active global transaction at * DeadlineExceededException case left behind. As looking * briefly through the Slim3 source codes, the locks by that * global transaction seems to be released while it rolls back * by DatastoreFilter, and itself seem to be removed from * TheadLocal stack of the global transactions. So, it won't be * returned by such Datastore.getCurrentGlobalTransaction() * method. See at * http://groups.google.com/group/slim3-user/browse_frm * /thread/e3d47d8f28c8e8d3 * /9e43553f3b56d1f2?tvc=1#9e43553f3b56d1f2 If it turns out * opposite, then that active global transaction can be cleared * some how at here for DeadlineExceededException case. */ message = String.format("Slim3 GlobalTransaction (ID:%1$s) failed on commit. %n" + "Slim3 must have already rolled back automatically behind " + "Spring transaction framework.", gtx.getId()); } throw new TransactionSystemException(message, throwable); } finally { if (gtx.isActive()) { if (exceptionUp) { if (logger.isInfoEnabled()) { logger.info(String.format( "GlobalTransaction instance (ID:%1$s) remains active after exception " + "durring commit attempt. That GlobalTransaction instance has not " + "been removed yet from slim3GtxObjMapThreadLocal member field.", gtx.getId(), this.getClass().getName())); } } else { if (logger.isWarnEnabled()) { logger.warn(String.format( "Unexpected system state: GlobalTransaction instance (ID:%1$s) " + "remains active even after commit attempt. That GlobalTransaction " + "instance was removed from slim3GtxObjMapThreadLocal member field.", gtx.getId(), this.getClass().getName())); } } } } }
From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java
protected boolean validateCurrentActiveGlobalTransactionToParticipate(final GlobalTransaction gtx, final TransactionDefinition definition) throws IllegalTransactionStateException { Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx); if ((!gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)) || (!gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction))) { throw new IllegalTransactionStateException(String.format( "Though %1$s propergation behavior (%2$d) was specified, " + "the GlobalTransaction instance (ID:%3$s) (what has been begun this " + "transaction before) is %4$s %5$s one. Expected it to be current active " + "one.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), gtx.getId(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"))); }/* w w w .j a v a 2s .c o m*/ return true; }
From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java
@Override protected DefaultTransactionStatus newTransactionStatus(TransactionDefinition definition, Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, Object suspendedResources) { // Validate isolation level and time-out settings validateIsolationLevel(definition.getIsolationLevel()); boolean actualNewSynchronization = newSynchronization && !TransactionSynchronizationManager.isSynchronizationActive(); GlobalTransaction gtx = (GlobalTransaction) transaction; Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx); GlobalTransaction newGtx;//from w ww . ja v a 2 s . c om switch (definition.getPropagationBehavior()) { case TransactionAttribute.PROPAGATION_NESTED: if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { if (!isNestedTransactionAllowed()) { throw new TransactionSystemException(String.format( "Unexpected system state: the value of nestedTransactionAllowed boolean " + "member field is false. It should have been checked early as true within " + "handleExistingTransaction method, in order to provide the nested " + "propagation behavior over the transaction (what has already begun " + "GlobalTransaction instance (ID:%1$s)) by nested begin and commit/rollback calls.", (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } if (logger.isInfoEnabled()) { logger.info(String.format( "Going to provide nested propagation behavior by nested begin and " + "commit/rollback calls on new GlobalTransaction instance over the " + "transaction what has already begun GlobalTransaction instance (ID:%1$s).", gtx.getId())); } } // Enter to below logic (for TransactionAttribute.PROPAGATION_REQUIRES_NEW case) case TransactionAttribute.PROPAGATION_REQUIRES_NEW: // Sanity check on system state if (!newTransaction) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: the value of newTransaction boolean member field " + "is false for %1$s propagation behavior (%2$d). Exptected it to be true.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior())); } // Sanity check on current GlobalTransaction instances if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { validateCurrentActiveGlobalTransactionToParticipate(gtx, definition); } // begin new GlobalTransaction newGtx = beginGlogalTransaction(gtx, gtxStateSet, definition); return new DefaultTransactionStatus(newGtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); case TransactionAttribute.PROPAGATION_REQUIRED: if (newTransaction) { // Sanity check on current GlobalTransaction instances if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: the value of newTransaction boolean member " + "field is true what direct to create new transaction for %1$s " + "propagation behavior (%2$d) though this transaction has already begun " + "the GlobalTransaction instance (ID:%3$s). Exptected it to be false and " + "participate to the existing transaction.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), gtx.getId())); } // begin new GlobalTransaction newGtx = beginGlogalTransaction(gtx, gtxStateSet, definition); return new DefaultTransactionStatus(newGtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } else { // Sanity check on current GlobalTransaction instances validateCurrentActiveGlobalTransactionToParticipate(gtx, definition); return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } case TransactionAttribute.PROPAGATION_NEVER: if (newTransaction && !gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } else { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), the value of " + "newTransaction boolean member field is expected to be true (actual: %3$b) " + "and transaction Object argument is expected to hold current active " + "GlobalTransaction instance (actual: %4$s %5$s one (ID:%6$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), newTransaction, (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } case TransactionAttribute.PROPAGATION_NOT_SUPPORTED: if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), the " + "transaction Object argument is expected to hold null value (actual: " + "%3$s %4$s GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), gtx.getId())); } // Create DeafultTransactionState with null transaction return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); case TransactionAttribute.PROPAGATION_SUPPORTS: if (newTransaction) { if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), when the " + "newTransaction is true, the transaction Object argument is expected to " + "hold null value (actual: %3$s %4$s GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), gtx.getId())); } return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } else { if (!gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) || !gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), when the " + "newTransaction is false, the transaction Object argument is expected to " + "hold current active GlobalTransaction instance (actual: %3$s %4$s " + "GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } case TransactionAttribute.PROPAGATION_MANDATORY: if (newTransaction || !gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) || !gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), the " + "newTransaction is expected to be false (actual: %3$b) and the transaction " + "Object argument is expected to hold current active GlobalTransaction " + "instance (actual: %3$s %4$s GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); default: throw new IllegalTransactionStateException(String.format( "Unknown propagation behavior (%1$d) is specified. Supported propagation " + "behaviors is either PROPAGATION_MANDATORY (%2$d), PROPAGATION_NESTED (%3$d), " + "PROPAGATION_NEVER (%4$d), PROPAGATION_NOT_SUPPORTED (%5$d), " + "PROPAGATION_REQUIRED (%6$d), PROPAGATION_REQUIRES_NEW (%7$d), " + "or PROPAGATION_SUPPORTS (%8$d).", definition.getPropagationBehavior(), TransactionAttribute.PROPAGATION_MANDATORY, TransactionAttribute.PROPAGATION_NESTED, TransactionAttribute.PROPAGATION_NEVER, TransactionAttribute.PROPAGATION_NOT_SUPPORTED, TransactionAttribute.PROPAGATION_REQUIRED, TransactionAttribute.PROPAGATION_REQUIRES_NEW, TransactionAttribute.PROPAGATION_SUPPORTS)); } // switch }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * This implementation handles propagation behavior. Delegates to * {@code doGetTransaction}, {@code isExistingTransaction} * and {@code doBegin}.//from ww w.j a va2s . co m * @see #doGetTransaction * @see #isExistingTransaction * @see #doBegin */ @Override public final Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition definition) throws TransactionException { // Use defaults if no transaction definition given. TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults()); return TransactionSynchronizationManager.currentTransaction().flatMap(synchronizationManager -> { Object transaction = doGetTransaction(synchronizationManager); // Cache debug flag to avoid repeated checks. boolean debugEnabled = logger.isDebugEnabled(); if (isExistingTransaction(transaction)) { // Existing transaction found -> check propagation behavior to find out how to behave. return handleExistingTransaction(synchronizationManager, def, transaction, debugEnabled); } // Check definition settings for new transaction. if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) { return Mono.error(new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout())); } // No existing transaction found -> check propagation behavior to find out how to proceed. if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) { return Mono.error(new IllegalTransactionStateException( "No existing transaction found for transaction marked with propagation 'mandatory'")); } else if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED || def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW || def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) { return TransactionContextManager.currentContext().map(TransactionSynchronizationManager::new) .flatMap(nestedSynchronizationManager -> suspend(nestedSynchronizationManager, null) .map(Optional::of).defaultIfEmpty(Optional.empty()).flatMap(suspendedResources -> { if (debugEnabled) { logger.debug("Creating new transaction with name [" + def.getName() + "]: " + def); } return Mono.defer(() -> { GenericReactiveTransaction status = newReactiveTransaction( nestedSynchronizationManager, def, transaction, true, debugEnabled, suspendedResources.orElse(null)); return doBegin(nestedSynchronizationManager, transaction, def) .doOnSuccess(ignore -> prepareSynchronization( nestedSynchronizationManager, status, def)) .thenReturn(status); }).onErrorResume(ErrorPredicates.RUNTIME_OR_ERROR, ex -> resume(nestedSynchronizationManager, null, suspendedResources.orElse(null)).then(Mono.error(ex))); })); } else { // Create "empty" transaction: no actual transaction, but potentially synchronization. if (def.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) { logger.warn("Custom isolation level specified but no actual transaction initiated; " + "isolation level will effectively be ignored: " + def); } return Mono.just( prepareReactiveTransaction(synchronizationManager, def, null, true, debugEnabled, null)); } }); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Create a ReactiveTransaction for an existing transaction. *///from www. ja va2s. c om private Mono<ReactiveTransaction> handleExistingTransaction( TransactionSynchronizationManager synchronizationManager, TransactionDefinition definition, Object transaction, boolean debugEnabled) throws TransactionException { if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) { return Mono.error(new IllegalTransactionStateException( "Existing transaction found for transaction marked with propagation 'never'")); } if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) { if (debugEnabled) { logger.debug("Suspending current transaction"); } Mono<SuspendedResourcesHolder> suspend = suspend(synchronizationManager, transaction); return suspend .map(suspendedResources -> prepareReactiveTransaction(synchronizationManager, definition, null, false, debugEnabled, suspendedResources)) // .switchIfEmpty(Mono.fromSupplier(() -> prepareReactiveTransaction(synchronizationManager, definition, null, false, debugEnabled, null))) .cast(ReactiveTransaction.class); } if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) { if (debugEnabled) { logger.debug("Suspending current transaction, creating new transaction with name [" + definition.getName() + "]"); } Mono<SuspendedResourcesHolder> suspendedResources = suspend(synchronizationManager, transaction); return suspendedResources.flatMap(suspendedResourcesHolder -> { GenericReactiveTransaction status = newReactiveTransaction(synchronizationManager, definition, transaction, true, debugEnabled, suspendedResourcesHolder); return doBegin(synchronizationManager, transaction, definition) .doOnSuccess(ignore -> prepareSynchronization(synchronizationManager, status, definition)) .thenReturn(status).onErrorResume(ErrorPredicates.RUNTIME_OR_ERROR, beginEx -> resumeAfterBeginException(synchronizationManager, transaction, suspendedResourcesHolder, beginEx).then(Mono.error(beginEx))); }); } if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) { if (debugEnabled) { logger.debug("Creating nested transaction with name [" + definition.getName() + "]"); } // Nested transaction through nested begin and commit/rollback calls. GenericReactiveTransaction status = newReactiveTransaction(synchronizationManager, definition, transaction, true, debugEnabled, null); return doBegin(synchronizationManager, transaction, definition) .doOnSuccess(ignore -> prepareSynchronization(synchronizationManager, status, definition)) .thenReturn(status); } // Assumably PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED. if (debugEnabled) { logger.debug("Participating in existing transaction"); } return Mono.just(prepareReactiveTransaction(synchronizationManager, definition, transaction, false, debugEnabled, null)); }