001/*
002 * Copyright 2002-2017 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.PlatformTransactionManager;
021import org.springframework.transaction.TransactionDefinition;
022import org.springframework.transaction.TransactionException;
023
024/**
025 * Extension of the {@link org.springframework.transaction.PlatformTransactionManager}
026 * interface, exposing a method for executing a given callback within a transaction.
027 *
028 * <p>Implementors of this interface automatically express a preference for
029 * callbacks over programmatic {@code getTransaction}, {@code commit}
030 * and {@code rollback} calls. Calling code may check whether a given
031 * transaction manager implements this interface to choose to prepare a
032 * callback instead of explicit transaction demarcation control.
033 *
034 * <p>Spring's {@link TransactionTemplate} and
035 * {@link org.springframework.transaction.interceptor.TransactionInterceptor}
036 * detect and use this PlatformTransactionManager variant automatically.
037 *
038 * @author Juergen Hoeller
039 * @since 2.0
040 * @see TransactionTemplate
041 * @see org.springframework.transaction.interceptor.TransactionInterceptor
042 */
043public interface CallbackPreferringPlatformTransactionManager extends PlatformTransactionManager {
044
045        /**
046         * Execute the action specified by the given callback object within a transaction.
047         * <p>Allows for returning a result object created within the transaction, that is,
048         * a domain object or a collection of domain objects. A RuntimeException thrown
049         * by the callback is treated as a fatal exception that enforces a rollback.
050         * Such an exception gets propagated to the caller of the template.
051         * @param definition the definition for the transaction to wrap the callback in
052         * @param callback the callback object that specifies the transactional action
053         * @return a result object returned by the callback, or {@code null} if none
054         * @throws TransactionException in case of initialization, rollback, or system errors
055         * @throws RuntimeException if thrown by the TransactionCallback
056         */
057        @Nullable
058        <T> T execute(@Nullable TransactionDefinition definition, TransactionCallback<T> callback)
059                        throws TransactionException;
060
061}