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

            MySqlRelationalTransaction startedTransaction = null;

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

                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
            {
                try
                {
                    startedTransaction?.Rollback();
                    startedTransaction?.Dispose();
                }
                catch
                {
                    // if the connection was lost, rollback command will fail.  prefer to throw original exception in that case
                }
                throw;
            }
            finally
            {
                connection.Close();
            }

            return(rowsAffected);
        }
Ejemplo n.º 2
0
 public bool MoveNext()
 {
     if (!_tryInitTransaction)
     {
         if (_queryContext.HasInclude && _queryContext.Connection.CurrentTransaction == null)
         {
             _transaction = _queryContext.Connection.BeginTransaction(IsolationLevel.RepeatableRead) as MySqlRelationalTransaction;
         }
         _tryInitTransaction = true;
     }
     if (!_enumerator.MoveNext())
     {
         _transaction?.Commit();
         return(false);
     }
     return(true);
 }
 public async Task <bool> MoveNext(CancellationToken cancellationToken)
 {
     if (!_tryInitTransaction)
     {
         if (_queryContext.HasInclude && _queryContext.Connection.CurrentTransaction == null)
         {
             _transaction = await _queryContext.Connection.BeginTransactionAsync(IsolationLevel.RepeatableRead, cancellationToken).ConfigureAwait(false) as MySqlRelationalTransaction;
         }
         _tryInitTransaction = true;
     }
     if (!await _enumerator.MoveNext(cancellationToken).ConfigureAwait(false))
     {
         if (_transaction != null)
         {
             await _transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
         }
         return(false);
     }
     return(true);
 }