Browse Source

Fix memory leak when using transction to publish message. (#816)

master
Savorboard 3 years ago
parent
commit
3ca108328b
8 changed files with 11 additions and 17 deletions
  1. +1
    -3
      src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs
  2. +1
    -1
      src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs
  3. +0
    -1
      src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs
  4. +2
    -2
      src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs
  5. +0
    -1
      src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs
  6. +2
    -2
      src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs
  7. +0
    -1
      src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs
  8. +5
    -6
      src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs

+ 1
- 3
src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs View File

@@ -25,9 +25,7 @@ namespace DotNetCore.CAP.MongoDB
services.AddSingleton<CapStorageMarkerService>(); services.AddSingleton<CapStorageMarkerService>();


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

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


services.Configure(_configure); services.Configure(_configure);




+ 1
- 1
src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs View File

@@ -81,7 +81,7 @@ namespace DotNetCore.CAP
ICapPublisher publisher, bool autoCommit = false) ICapPublisher publisher, bool autoCommit = false)
{ {
var clientSessionHandle = client.StartSession(); 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); var capTrans = publisher.Transaction.Value.Begin(clientSessionHandle, autoCommit);
return new CapMongoDbClientSessionHandle(capTrans); return new CapMongoDbClientSessionHandle(capTrans);
} }


+ 0
- 1
src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs View File

@@ -26,7 +26,6 @@ namespace DotNetCore.CAP
services.AddSingleton<IDataStorage, MySqlDataStorage>(); services.AddSingleton<IDataStorage, MySqlDataStorage>();
services.TryAddSingleton<IStorageInitializer, MySqlStorageInitializer>(); services.TryAddSingleton<IStorageInitializer, MySqlStorageInitializer>();
services.AddTransient<ICapTransaction, MySqlCapTransaction>();


//Add MySqlOptions //Add MySqlOptions
services.Configure(_configure); services.Configure(_configure);


+ 2
- 2
src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs View File

@@ -121,7 +121,7 @@ namespace DotNetCore.CAP
ICapPublisher publisher, bool autoCommit = false) ICapPublisher publisher, bool autoCommit = false)
{ {
var trans = database.BeginTransaction(); 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); var capTrans = publisher.Transaction.Value.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans); return new CapEFDbTransaction(capTrans);
} }
@@ -142,7 +142,7 @@ namespace DotNetCore.CAP
} }


var dbTransaction = dbConnection.BeginTransaction(); 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); return publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
} }
} }

+ 0
- 1
src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs View File

@@ -27,7 +27,6 @@ namespace DotNetCore.CAP


services.AddSingleton<IDataStorage, PostgreSqlDataStorage>(); services.AddSingleton<IDataStorage, PostgreSqlDataStorage>();
services.AddSingleton<IStorageInitializer, PostgreSqlStorageInitializer>(); services.AddSingleton<IStorageInitializer, PostgreSqlStorageInitializer>();
services.AddTransient<ICapTransaction, PostgreSqlCapTransaction>();
} }
} }
} }

+ 2
- 2
src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs View File

@@ -124,7 +124,7 @@ namespace DotNetCore.CAP
if (dbConnection.State == ConnectionState.Closed) dbConnection.Open(); if (dbConnection.State == ConnectionState.Closed) dbConnection.Open();


var dbTransaction = dbConnection.BeginTransaction(); 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); return publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
} }


@@ -139,7 +139,7 @@ namespace DotNetCore.CAP
ICapPublisher publisher, bool autoCommit = false) ICapPublisher publisher, bool autoCommit = false)
{ {
var trans = database.BeginTransaction(); 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); var capTrans = publisher.Transaction.Value.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans); return new CapEFDbTransaction(capTrans);
} }


+ 0
- 1
src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs View File

@@ -29,7 +29,6 @@ namespace DotNetCore.CAP
services.AddSingleton<DiagnosticProcessorObserver>(); services.AddSingleton<DiagnosticProcessorObserver>();
services.AddSingleton<IDataStorage, SqlServerDataStorage>(); services.AddSingleton<IDataStorage, SqlServerDataStorage>();
services.AddSingleton<IStorageInitializer, SqlServerStorageInitializer>(); services.AddSingleton<IStorageInitializer, SqlServerStorageInitializer>();
services.AddTransient<ICapTransaction, SqlServerCapTransaction>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IProcessingServer, DiagnosticRegister>()); services.TryAddEnumerable(ServiceDescriptor.Singleton<IProcessingServer, DiagnosticRegister>());


services.Configure(_configure); services.Configure(_configure);


+ 5
- 6
src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs View File

@@ -46,14 +46,14 @@ namespace DotNetCore.CAP
if (dbTransaction == null) throw new ArgumentNullException(nameof(DbTransaction)); 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)) if (_diagnosticProcessor.BufferList.TryGetValue(transactionKey, out var list))
{ {
list.Add(msg); list.Add(msg);
} }
else else
{ {
var msgList = new List<MediumMessage>(1) {msg};
var msgList = new List<MediumMessage>(1) { msg };
_diagnosticProcessor.BufferList.TryAdd(transactionKey, msgList); _diagnosticProcessor.BufferList.TryAdd(transactionKey, msgList);
} }
} }
@@ -163,11 +163,10 @@ namespace DotNetCore.CAP
ICapPublisher publisher, bool autoCommit = false) ICapPublisher publisher, bool autoCommit = false)
{ {
if (dbConnection.State == ConnectionState.Closed) dbConnection.Open(); if (dbConnection.State == ConnectionState.Closed) dbConnection.Open();

var dbTransaction = dbConnection.BeginTransaction(); 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); var capTransaction = publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
return (IDbTransaction) capTransaction.DbTransaction;
return (IDbTransaction)capTransaction.DbTransaction;
} }


/// <summary> /// <summary>
@@ -181,7 +180,7 @@ namespace DotNetCore.CAP
ICapPublisher publisher, bool autoCommit = false) ICapPublisher publisher, bool autoCommit = false)
{ {
var trans = database.BeginTransaction(); 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); var capTrans = publisher.Transaction.Value.Begin(trans, autoCommit);
return new CapEFDbTransaction(capTrans); return new CapEFDbTransaction(capTrans);
} }


Loading…
Cancel
Save