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.Connection;
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 JDBC Connection.
027 * Allows to execute any number of operations on a single Connection,
028 * using any type and number of Statements.
029 *
030 * <p>This is particularly useful for delegating to existing data access code
031 * that expects a Connection to work on and throws SQLException. For newly
032 * written code, it is strongly recommended to use JdbcTemplate's more specific
033 * operations, for example a {@code query} or {@code update} variant.
034 *
035 * @author Juergen Hoeller
036 * @since 1.1.3
037 * @param <T> the result type
038 * @see JdbcTemplate#execute(ConnectionCallback)
039 * @see JdbcTemplate#query
040 * @see JdbcTemplate#update
041 */
042@FunctionalInterface
043public interface ConnectionCallback<T> {
044
045        /**
046         * Gets called by {@code JdbcTemplate.execute} with an active JDBC
047         * Connection. Does not need to care about activating or closing the
048         * Connection, or handling transactions.
049         * <p>If called without a thread-bound JDBC transaction (initiated by
050         * DataSourceTransactionManager), the code will simply get executed on the
051         * JDBC connection with its transactional semantics. If JdbcTemplate is
052         * configured to use a JTA-aware DataSource, the JDBC Connection and thus
053         * the callback code will be transactional if a JTA transaction is active.
054         * <p>Allows for returning a result object created within the callback, i.e.
055         * a domain object or a collection of domain objects. Note that there's special
056         * support for single step actions: see {@code JdbcTemplate.queryForObject}
057         * etc. A thrown RuntimeException is treated as application exception:
058         * it gets propagated to the caller of the template.
059         * @param con active JDBC Connection
060         * @return a result object, or {@code null} if none
061         * @throws SQLException if thrown by a JDBC method, to be auto-converted
062         * to a DataAccessException by an SQLExceptionTranslator
063         * @throws DataAccessException in case of custom exceptions
064         * @see JdbcTemplate#queryForObject(String, Class)
065         * @see JdbcTemplate#queryForRowSet(String)
066         */
067        @Nullable
068        T doInConnection(Connection con) throws SQLException, DataAccessException;
069
070}