Skip to content

Commit

Permalink
Resolved #7489: Async implementation for Transaction Commit/Rollback.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Jan 28, 2021
1 parent 7e902d7 commit 103b211
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Threading;

namespace Volo.Abp.Uow.EntityFrameworkCore
{
Expand All @@ -14,22 +15,22 @@ public class EfCoreTransactionApi : ITransactionApi, ISupportsRollback
public IEfCoreDbContext StarterDbContext { get; }
public List<IEfCoreDbContext> AttendedDbContexts { get; }

public EfCoreTransactionApi(IDbContextTransaction dbContextTransaction, IEfCoreDbContext starterDbContext)
protected ICancellationTokenProvider CancellationTokenProvider { get; }

public EfCoreTransactionApi(
IDbContextTransaction dbContextTransaction,
IEfCoreDbContext starterDbContext,
ICancellationTokenProvider cancellationTokenProvider)
{
DbContextTransaction = dbContextTransaction;
StarterDbContext = starterDbContext;
CancellationTokenProvider = cancellationTokenProvider;
AttendedDbContexts = new List<IEfCoreDbContext>();
}

public Task CommitAsync()
{
Commit();
return Task.CompletedTask;
}

protected void Commit()
public async Task CommitAsync()
{
DbContextTransaction.Commit();
await DbContextTransaction.CommitAsync(CancellationTokenProvider.Token);

foreach (var dbContext in AttendedDbContexts)
{
Expand All @@ -38,7 +39,7 @@ protected void Commit()
continue; //Relational databases use the shared transaction
}

dbContext.Database.CommitTransaction();
await dbContext.Database.CommitTransactionAsync(CancellationTokenProvider.Token);
}
}

Expand All @@ -47,15 +48,11 @@ public void Dispose()
DbContextTransaction.Dispose();
}

public void Rollback()
{
DbContextTransaction.Rollback();
}

public Task RollbackAsync(CancellationToken cancellationToken)
{
DbContextTransaction.Rollback();
return Task.CompletedTask;
return DbContextTransaction.RollbackAsync(
CancellationTokenProvider.FallbackToProvider(cancellationToken)
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ private TDbContext CreateDbContextWithTransaction(IUnitOfWork unitOfWork)
transactionApiKey,
new EfCoreTransactionApi(
dbtransaction,
dbContext
dbContext,
_cancellationTokenProvider
)
);

Expand Down Expand Up @@ -212,7 +213,8 @@ private async Task<TDbContext> CreateDbContextWithTransactionAsync(IUnitOfWork u
transactionApiKey,
new EfCoreTransactionApi(
dbTransaction,
dbContext
dbContext,
_cancellationTokenProvider
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Volo.Abp.Threading;

namespace Volo.Abp.Uow.MongoDB
{
public class MongoDbTransactionApi : ITransactionApi, ISupportsRollback
{
public IClientSessionHandle SessionHandle { get; }

public MongoDbTransactionApi(IClientSessionHandle sessionHandle)
protected ICancellationTokenProvider CancellationTokenProvider { get; }

public MongoDbTransactionApi(
IClientSessionHandle sessionHandle,
ICancellationTokenProvider cancellationTokenProvider)
{
SessionHandle = sessionHandle;
CancellationTokenProvider = cancellationTokenProvider;
}

public async Task CommitAsync()
{
await SessionHandle.CommitTransactionAsync();
}

protected void Commit()
{
SessionHandle.CommitTransaction();
await SessionHandle.CommitTransactionAsync(CancellationTokenProvider.Token);
}

public void Dispose()
{
SessionHandle.Dispose();
}

public void Rollback()
{
SessionHandle.AbortTransaction();
}

public async Task RollbackAsync(CancellationToken cancellationToken)
{
await SessionHandle.AbortTransactionAsync(cancellationToken);
await SessionHandle.AbortTransactionAsync(
CancellationTokenProvider.FallbackToProvider(cancellationToken)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ private TMongoDbContext CreateDbContextWithTransaction(

unitOfWork.AddTransactionApi(
transactionApiKey,
new MongoDbTransactionApi(session)
new MongoDbTransactionApi(
session,
_cancellationTokenProvider
)
);

dbContext.ToAbpMongoDbContext().InitializeDatabase(database, client, session);
Expand Down Expand Up @@ -215,7 +218,10 @@ private async Task<TMongoDbContext> CreateDbContextWithTransactionAsync(

unitOfWork.AddTransactionApi(
transactionApiKey,
new MongoDbTransactionApi(session)
new MongoDbTransactionApi(
session,
_cancellationTokenProvider
)
);

dbContext.ToAbpMongoDbContext().InitializeDatabase(database, client, session);
Expand Down
4 changes: 1 addition & 3 deletions framework/src/Volo.Abp.Uow/Volo/Abp/Uow/ISupportsRollback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace Volo.Abp.Uow
{
public interface ISupportsRollback
{
void Rollback();

Task RollbackAsync(CancellationToken cancellationToken);
}
}
}
21 changes: 0 additions & 21 deletions framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,27 +262,6 @@ private void PreventMultipleComplete()
}
}

protected virtual void RollbackAll()
{
foreach (var databaseApi in GetAllActiveDatabaseApis())
{
try
{
(databaseApi as ISupportsRollback)?.Rollback();
}
catch { }
}

foreach (var transactionApi in GetAllActiveTransactionApis())
{
try
{
(transactionApi as ISupportsRollback)?.Rollback();
}
catch { }
}
}

protected virtual async Task RollbackAllAsync(CancellationToken cancellationToken)
{
foreach (var databaseApi in GetAllActiveDatabaseApis())
Expand Down

0 comments on commit 103b211

Please sign in to comment.