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.jdbc.core; 018 019import java.sql.CallableStatement; 020import java.sql.SQLException; 021 022import org.springframework.dao.DataAccessException; 023import org.springframework.lang.Nullable; 024 025/** 026 * Generic callback interface for code that operates on a CallableStatement. 027 * Allows to execute any number of operations on a single CallableStatement, 028 * for example a single execute call or repeated execute calls with varying 029 * parameters. 030 * 031 * <p>Used internally by JdbcTemplate, but also useful for application code. 032 * Note that the passed-in CallableStatement can have been created by the 033 * framework or by a custom CallableStatementCreator. However, the latter is 034 * hardly ever necessary, as most custom callback actions will perform updates 035 * in which case a standard CallableStatement is fine. Custom actions will 036 * always set parameter values themselves, so that CallableStatementCreator 037 * capability is not needed either. 038 * 039 * @author Juergen Hoeller 040 * @since 16.03.2004 041 * @param <T> the result type 042 * @see JdbcTemplate#execute(String, CallableStatementCallback) 043 * @see JdbcTemplate#execute(CallableStatementCreator, CallableStatementCallback) 044 */ 045@FunctionalInterface 046public interface CallableStatementCallback<T> { 047 048 /** 049 * Gets called by {@code JdbcTemplate.execute} with an active JDBC 050 * CallableStatement. Does not need to care about closing the Statement 051 * or the Connection, or about handling transactions: this will all be 052 * handled by Spring's JdbcTemplate. 053 * 054 * <p><b>NOTE:</b> Any ResultSets opened should be closed in finally blocks 055 * within the callback implementation. Spring will close the Statement 056 * object after the callback returned, but this does not necessarily imply 057 * that the ResultSet resources will be closed: the Statement objects might 058 * get pooled by the connection pool, with {@code close} calls only 059 * returning the object to the pool but not physically closing the resources. 060 * 061 * <p>If called without a thread-bound JDBC transaction (initiated by 062 * DataSourceTransactionManager), the code will simply get executed on the 063 * JDBC connection with its transactional semantics. If JdbcTemplate is 064 * configured to use a JTA-aware DataSource, the JDBC connection and thus 065 * the callback code will be transactional if a JTA transaction is active. 066 * 067 * <p>Allows for returning a result object created within the callback, i.e. 068 * a domain object or a collection of domain objects. A thrown RuntimeException 069 * is treated as application exception: it gets propagated to the caller of 070 * the template. 071 * 072 * @param cs active JDBC CallableStatement 073 * @return a result object, or {@code null} if none 074 * @throws SQLException if thrown by a JDBC method, to be auto-converted 075 * into a DataAccessException by an SQLExceptionTranslator 076 * @throws DataAccessException in case of custom exceptions 077 */ 078 @Nullable 079 T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException; 080 081}