001/*
002 * Copyright 2002-2018 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 org.springframework.lang.Nullable;
020import org.springframework.transaction.TransactionStatus;
021
022/**
023 * Callback interface for transactional code. Used with {@link TransactionTemplate}'s
024 * {@code execute} method, often as anonymous class within a method implementation.
025 *
026 * <p>Typically used to assemble various calls to transaction-unaware data access
027 * services into a higher-level service method with transaction demarcation. As an
028 * alternative, consider the use of declarative transaction demarcation (e.g. through
029 * Spring's {@link org.springframework.transaction.annotation.Transactional} annotation).
030 *
031 * @author Juergen Hoeller
032 * @since 17.03.2003
033 * @see TransactionTemplate
034 * @see CallbackPreferringPlatformTransactionManager
035 * @param <T> the result type
036 */
037@FunctionalInterface
038public interface TransactionCallback<T> {
039
040        /**
041         * Gets called by {@link TransactionTemplate#execute} within a transactional context.
042         * Does not need to care about transactions itself, although it can retrieve and
043         * influence the status of the current transaction via the given status object,
044         * e.g. setting rollback-only.
045         * <p>Allows for returning a result object created within the transaction, i.e. a
046         * domain object or a collection of domain objects. A RuntimeException thrown by the
047         * callback is treated as application exception that enforces a rollback. Any such
048         * exception will be propagated to the caller of the template, unless there is a
049         * problem rolling back, in which case a TransactionException will be thrown.
050         * @param status associated transaction status
051         * @return a result object, or {@code null}
052         * @see TransactionTemplate#execute
053         * @see CallbackPreferringPlatformTransactionManager#execute
054         */
055        @Nullable
056        T doInTransaction(TransactionStatus status);
057
058}