public virtual IReadOnlyList <HistoryRow> GetAppliedMigrations() { var rows = new List <HistoryRow>(); if (Exists()) { _connection.Open(); try { using (var reader = _executor.ExecuteReader(_connection, GetAppliedMigrationsSql)) { while (reader.Read()) { rows.Add(new HistoryRow(reader.GetString(0), reader.GetString(1))); } } } finally { _connection.Close(); } } return(rows); }
public virtual int Execute( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var rowsAffected = 0; connection.Open(); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return(rowsAffected); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public virtual void Dispose() { if (!_disposed) { // Defensively read RecordsAffected since MySQL provider can throw ObjectDisposedException var recordsAffected = -1; try { recordsAffected = _reader.RecordsAffected; } catch { } _reader.Dispose(); _command.Parameters.Clear(); _command.Dispose(); _connection.Close(); _logger.DataReaderDisposing( _connection, _command, _reader, _commandId, recordsAffected, _readCount, _startTime, _stopwatch.Elapsed); _disposed = true; } }
public virtual object Execute( IRelationalConnection connection, Func <object> action) { Check.NotNull(connection, nameof(connection)); // TODO Deal with suppressing transactions etc. var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open; if (!connectionWasOpen) { Logger.OpeningConnection(connection.ConnectionString); connection.Open(); } try { return(action()); } finally { if (!connectionWasOpen) { Logger.ClosingConnection(connection.ConnectionString); connection.Close(); } } }
public virtual async Task<int> ExecuteAsync( IEnumerable<ModificationCommandBatch> commandBatches, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { var rowsAffected = 0; await connection.OpenAsync(cancellationToken); IRelationalTransaction startedTransaction = null; try { if (connection.Transaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { await commandbatch.ExecuteAsync(connection, cancellationToken); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return rowsAffected; }
public virtual int Execute( IEnumerable<ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var rowsAffected = 0; connection.Open(); IRelationalTransaction startedTransaction = null; try { if (connection.Transaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return rowsAffected; }
public virtual async Task <object> ExecuteAsync( IRelationalConnection connection, Func <Task <object> > action, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(connection, nameof(connection)); var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open; if (!connectionWasOpen) { Logger.OpeningConnection(connection.ConnectionString); await connection.OpenAsync(cancellationToken); } try { return(await action()); } finally { if (!connectionWasOpen) { Logger.ClosingConnection(connection.ConnectionString); connection.Close(); } } }
public static async Task ExecuteNonQueryAsync( [NotNull] this IEnumerable <IRelationalCommand> commands, [NotNull] IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(commands, nameof(commands)); Check.NotNull(connection, nameof(connection)); await connection.OpenAsync(cancellationToken); try { foreach (var command in commands) { await command.ExecuteNonQueryAsync( connection, manageConnection : false, cancellationToken : cancellationToken); } } finally { connection.Close(); } }
public virtual async Task <int> ExecuteAsync( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { var rowsAffected = 0; await connection.OpenAsync(cancellationToken); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { await commandbatch.ExecuteAsync(connection, cancellationToken); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return(rowsAffected); }
public virtual async Task <object> ExecuteAsync( [NotNull] IRelationalConnection connection, [NotNull] Func <Task <object> > action, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(connection, nameof(connection)); // TODO Deal with suppressing transactions etc. var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open; if (!connectionWasOpen) { Logger.OpeningConnection(connection.ConnectionString); await connection.OpenAsync(cancellationToken).WithCurrentCulture(); } try { return(await action().WithCurrentCulture()); } finally { if (!connectionWasOpen) { Logger.ClosingConnection(connection.ConnectionString); connection.Close(); } } }
public virtual async Task ExecuteNonQueryAsync( IRelationalConnection connection, IEnumerable <RelationalCommand> relationalCommands, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(connection, nameof(connection)); Check.NotNull(relationalCommands, nameof(relationalCommands)); await connection.OpenAsync(cancellationToken); try { foreach (var command in relationalCommands) { await ExecuteAsync( connection, command, async c => await c.ExecuteNonQueryAsync(cancellationToken), cancellationToken); } } finally { connection.Close(); } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public virtual void Dispose() { if (!_disposed) { InterceptionResult?interceptionResult = null; try { _reader.Close(); // can throw interceptionResult = _logger?.DataReaderDisposing( _connection, _command, _reader, _commandId, _reader.RecordsAffected, _readCount, _startTime, _stopwatch.Elapsed); // can throw } finally { _disposed = true; if (interceptionResult == null) { _reader.Dispose(); _command.Parameters.Clear(); _command.Dispose(); _connection.Close(); } } } }
private static void CleanupCommand( DbCommand command, IRelationalConnection connection) { command.Parameters.Clear(); command.Dispose(); connection.Close(); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual void ExecuteNonQuery( IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); var userTransaction = connection.CurrentTransaction; if (userTransaction is not null && migrationCommands.Any(x => x.TransactionSuppressed)) { throw new NotSupportedException(RelationalStrings.TransactionSuppressedMigrationInUserTransaction); } using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) { connection.Open(); try { IDbContextTransaction?transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed && userTransaction is null) { transaction = connection.BeginTransaction(); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); transaction.Dispose(); transaction = null; } command.ExecuteNonQuery(connection); } transaction?.Commit(); } finally { transaction?.Dispose(); } } finally { connection.Close(); } } }
private void ClearTransaction() { _relationalConnection.UseTransaction(null); if (_connectionClosed) { return; } _connectionClosed = true; _relationalConnection.Close(); }
public virtual void Dispose() { if (!_disposed) { _reader.Dispose(); _command.Dispose(); _connection?.Close(); _disposed = true; } }
private void ClearTransaction() { Debug.Assert(_relationalConnection.CurrentTransaction == null || _relationalConnection.CurrentTransaction == this); _relationalConnection.UseTransaction(null); if (!_connectionClosed) { _connectionClosed = true; _relationalConnection.Close(); } }
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); }
public async Task <int> ExecuteAsync( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { var registrosAfetados = 0; await connection.OpenAsync(cancellationToken, false).ConfigureAwait(false); FirebirdSqlRelationalTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = await(connection as FirebirdSqlRelationalConnection).BeginTransactionAsync(cancellationToken).ConfigureAwait(false) as FirebirdSqlRelationalTransaction; } foreach (var commandbatch in commandBatches) { await commandbatch.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false); registrosAfetados += 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(registrosAfetados); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual async Task ExecuteNonQueryAsync( IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) { await connection.OpenAsync(cancellationToken); try { IDbContextTransaction transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed) { transaction = await connection.BeginTransactionAsync(cancellationToken); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); transaction.Dispose(); transaction = null; } await command.ExecuteNonQueryAsync(connection, cancellationToken : cancellationToken); } transaction?.Commit(); } finally { transaction?.Dispose(); } } finally { connection.Close(); } } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual async Task <int> ExecuteAsync( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { var rowsAffected = 0; await connection.OpenAsync(cancellationToken); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { await commandbatch.ExecuteAsync(connection, cancellationToken); // Fixed Issue #1: DataReader conflicted when added multiple entities try { lock (this) { var reader = (connection.DbConnection as MySqlConnection).Reader.LastOrDefault(); if (reader != null) { reader.Dispose(); (connection.DbConnection as MySqlConnection).Reader.Remove(reader); } } } catch { } rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return(rowsAffected); }
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); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual void ExecuteNonQuery( IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) { connection.Open(); try { IDbContextTransaction transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed) { transaction = connection.BeginTransaction(); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); transaction.Dispose(); transaction = null; } command.ExecuteNonQuery(connection); } transaction?.Commit(); } finally { transaction?.Dispose(); } } finally { connection.Close(); } } }
public async Task ExecuteNonQueryAsync(IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken = default) { connection.Open(); try { foreach (var command in migrationCommands) { await command.ExecuteNonQueryAsync(connection); } } finally { connection.Close(); } }
public override bool Exists() { try { return(_relationalConnection.Open()); } catch (Exception) { return(false); } finally { _relationalConnection.Close(); } }
public void ExecuteNonQuery(IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection) { connection.Open(); try { foreach (var command in migrationCommands) { command.ExecuteNonQuery(connection); } } finally { connection.Close(); } }
public int Execute( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var registrosAfetados = 0; connection.Open(); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); registrosAfetados += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); startedTransaction?.Dispose(); } catch (Exception ex) { ex.Message.ToString(); 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(registrosAfetados); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void ExecuteNonQuery( IEnumerable<MigrationCommand> migrationCommands, IRelationalConnection connection) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); connection.Open(); try { IDbContextTransaction transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed) { transaction = connection.BeginTransaction(); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); transaction.Dispose(); transaction = null; } command.ExecuteNonQuery(connection); } transaction?.Commit(); } finally { transaction?.Dispose(); } } finally { connection.Close(); } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual int Execute( IEnumerable<ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var rowsAffected = 0; connection.Open(); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); // Fixed Issue #1: DataReader conflicted when added multiple entities try { lock (this) { var reader = (connection.DbConnection as MySqlConnection).Reader.LastOrDefault(); if (reader != null) { reader.Dispose(); (connection.DbConnection as MySqlConnection).Reader.Remove(reader); } } } catch { } rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return rowsAffected; }
public int Execute(IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var recordAffecteds = 0; if (connection?.DbConnection?.State != System.Data.ConnectionState.Open) { connection.Open(); } IDbContextTransaction currentTransaction = null; try { if (connection.CurrentTransaction == null) { currentTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); recordAffecteds += commandbatch.ModificationCommands.Count; } currentTransaction?.Commit(); currentTransaction?.Dispose(); } catch (Exception ex) { try { currentTransaction?.Rollback(); currentTransaction?.Dispose(); } catch { // } throw ex; } finally { connection?.Close(); } return(recordAffecteds); }
protected virtual async Task <object> ExecuteAsync( IRelationalConnection connection, Func <Task <object> > action, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(connection, nameof(connection)); await connection.OpenAsync(cancellationToken); try { return(await action()); } finally { connection.Close(); } }
protected virtual object Execute( IRelationalConnection connection, Func <object> action) { Check.NotNull(connection, nameof(connection)); // TODO Deal with suppressing transactions etc. connection.Open(); try { return(action()); } finally { connection.Close(); } }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual int Execute( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var rowsAffected = 0; IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null && (connection as ITransactionEnlistmentManager)?.EnlistedTransaction == null && Transaction.Current == null && CurrentContext.Context.Database.AutoTransactionsEnabled) { startedTransaction = connection.BeginTransaction(); } else { connection.Open(); } foreach (var batch in commandBatches) { batch.Execute(connection); rowsAffected += batch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { if (startedTransaction != null) { startedTransaction.Dispose(); } else { connection.Close(); } } return(rowsAffected); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public virtual void Dispose() { if (!_disposed) { _logger.DataReaderDisposing( _connection, _command, _reader, _commandId, _reader.RecordsAffected, _readCount, _startTime, _stopwatch.Elapsed); _reader.Dispose(); _command.Dispose(); _connection?.Close(); _disposed = true; } }
public virtual int Execute( IEnumerable<ModificationCommandBatch> commandBatches, IRelationalConnection connection) { Check.NotNull(commandBatches, nameof(commandBatches)); Check.NotNull(connection, nameof(connection)); var rowsAffected = 0; connection.Open(); RelationalTransaction startedTransaction = null; try { if (connection.Transaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { rowsAffected += commandbatch.Execute( connection.Transaction, _typeMapper, _context, Logger); } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return rowsAffected; }
public virtual async Task<int> ExecuteAsync( IEnumerable<ModificationCommandBatch> commandBatches, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(commandBatches, nameof(commandBatches)); Check.NotNull(connection, nameof(connection)); var rowsAffected = 0; await connection.OpenAsync(cancellationToken); RelationalTransaction startedTransaction = null; try { if (connection.Transaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { rowsAffected += await commandbatch.ExecuteAsync( connection.Transaction, _typeMapper, _context, Logger, cancellationToken); } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return rowsAffected; }
protected virtual object Execute( IRelationalConnection connection, Func<object> action) { Check.NotNull(connection, nameof(connection)); // TODO Deal with suppressing transactions etc. connection.Open(); try { return action(); } finally { connection.Close(); } }
protected virtual async Task<object> ExecuteAsync( IRelationalConnection connection, Func<Task<object>> action, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(connection, nameof(connection)); await connection.OpenAsync(cancellationToken); try { return await action(); } finally { connection.Close(); } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual async Task ExecuteNonQueryAsync( IEnumerable<MigrationCommand> migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); await connection.OpenAsync(cancellationToken); try { IDbContextTransaction transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed) { transaction = await connection.BeginTransactionAsync(cancellationToken); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); transaction.Dispose(); transaction = null; } await command.ExecuteNonQueryAsync(connection, cancellationToken: cancellationToken); } transaction?.Commit(); } finally { transaction?.Dispose(); } } finally { connection.Close(); } }
public virtual object Execute( IRelationalConnection connection, Func<object> action) { Check.NotNull(connection, nameof(connection)); // TODO Deal with suppressing transactions etc. var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open; if (!connectionWasOpen) { Logger.OpeningConnection(connection.ConnectionString); connection.Open(); } try { return action(); } finally { if (!connectionWasOpen) { Logger.ClosingConnection(connection.ConnectionString); connection.Close(); } } }
public virtual async Task<object> ExecuteAsync( IRelationalConnection connection, Func<Task<object>> action, CancellationToken cancellationToken = default(CancellationToken)) { Check.NotNull(connection, nameof(connection)); var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open; if (!connectionWasOpen) { Logger.OpeningConnection(connection.ConnectionString); await connection.OpenAsync(cancellationToken); } try { return await action(); } finally { if (!connectionWasOpen) { Logger.ClosingConnection(connection.ConnectionString); connection.Close(); } } }