-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1834 from totvsnetcore/efcore-sharedtransaction
Add EfCoreTransactionStrategy to share transactions
- Loading branch information
Showing
7 changed files
with
243 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/Abp.EntityFrameworkCore/EntityFrameworkCore/IDbContextResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Data.Common; | ||
|
||
namespace Abp.EntityFrameworkCore | ||
{ | ||
public interface IDbContextResolver | ||
{ | ||
TDbContext Resolve<TDbContext>(string connectionString) | ||
where TDbContext : DbContext; | ||
} | ||
|
||
TDbContext Resolve<TDbContext>(DbConnection existingConnection) | ||
where TDbContext : DbContext; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Abp.EntityFrameworkCore/EntityFrameworkCore/Uow/ActiveTransactionInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using System.Collections.Generic; | ||
|
||
namespace Abp.EntityFrameworkCore.Uow | ||
{ | ||
public class ActiveTransactionInfo | ||
{ | ||
public IDbContextTransaction DbContextTransaction { get; } | ||
|
||
public DbContext StarterDbContext { get; } | ||
|
||
public List<DbContext> AttendedDbContexts { get; } | ||
|
||
public ActiveTransactionInfo(IDbContextTransaction dbContextTransaction, DbContext starterDbContext) | ||
{ | ||
DbContextTransaction = dbContextTransaction; | ||
StarterDbContext = starterDbContext; | ||
|
||
AttendedDbContexts = new List<DbContext>(); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/Abp.EntityFrameworkCore/EntityFrameworkCore/Uow/DbContextEfCoreTransactionStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Abp.Collections.Extensions; | ||
using Abp.Dependency; | ||
using Abp.Domain.Uow; | ||
using Abp.EntityFrameworkCore.Extensions; | ||
using Abp.Transactions.Extensions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using System.Collections.Generic; | ||
using System.Transactions; | ||
|
||
namespace Abp.EntityFrameworkCore.Uow | ||
{ | ||
public class DbContextEfCoreTransactionStrategy : IEfCoreTransactionStrategy, ITransientDependency | ||
{ | ||
protected UnitOfWorkOptions Options { get; private set; } | ||
|
||
protected IDictionary<string, ActiveTransactionInfo> ActiveTransactions { get; } | ||
|
||
public DbContextEfCoreTransactionStrategy() | ||
{ | ||
ActiveTransactions = new Dictionary<string, ActiveTransactionInfo>(); | ||
} | ||
|
||
public void Commit() | ||
{ | ||
foreach (var activeTransaction in ActiveTransactions.Values) | ||
{ | ||
activeTransaction.DbContextTransaction.Commit(); | ||
|
||
foreach (var dbContext in activeTransaction.AttendedDbContexts) | ||
{ | ||
if (dbContext.HasRelationalTransactionManager()) | ||
{ | ||
//Relational databases use the SharedTransaction | ||
continue; | ||
} | ||
|
||
dbContext.Database.CommitTransaction(); | ||
} | ||
} | ||
} | ||
|
||
public DbContext CreateDbContext<TDbContext>(string connectionString, IDbContextResolver dbContextResolver) where TDbContext : DbContext | ||
{ | ||
DbContext dbContext; | ||
|
||
var activeTransaction = ActiveTransactions.GetOrDefault(connectionString); | ||
|
||
if (activeTransaction == null) | ||
{ | ||
dbContext = dbContextResolver.Resolve<TDbContext>(connectionString); | ||
} | ||
else | ||
{ | ||
dbContext = dbContextResolver.Resolve<TDbContext>(activeTransaction.DbContextTransaction.GetDbTransaction().Connection); | ||
} | ||
|
||
if (dbContext.HasRelationalTransactionManager()) | ||
{ | ||
if (activeTransaction == null) | ||
{ | ||
var dbtransaction = dbContext.Database.BeginTransaction((Options.IsolationLevel ?? IsolationLevel.ReadUncommitted).ToSystemDataIsolationLevel()); | ||
activeTransaction = new ActiveTransactionInfo(dbtransaction, dbContext); | ||
ActiveTransactions[connectionString] = activeTransaction; | ||
} | ||
else | ||
{ | ||
dbContext.Database.UseTransaction(activeTransaction.DbContextTransaction.GetDbTransaction()); | ||
activeTransaction.AttendedDbContexts.Add(dbContext); | ||
} | ||
} | ||
else | ||
{ | ||
dbContext.Database.BeginTransaction(); | ||
} | ||
|
||
return dbContext; | ||
} | ||
|
||
public void Dispose(IIocResolver iocResolver) | ||
{ | ||
foreach (var activeTransaction in ActiveTransactions.Values) | ||
{ | ||
activeTransaction.DbContextTransaction.Dispose(); | ||
|
||
foreach (var attendedDbContext in activeTransaction.AttendedDbContexts) | ||
{ | ||
iocResolver.Release(attendedDbContext); | ||
} | ||
|
||
activeTransaction.StarterDbContext.Dispose(); | ||
} | ||
|
||
ActiveTransactions.Clear(); | ||
} | ||
|
||
public void InitOptions(UnitOfWorkOptions options) | ||
{ | ||
Options = options; | ||
} | ||
} | ||
} |
Oops, something went wrong.