001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.transaction;
018
019import reactor.core.publisher.Mono;
020
021import org.springframework.lang.Nullable;
022
023/**
024 * This is the central interface in Spring's reactive transaction infrastructure.
025 * Applications can use this directly, but it is not primarily meant as an API:
026 * Typically, applications will work with either transactional operators or
027 * declarative transaction demarcation through AOP.
028 *
029 * @author Mark Paluch
030 * @author Juergen Hoeller
031 * @since 5.2
032 * @see org.springframework.transaction.reactive.TransactionalOperator
033 * @see org.springframework.transaction.interceptor.TransactionInterceptor
034 * @see org.springframework.transaction.PlatformTransactionManager
035 */
036public interface ReactiveTransactionManager extends TransactionManager {
037
038        /**
039         * Emit a currently active reactive transaction or create a new one, according to
040         * the specified propagation behavior.
041         * <p>Note that parameters like isolation level or timeout will only be applied
042         * to new transactions, and thus be ignored when participating in active ones.
043         * <p>Furthermore, not all transaction definition settings will be supported
044         * by every transaction manager: A proper transaction manager implementation
045         * should throw an exception when unsupported settings are encountered.
046         * <p>An exception to the above rule is the read-only flag, which should be
047         * ignored if no explicit read-only mode is supported. Essentially, the
048         * read-only flag is just a hint for potential optimization.
049         * @param definition the TransactionDefinition instance,
050         * describing propagation behavior, isolation level, timeout etc.
051         * @return transaction status object representing the new or current transaction
052         * @throws TransactionException in case of lookup, creation, or system errors
053         * @throws IllegalTransactionStateException if the given transaction definition
054         * cannot be executed (for example, if a currently active transaction is in
055         * conflict with the specified propagation behavior)
056         * @see TransactionDefinition#getPropagationBehavior
057         * @see TransactionDefinition#getIsolationLevel
058         * @see TransactionDefinition#getTimeout
059         * @see TransactionDefinition#isReadOnly
060         */
061        Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition definition)
062                        throws TransactionException;
063
064        /**
065         * Commit the given transaction, with regard to its status. If the transaction
066         * has been marked rollback-only programmatically, perform a rollback.
067         * <p>If the transaction wasn't a new one, omit the commit for proper
068         * participation in the surrounding transaction. If a previous transaction
069         * has been suspended to be able to create a new one, resume the previous
070         * transaction after committing the new one.
071         * <p>Note that when the commit call completes, no matter if normally or
072         * throwing an exception, the transaction must be fully completed and
073         * cleaned up. No rollback call should be expected in such a case.
074         * <p>If this method throws an exception other than a TransactionException,
075         * then some before-commit error caused the commit attempt to fail. For
076         * example, an O/R Mapping tool might have tried to flush changes to the
077         * database right before commit, with the resulting DataAccessException
078         * causing the transaction to fail. The original exception will be
079         * propagated to the caller of this commit method in such a case.
080         * @param transaction object returned by the {@code getTransaction} method
081         * @throws UnexpectedRollbackException in case of an unexpected rollback
082         * that the transaction coordinator initiated
083         * @throws HeuristicCompletionException in case of a transaction failure
084         * caused by a heuristic decision on the side of the transaction coordinator
085         * @throws TransactionSystemException in case of commit or system errors
086         * (typically caused by fundamental resource failures)
087         * @throws IllegalTransactionStateException if the given transaction
088         * is already completed (that is, committed or rolled back)
089         * @see ReactiveTransaction#setRollbackOnly
090         */
091        Mono<Void> commit(ReactiveTransaction transaction) throws TransactionException;
092
093        /**
094         * Perform a rollback of the given transaction.
095         * <p>If the transaction wasn't a new one, just set it rollback-only for proper
096         * participation in the surrounding transaction. If a previous transaction
097         * has been suspended to be able to create a new one, resume the previous
098         * transaction after rolling back the new one.
099         * <p><b>Do not call rollback on a transaction if commit threw an exception.</b>
100         * The transaction will already have been completed and cleaned up when commit
101         * returns, even in case of a commit exception. Consequently, a rollback call
102         * after commit failure will lead to an IllegalTransactionStateException.
103         * @param transaction object returned by the {@code getTransaction} method
104         * @throws TransactionSystemException in case of rollback or system errors
105         * (typically caused by fundamental resource failures)
106         * @throws IllegalTransactionStateException if the given transaction
107         * is already completed (that is, committed or rolled back)
108         */
109        Mono<Void> rollback(ReactiveTransaction transaction) throws TransactionException;
110
111}