private static T Execute <T>(WrappedConnection wrappedConnection, string sql, Func <IDbCommand, T> query) { Check.NotNull(wrappedConnection, nameof(wrappedConnection)); Check.NotNullOrEmpty(sql, nameof(sql)); Check.NotNull(query, nameof(query)); bool wasClosed = wrappedConnection.DbConnection.State == ConnectionState.Closed; try { if (wasClosed) { wrappedConnection.Open(); } using (IDbCommand cmd = wrappedConnection.DbConnection.CreateCommand()) { cmd.CommandText = sql; cmd.Transaction = wrappedConnection.CurrentTx; return(query(cmd)); } } catch (Exception ex) { throw new EvolveSqlException(sql, ex); } finally { if (wasClosed) { wrappedConnection.Close(); } } }
public override void Close() { if ((Transaction != null) && !Transaction.GetWrappedProperty <bool>("HasAlreadyBeenCommittedOrRolledBack") && (Transaction.Connection == null)) { Transaction.SetWrappedField("_connection", WrappedConnection); } _catalogHelpers = null; WrappedConnection.Close(); }
/// <inheritdoc cref="DbConnection.Close()" /> public override void Close() { if (Profiler == null || !Profiler.IsEnabled) { WrappedConnection.Close(); return; } Profiler.OnClosing(this); WrappedConnection.Close(); Profiler.OnClosed(this); }
public void When_opened_multiple_times_a_single_call_to_close_is_not_enought() { var cnn = new SqliteConnection("Data Source=:memory:"); using (var wrappedConnection = new WrappedConnection(cnn)) { wrappedConnection.Open(); wrappedConnection.Open(); wrappedConnection.Open(); wrappedConnection.Close(); Assert.True(wrappedConnection.DbConnection.State == ConnectionState.Open); } Assert.True(cnn.State == ConnectionState.Closed); }
public void When_opened_multiple_times_a_single_call_to_close_is_not_enought() { var cnn = _pgContainer.CreateDbConnection(); using (var wrappedConnection = new WrappedConnection(cnn)) { wrappedConnection.Open(); wrappedConnection.Open(); wrappedConnection.Open(); wrappedConnection.Close(); Assert.True(wrappedConnection.DbConnection.State == ConnectionState.Open); } Assert.True(cnn.State == ConnectionState.Closed); }