public async Task <int> ExecuteAsync(
            IEnumerable <ModificationCommandBatch> commandBatches,
            IRelationalConnection connection,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var rowsAffected = 0;
            await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

            FbRelationalTransaction startedTransaction = null;

            try
            {
                if (connection.CurrentTransaction == null)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    startedTransaction = await(connection as FbRelationalConnection).BeginTransactionAsync(cancellationToken).ConfigureAwait(false) as FbRelationalTransaction;
                }

                foreach (var commandbatch in commandBatches)
                {
                    await commandbatch.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                    rowsAffected += commandbatch.ModificationCommands.Count;
                }

                if (startedTransaction != null)
                {
                    await startedTransaction.CommitAsync(cancellationToken).ConfigureAwait(false);
                }
                startedTransaction?.Dispose();
            }
            catch (Exception err)
            {
                try
                {
                    startedTransaction?.Rollback();
                    startedTransaction?.Dispose();
                }
                catch
                {
                    // if the connection was lost, rollback command will fail.  prefer to throw original exception in that case
                }
                throw err;
            }
            finally
            {
                connection.Close();
            }

            return(rowsAffected);
        }
Esempio n. 2
0
        public async Task <int> ExecuteAsync(
            IEnumerable <ModificationCommandBatch> commandBatches,
            IRelationalConnection connection,
            CancellationToken cancellationToken = default)
        {
            var RowsAffecteds = 0;

            FbRelationalTransaction currentTransaction = null;

            try
            {
                if (connection?.DbConnection?.State != System.Data.ConnectionState.Open)
                {
                    await connection.OpenAsync(cancellationToken, false).ConfigureAwait(false);
                }

                if (connection.CurrentTransaction == null)
                {
                    currentTransaction =
                        await((FbRelationalConnection)connection)
                        .BeginTransactionAsync(cancellationToken).ConfigureAwait(false) as FbRelationalTransaction;
                }

                foreach (var commandbatch in commandBatches)
                {
                    await commandbatch.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                    RowsAffecteds += commandbatch.ModificationCommands.Count;
                }

                if (currentTransaction != null)
                {
                    await currentTransaction.CommitAsync(cancellationToken).ConfigureAwait(false);
                }

                currentTransaction?.Dispose();
            }
            catch (Exception err)
            {
                currentTransaction?.Rollback();
                currentTransaction?.Dispose();

                throw err;
            }
            finally
            {
                connection?.Close();
            }
            return(RowsAffecteds);
        }