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