예제 #1
0
        /// <summary>
        /// Execute a ADO.NET operation on a command object using a generic interface based callback.
        /// Direct use of this method is not recommended.
        /// </summary>
        /// <typeparam name="T">return type</typeparam>
        /// <param name="action">the action to be executed</param>
        public T Execute <T>(IDataAdapterCallback <T> action)
        {
            IDbDataAdapter dataAdapter = null;

            try
            {
                DbProvider.OpenConnection();
                dataAdapter = DbProvider.CreateDataAddapter();
                dataAdapter.SelectCommand             = DbProvider.CreateCommand();
                dataAdapter.SelectCommand.Connection  = DbProvider.Connection;
                dataAdapter.SelectCommand.Transaction = DbProvider.Transaction;

                T result = default(T);
                result = action.DoInDataAdapter(dataAdapter);
                return(result);
            }
            catch (DataException)
            {
                throw;
            }
            catch (DbException e)
            {
                throw new DataException($"Failed to execute a command cllback: {e.Message}", e);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionUtils.DisposeDataAdapterCommands(dataAdapter);
                ConnectionUtils.ClosseConnection(DbProvider);
            }
        }
예제 #2
0
        /// <summary>
        /// Execute ADO.NET operations on a IDbDataAdapter object using an interface based callback.
        /// </summary>
        /// <remarks>This allows for implementing abritrary data access operations
        /// on a single DataAdapter within Spring's managed ADO.NET environment.
        /// </remarks>
        /// <param name="dataAdapterCallback">The data adapter callback.</param>
        /// <returns>A result object returned by the callback or null</returns>
        public virtual object Execute(IDataAdapterCallback dataAdapterCallback)
        {
            ConnectionTxPair connectionTxPairToUse = GetConnectionTxPair(DbProvider);
            IDbDataAdapter dataAdapter = null;
            try
            {
                dataAdapter = DbProvider.CreateDataAdapter();
                //TODO row updated event handling...
                dataAdapter.SelectCommand = DbProvider.CreateCommand();
                dataAdapter.SelectCommand.Connection = connectionTxPairToUse.Connection;
                //TODO register for warnings on connection.
                dataAdapter.SelectCommand.Transaction = connectionTxPairToUse.Transaction;
                ApplyCommandSettings(dataAdapter.SelectCommand);
                object result = dataAdapterCallback.DoInDataAdapter(dataAdapter);
                return result;

            }
            catch (Exception)
            {
                DisposeDataAdapterCommands(dataAdapter);
                //TODO set dataAdapter command's = null; ?
                //TODO exception translation? different hierarchy for data set operations.
                DisposeConnection(connectionTxPairToUse.Connection, DbProvider);
                connectionTxPairToUse.Connection = null;
                throw;
            }
            finally
            {
                DisposeDataAdapterCommands(dataAdapter);
                DisposeConnection(connectionTxPairToUse.Connection, DbProvider);
            }


        }