Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unnecessary connection creation in metrics #4573

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix unnecessary connection creation in metrics
  • Loading branch information
MaceWindu committed Jul 16, 2024
commit bf1165a986c8a2507d8d6f95a7c2490bec2ed630
18 changes: 9 additions & 9 deletions Source/LinqToDB/Async/AsyncDbTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ protected internal AsyncDbTransaction(DbTransaction transaction)

public virtual void Commit ()
{
using var a = ActivityService.Start(ActivityID.TransactionCommit)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
using var a = ActivityService.Start(ActivityID.TransactionCommit)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);
Transaction.Commit();
}

public virtual void Rollback()
{
using var a = ActivityService.Start(ActivityID.TransactionRollback)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
using var a = ActivityService.Start(ActivityID.TransactionRollback)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);
Transaction.Rollback();
}

public virtual Task CommitAsync(CancellationToken cancellationToken)
{
#if NET6_0_OR_GREATER
var a = ActivityService.StartAndConfigureAwait(ActivityID.TransactionCommitAsync)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
var a = ActivityService.StartAndConfigureAwait(ActivityID.TransactionCommitAsync)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);

if (a is null)
return Transaction.CommitAsync(cancellationToken);
Expand All @@ -56,7 +56,7 @@ static async Task CallAwaitUsing(AsyncDisposableWrapper activity, DbTransaction
await transaction.CommitAsync(token).ConfigureAwait(Common.Configuration.ContinueOnCapturedContext);
}
#else
using var a = ActivityService.Start(ActivityID.TransactionCommitAsync)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
using var a = ActivityService.Start(ActivityID.TransactionCommitAsync)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);

Transaction.Commit();
return Task.CompletedTask;
Expand All @@ -66,7 +66,7 @@ static async Task CallAwaitUsing(AsyncDisposableWrapper activity, DbTransaction
public virtual Task RollbackAsync(CancellationToken cancellationToken)
{
#if NET6_0_OR_GREATER
var a = ActivityService.StartAndConfigureAwait(ActivityID.TransactionRollbackAsync)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
var a = ActivityService.StartAndConfigureAwait(ActivityID.TransactionRollbackAsync)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);

if (a is null)
return Transaction.RollbackAsync(cancellationToken);
Expand All @@ -79,7 +79,7 @@ static async Task CallAwaitUsing(AsyncDisposableWrapper activity, DbTransaction
await transaction.RollbackAsync(token).ConfigureAwait(Common.Configuration.ContinueOnCapturedContext);
}
#else
using var a = ActivityService.Start(ActivityID.TransactionRollbackAsync)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
using var a = ActivityService.Start(ActivityID.TransactionRollbackAsync)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);

Transaction.Rollback();
return Task.CompletedTask;
Expand All @@ -90,7 +90,7 @@ static async Task CallAwaitUsing(AsyncDisposableWrapper activity, DbTransaction

public virtual void Dispose()
{
using var _ = ActivityService.Start(ActivityID.TransactionDispose)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
using var _ = ActivityService.Start(ActivityID.TransactionDispose)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);
Transaction.Dispose();
}

Expand All @@ -101,7 +101,7 @@ public virtual ValueTask DisposeAsync()
{
if (Transaction is IAsyncDisposable asyncDisposable)
{
var a = ActivityService.StartAndConfigureAwait(ActivityID.TransactionDisposeAsync)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
var a = ActivityService.StartAndConfigureAwait(ActivityID.TransactionDisposeAsync)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);

if (a is null)
return asyncDisposable.DisposeAsync();
Expand All @@ -115,7 +115,7 @@ static async ValueTask CallAwaitUsing(AsyncDisposableWrapper activity, IAsyncDis
}
}

using var _ = ActivityService.Start(ActivityID.TransactionDisposeAsync)?.AddQueryInfo(DataConnection, DataConnection?.Connection, null);
using var _ = ActivityService.Start(ActivityID.TransactionDisposeAsync)?.AddQueryInfo(DataConnection, DataConnection?.CurrentConnection, null);

Transaction.Dispose();
return default;
Expand Down
3 changes: 3 additions & 0 deletions Source/LinqToDB/Data/DataConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,14 @@ public TraceSwitch TraceSwitchConnection
IAsyncDbConnection? _connection;
Func<DataOptions, DbConnection>? _connectionFactory;

// TODO: V6 remove it or replace with non-creating access + creation method if such public APIs needed
/// <summary>
/// Gets underlying database connection, used by current connection object.
/// </summary>
public DbConnection Connection => EnsureConnection().Connection;

internal DbConnection? CurrentConnection => _connection;

internal IAsyncDbConnection EnsureConnection(bool connect = true)
{
CheckAndThrowOnDisposed();
Expand Down
Loading