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.jca.cci.core;
018
019import java.sql.SQLException;
020
021import javax.resource.ResourceException;
022import javax.resource.cci.Connection;
023import javax.resource.cci.ConnectionFactory;
024
025import org.springframework.dao.DataAccessException;
026import org.springframework.lang.Nullable;
027
028/**
029 * Generic callback interface for code that operates on a CCI Connection.
030 * Allows to execute any number of operations on a single Connection,
031 * using any type and number of Interaction.
032 *
033 * <p>This is particularly useful for delegating to existing data access code
034 * that expects a Connection to work on and throws ResourceException. For newly
035 * written code, it is strongly recommended to use CciTemplate's more specific
036 * {@code execute} variants.
037 *
038 * @author Thierry Templier
039 * @author Juergen Hoeller
040 * @since 1.2
041 * @param <T> the result type
042 * @see CciTemplate#execute(ConnectionCallback)
043 * @see CciTemplate#execute(javax.resource.cci.InteractionSpec, javax.resource.cci.Record)
044 * @see CciTemplate#execute(javax.resource.cci.InteractionSpec, RecordCreator, RecordExtractor)
045 */
046@FunctionalInterface
047public interface ConnectionCallback<T> {
048
049        /**
050         * Gets called by {@code CciTemplate.execute} with an active CCI Connection.
051         * Does not need to care about activating or closing the Connection, or handling
052         * transactions.
053         * <p>If called without a thread-bound CCI transaction (initiated by
054         * CciLocalTransactionManager), the code will simply get executed on the CCI
055         * Connection with its transactional semantics. If CciTemplate is configured
056         * to use a JTA-aware ConnectionFactory, the CCI Connection and thus the callback
057         * code will be transactional if a JTA transaction is active.
058         * <p>Allows for returning a result object created within the callback, i.e.
059         * a domain object or a collection of domain objects. Note that there's special
060         * support for single step actions: see the {@code CciTemplate.execute}
061         * variants. A thrown RuntimeException is treated as application exception:
062         * it gets propagated to the caller of the template.
063         * @param connection active CCI Connection
064         * @param connectionFactory the CCI ConnectionFactory that the Connection was
065         * created with (gives access to RecordFactory and ResourceAdapterMetaData)
066         * @return a result object, or {@code null} if none
067         * @throws ResourceException if thrown by a CCI method, to be auto-converted
068         * to a DataAccessException
069         * @throws SQLException if thrown by a ResultSet method, to be auto-converted
070         * to a DataAccessException
071         * @throws DataAccessException in case of custom exceptions
072         * @see javax.resource.cci.ConnectionFactory#getRecordFactory()
073         * @see javax.resource.cci.ConnectionFactory#getMetaData()
074         * @see CciTemplate#execute(javax.resource.cci.InteractionSpec, RecordCreator, RecordExtractor)
075         */
076        @Nullable
077        T doInConnection(Connection connection, ConnectionFactory connectionFactory)
078                        throws ResourceException, SQLException, DataAccessException;
079
080}