From a9e0743f07c1c778c1ef0fde2553e5ed03c5f976 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Tue, 15 Aug 2017 17:26:23 +0800 Subject: [PATCH] fixed issue #25 --- src/DotNetCore.CAP.MySql/CapPublisher.cs | 12 ++++++++---- src/DotNetCore.CAP.PostgreSql/CapPublisher.cs | 12 ++++++++---- src/DotNetCore.CAP.SqlServer/CapPublisher.cs | 14 +++++++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/DotNetCore.CAP.MySql/CapPublisher.cs b/src/DotNetCore.CAP.MySql/CapPublisher.cs index 6b8476a..59801df 100644 --- a/src/DotNetCore.CAP.MySql/CapPublisher.cs +++ b/src/DotNetCore.CAP.MySql/CapPublisher.cs @@ -34,13 +34,17 @@ namespace DotNetCore.CAP.MySql protected override void PrepareConnectionForEF() { DbConnection = _dbContext.Database.GetDbConnection(); - var transaction = _dbContext.Database.CurrentTransaction; - if (transaction == null) + var dbContextTransaction = _dbContext.Database.CurrentTransaction; + var dbTrans = dbContextTransaction?.GetDbTransaction(); + //DbTransaction is dispose in original + if (dbTrans?.Connection == null) { IsCapOpenedTrans = true; - transaction = _dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); + dbContextTransaction?.Dispose(); + dbContextTransaction = _dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); + dbTrans = dbContextTransaction.GetDbTransaction(); } - DbTranasaction = transaction.GetDbTransaction(); + DbTranasaction = dbTrans; } protected override void Execute(IDbConnection dbConnection, IDbTransaction dbTransaction, CapPublishedMessage message) diff --git a/src/DotNetCore.CAP.PostgreSql/CapPublisher.cs b/src/DotNetCore.CAP.PostgreSql/CapPublisher.cs index f141b1e..4e3c9cd 100644 --- a/src/DotNetCore.CAP.PostgreSql/CapPublisher.cs +++ b/src/DotNetCore.CAP.PostgreSql/CapPublisher.cs @@ -34,13 +34,17 @@ namespace DotNetCore.CAP.PostgreSql protected override void PrepareConnectionForEF() { DbConnection = _dbContext.Database.GetDbConnection(); - var transaction = _dbContext.Database.CurrentTransaction; - if (transaction == null) + var dbContextTransaction = _dbContext.Database.CurrentTransaction; + var dbTrans = dbContextTransaction?.GetDbTransaction(); + //DbTransaction is dispose in original + if (dbTrans?.Connection == null) { IsCapOpenedTrans = true; - transaction = _dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); + dbContextTransaction?.Dispose(); + dbContextTransaction = _dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); + dbTrans = dbContextTransaction.GetDbTransaction(); } - DbTranasaction = transaction.GetDbTransaction(); + DbTranasaction = dbTrans; } protected override void Execute(IDbConnection dbConnection, IDbTransaction dbTransaction, CapPublishedMessage message) diff --git a/src/DotNetCore.CAP.SqlServer/CapPublisher.cs b/src/DotNetCore.CAP.SqlServer/CapPublisher.cs index 3acddb2..500a82a 100644 --- a/src/DotNetCore.CAP.SqlServer/CapPublisher.cs +++ b/src/DotNetCore.CAP.SqlServer/CapPublisher.cs @@ -1,5 +1,6 @@ using System; using System.Data; +using System.Data.SqlClient; using System.Threading.Tasks; using Dapper; using DotNetCore.CAP.Abstractions; @@ -34,13 +35,17 @@ namespace DotNetCore.CAP.SqlServer protected override void PrepareConnectionForEF() { DbConnection = _dbContext.Database.GetDbConnection(); - var transaction = _dbContext.Database.CurrentTransaction; - if (transaction == null) + var dbContextTransaction = _dbContext.Database.CurrentTransaction; + var dbTrans = dbContextTransaction?.GetDbTransaction(); + //DbTransaction is dispose in original + if (dbTrans?.Connection == null) { IsCapOpenedTrans = true; - transaction = _dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); + dbContextTransaction?.Dispose(); + dbContextTransaction = _dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); + dbTrans = dbContextTransaction.GetDbTransaction(); } - DbTranasaction = transaction.GetDbTransaction(); + DbTranasaction = dbTrans; } protected override void Execute(IDbConnection dbConnection, IDbTransaction dbTransaction, CapPublishedMessage message) @@ -53,7 +58,6 @@ namespace DotNetCore.CAP.SqlServer protected override async Task ExecuteAsync(IDbConnection dbConnection, IDbTransaction dbTransaction, CapPublishedMessage message) { await dbConnection.ExecuteAsync(PrepareSql(), message, dbTransaction); - _logger.LogInformation("Published Message has been persisted in the database. name:" + message.ToString()); }