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