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