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