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