Skip to content

Commit

Permalink
Fix memory leak when using transction to publish message. (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
yang-xiaodong committed Apr 6, 2021
1 parent b764930 commit 3ca1083
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 17 deletions.
4 changes: 1 addition & 3 deletions src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public void AddServices(IServiceCollection services)
services.AddSingleton<CapStorageMarkerService>();

services.AddSingleton<IDataStorage, MongoDBDataStorage>();
services.AddSingleton<IStorageInitializer, MongoDBStorageInitializer>();

services.AddTransient<ICapTransaction, MongoDBCapTransaction>();
services.AddSingleton<IStorageInitializer, MongoDBStorageInitializer>();

services.Configure(_configure);

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static IClientSessionHandle StartTransaction(this IMongoClient client,
ICapPublisher publisher, bool autoCommit = false)
{
var clientSessionHandle = client.StartSession();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<MongoDBCapTransaction>(publisher.ServiceProvider);
var capTrans = publisher.Transaction.Value.Begin(clientSessionHandle, autoCommit);
return new CapMongoDbClientSessionHandle(capTrans);
}
Expand Down
1 change: 0 additions & 1 deletion src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public void AddServices(IServiceCollection services)
services.AddSingleton<IDataStorage, MySqlDataStorage>();

services.TryAddSingleton<IStorageInitializer, MySqlStorageInitializer>();
services.AddTransient<ICapTransaction, MySqlCapTransaction>();

//Add MySqlOptions
services.Configure(_configure);
Expand Down
4 changes: 2 additions & 2 deletions src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static IDbContextTransaction BeginTransaction(this DatabaseFacade databas
ICapPublisher publisher, bool autoCommit = false)
{
var trans = database.BeginTransaction();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(publisher.ServiceProvider);
var capTrans = publisher.Transaction.Value.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans);
}
Expand All @@ -142,7 +142,7 @@ public static ICapTransaction BeginTransaction(this IDbConnection dbConnection,
}

var dbTransaction = dbConnection.BeginTransaction();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(publisher.ServiceProvider);
return publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void AddServices(IServiceCollection services)

services.AddSingleton<IDataStorage, PostgreSqlDataStorage>();
services.AddSingleton<IStorageInitializer, PostgreSqlStorageInitializer>();
services.AddTransient<ICapTransaction, PostgreSqlCapTransaction>();
}
}
}
4 changes: 2 additions & 2 deletions src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static ICapTransaction BeginTransaction(this IDbConnection dbConnection,
if (dbConnection.State == ConnectionState.Closed) dbConnection.Open();

var dbTransaction = dbConnection.BeginTransaction();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<PostgreSqlCapTransaction>(publisher.ServiceProvider);
return publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
}

Expand All @@ -139,7 +139,7 @@ public static IDbContextTransaction BeginTransaction(this DatabaseFacade databas
ICapPublisher publisher, bool autoCommit = false)
{
var trans = database.BeginTransaction();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<PostgreSqlCapTransaction>(publisher.ServiceProvider);
var capTrans = publisher.Transaction.Value.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void AddServices(IServiceCollection services)
services.AddSingleton<DiagnosticProcessorObserver>();
services.AddSingleton<IDataStorage, SqlServerDataStorage>();
services.AddSingleton<IStorageInitializer, SqlServerStorageInitializer>();
services.AddTransient<ICapTransaction, SqlServerCapTransaction>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IProcessingServer, DiagnosticRegister>());

services.Configure(_configure);
Expand Down
11 changes: 5 additions & 6 deletions src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ protected override void AddToSent(MediumMessage msg)
if (dbTransaction == null) throw new ArgumentNullException(nameof(DbTransaction));
}

var transactionKey = ((SqlConnection) dbTransaction.Connection).ClientConnectionId;
var transactionKey = ((SqlConnection)dbTransaction.Connection).ClientConnectionId;
if (_diagnosticProcessor.BufferList.TryGetValue(transactionKey, out var list))
{
list.Add(msg);
}
else
{
var msgList = new List<MediumMessage>(1) {msg};
var msgList = new List<MediumMessage>(1) { msg };
_diagnosticProcessor.BufferList.TryAdd(transactionKey, msgList);
}
}
Expand Down Expand Up @@ -163,11 +163,10 @@ public static IDbTransaction BeginTransaction(this IDbConnection dbConnection,
ICapPublisher publisher, bool autoCommit = false)
{
if (dbConnection.State == ConnectionState.Closed) dbConnection.Open();

var dbTransaction = dbConnection.BeginTransaction();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<SqlServerCapTransaction>(publisher.ServiceProvider);
var capTransaction = publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
return (IDbTransaction) capTransaction.DbTransaction;
return (IDbTransaction)capTransaction.DbTransaction;
}

/// <summary>
Expand All @@ -181,7 +180,7 @@ public static IDbContextTransaction BeginTransaction(this DatabaseFacade databas
ICapPublisher publisher, bool autoCommit = false)
{
var trans = database.BeginTransaction();
publisher.Transaction.Value = publisher.ServiceProvider.GetService<ICapTransaction>();
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<SqlServerCapTransaction>(publisher.ServiceProvider);
var capTrans = publisher.Transaction.Value.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans);
}
Expand Down

0 comments on commit 3ca1083

Please sign in to comment.