001/*
002 * Copyright 2002-2019 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.support;
018
019import java.util.function.Consumer;
020
021import org.springframework.lang.Nullable;
022import org.springframework.transaction.TransactionException;
023import org.springframework.transaction.TransactionStatus;
024
025/**
026 * Interface specifying basic transaction execution operations.
027 * Implemented by {@link TransactionTemplate}. Not often used directly,
028 * but a useful option to enhance testability, as it can easily be
029 * mocked or stubbed.
030 *
031 * @author Juergen Hoeller
032 * @since 2.0.4
033 */
034public interface TransactionOperations {
035
036        /**
037         * Execute the action specified by the given callback object within a transaction.
038         * <p>Allows for returning a result object created within the transaction, that is,
039         * a domain object or a collection of domain objects. A RuntimeException thrown
040         * by the callback is treated as a fatal exception that enforces a rollback.
041         * Such an exception gets propagated to the caller of the template.
042         * @param action the callback object that specifies the transactional action
043         * @return a result object returned by the callback, or {@code null} if none
044         * @throws TransactionException in case of initialization, rollback, or system errors
045         * @throws RuntimeException if thrown by the TransactionCallback
046         * @see #executeWithoutResult(Consumer)
047         */
048        @Nullable
049        <T> T execute(TransactionCallback<T> action) throws TransactionException;
050
051        /**
052         * Execute the action specified by the given {@link Runnable} within a transaction.
053         * <p>If you need to return an object from the callback or access the
054         * {@link org.springframework.transaction.TransactionStatus} from within the callback,
055         * use {@link #execute(TransactionCallback)} instead.
056         * <p>This variant is analogous to using a {@link TransactionCallbackWithoutResult}
057         * but with a simplified signature for common cases - and conveniently usable with
058         * Java 8 lambda expressions.
059         * @param action the Runnable that specifies the transactional action
060         * @throws TransactionException in case of initialization, rollback, or system errors
061         * @throws RuntimeException if thrown by the Runnable
062         * @since 5.2
063         * @see #execute(TransactionCallback)
064         * @see TransactionCallbackWithoutResult
065         */
066        default void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException {
067                execute(status -> {
068                        action.accept(status);
069                        return null;
070                });
071        }
072
073
074        /**
075         * Return an implementation of the {@code TransactionOperations} interface which
076         * executes a given {@link TransactionCallback} without an actual transaction.
077         * <p>Useful for testing: The behavior is equivalent to running with a
078         * transaction manager with no actual transaction (PROPAGATION_SUPPORTS)
079         * and no synchronization (SYNCHRONIZATION_NEVER).
080         * <p>For a {@link TransactionOperations} implementation with actual
081         * transaction processing, use {@link TransactionTemplate} with an appropriate
082         * {@link org.springframework.transaction.PlatformTransactionManager}.
083         * @since 5.2
084         * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_SUPPORTS
085         * @see AbstractPlatformTransactionManager#SYNCHRONIZATION_NEVER
086         * @see TransactionTemplate
087         */
088        static TransactionOperations withoutTransaction() {
089                return WithoutTransactionOperations.INSTANCE;
090        }
091
092}