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