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