Browse Source

Merge pull request #10713 from abpframework/maliming/UnitOfWorkDbContextProvider

Log errors and not using if the current database does not support transactions.
pull/10784/head
Halil İbrahim Kalkan 5 years ago
committed by GitHub
parent
commit
0614765fe7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 166
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs
  2. 5
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

166
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs

@ -18,6 +18,8 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext> public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext>
where TDbContext : IEfCoreDbContext where TDbContext : IEfCoreDbContext
{ {
private const string TransactionsNotSupportedErrorMessage = "Current database does not support transactions. Your database may remain in an inconsistent state in an error case.";
public ILogger<UnitOfWorkDbContextProvider<TDbContext>> Logger { get; set; } public ILogger<UnitOfWorkDbContextProvider<TDbContext>> Logger { get; set; }
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
@ -170,19 +172,29 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
{ {
var dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>(); var dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>();
var dbtransaction = unitOfWork.Options.IsolationLevel.HasValue try
? dbContext.Database.BeginTransaction(unitOfWork.Options.IsolationLevel.Value) {
: dbContext.Database.BeginTransaction(); var dbtransaction = unitOfWork.Options.IsolationLevel.HasValue
? dbContext.Database.BeginTransaction(unitOfWork.Options.IsolationLevel.Value)
unitOfWork.AddTransactionApi( : dbContext.Database.BeginTransaction();
transactionApiKey,
new EfCoreTransactionApi( unitOfWork.AddTransactionApi(
dbtransaction, transactionApiKey,
dbContext, new EfCoreTransactionApi(
_cancellationTokenProvider dbtransaction,
) dbContext,
); _cancellationTokenProvider
)
);
}
catch (Exception e) when (e is InvalidOperationException || e is NotSupportedException)
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
return dbContext; return dbContext;
} }
else else
@ -199,25 +211,45 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
} }
else else
{ {
/* User did not re-use the ExistingConnection and we are starting a new transaction. try
* EfCoreTransactionApi will check the connection string match and separately
* commit/rollback this transaction over the DbContext instance. */
if (unitOfWork.Options.IsolationLevel.HasValue)
{ {
dbContext.Database.BeginTransaction(unitOfWork.Options.IsolationLevel.Value); /* User did not re-use the ExistingConnection and we are starting a new transaction.
* EfCoreTransactionApi will check the connection string match and separately
* commit/rollback this transaction over the DbContext instance. */
if (unitOfWork.Options.IsolationLevel.HasValue)
{
dbContext.Database.BeginTransaction(unitOfWork.Options.IsolationLevel.Value);
}
else
{
dbContext.Database.BeginTransaction();
}
} }
else catch (Exception e) when (e is InvalidOperationException || e is NotSupportedException)
{ {
dbContext.Database.BeginTransaction(); Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
} }
} }
} }
else else
{ {
/* No need to store the returning IDbContextTransaction for non-relational databases try
* since EfCoreTransactionApi will handle the commit/rollback over the DbContext instance. {
*/ /* No need to store the returning IDbContextTransaction for non-relational databases
dbContext.Database.BeginTransaction(); * since EfCoreTransactionApi will handle the commit/rollback over the DbContext instance.
*/
dbContext.Database.BeginTransaction();
}
catch (Exception e) when (e is InvalidOperationException || e is NotSupportedException)
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
} }
activeTransaction.AttendedDbContexts.Add(dbContext); activeTransaction.AttendedDbContexts.Add(dbContext);
@ -235,18 +267,28 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
{ {
var dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>(); var dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>();
var dbTransaction = unitOfWork.Options.IsolationLevel.HasValue try
? await dbContext.Database.BeginTransactionAsync(unitOfWork.Options.IsolationLevel.Value, GetCancellationToken()) {
: await dbContext.Database.BeginTransactionAsync(GetCancellationToken()); var dbTransaction = unitOfWork.Options.IsolationLevel.HasValue
? await dbContext.Database.BeginTransactionAsync(unitOfWork.Options.IsolationLevel.Value, GetCancellationToken())
unitOfWork.AddTransactionApi( : await dbContext.Database.BeginTransactionAsync(GetCancellationToken());
transactionApiKey,
new EfCoreTransactionApi( unitOfWork.AddTransactionApi(
dbTransaction, transactionApiKey,
dbContext, new EfCoreTransactionApi(
_cancellationTokenProvider dbTransaction,
) dbContext,
); _cancellationTokenProvider
)
);
}
catch (Exception e) when (e is InvalidOperationException || e is NotSupportedException)
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
return dbContext; return dbContext;
} }
@ -264,30 +306,50 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
} }
else else
{ {
/* User did not re-use the ExistingConnection and we are starting a new transaction. try
* EfCoreTransactionApi will check the connection string match and separately
* commit/rollback this transaction over the DbContext instance. */
if (unitOfWork.Options.IsolationLevel.HasValue)
{ {
await dbContext.Database.BeginTransactionAsync( /* User did not re-use the ExistingConnection and we are starting a new transaction.
unitOfWork.Options.IsolationLevel.Value, * EfCoreTransactionApi will check the connection string match and separately
GetCancellationToken() * commit/rollback this transaction over the DbContext instance. */
); if (unitOfWork.Options.IsolationLevel.HasValue)
{
await dbContext.Database.BeginTransactionAsync(
unitOfWork.Options.IsolationLevel.Value,
GetCancellationToken()
);
}
else
{
await dbContext.Database.BeginTransactionAsync(
GetCancellationToken()
);
}
} }
else catch (Exception e) when (e is InvalidOperationException || e is NotSupportedException)
{ {
await dbContext.Database.BeginTransactionAsync( Logger.LogError(TransactionsNotSupportedErrorMessage);
GetCancellationToken() Logger.LogException(e);
);
return dbContext;
} }
} }
} }
else else
{ {
/* No need to store the returning IDbContextTransaction for non-relational databases try
* since EfCoreTransactionApi will handle the commit/rollback over the DbContext instance. {
*/ /* No need to store the returning IDbContextTransaction for non-relational databases
await dbContext.Database.BeginTransactionAsync(GetCancellationToken()); * since EfCoreTransactionApi will handle the commit/rollback over the DbContext instance.
*/
await dbContext.Database.BeginTransactionAsync(GetCancellationToken());
}
catch (Exception e) when (e is InvalidOperationException || e is NotSupportedException)
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
} }
activeTransaction.AttendedDbContexts.Add(dbContext); activeTransaction.AttendedDbContexts.Add(dbContext);

5
framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

@ -17,6 +17,7 @@ namespace Volo.Abp.Uow.MongoDB
public class UnitOfWorkMongoDbContextProvider<TMongoDbContext> : IMongoDbContextProvider<TMongoDbContext> public class UnitOfWorkMongoDbContextProvider<TMongoDbContext> : IMongoDbContextProvider<TMongoDbContext>
where TMongoDbContext : IAbpMongoDbContext where TMongoDbContext : IAbpMongoDbContext
{ {
private const string TransactionsNotSupportedErrorMessage = "Current database does not support transactions. Your database may remain in an inconsistent state in an error case.";
public ILogger<UnitOfWorkMongoDbContextProvider<TMongoDbContext>> Logger { get; set; } public ILogger<UnitOfWorkMongoDbContextProvider<TMongoDbContext>> Logger { get; set; }
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
@ -190,7 +191,7 @@ namespace Volo.Abp.Uow.MongoDB
} }
catch (NotSupportedException e) catch (NotSupportedException e)
{ {
Logger.LogError("The current MongoDB database does not support transactions, All operations will be performed in non-transactions, This may cause errors."); Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e); Logger.LogException(e);
dbContext.ToAbpMongoDbContext().InitializeDatabase(database, client, null); dbContext.ToAbpMongoDbContext().InitializeDatabase(database, client, null);
@ -241,7 +242,7 @@ namespace Volo.Abp.Uow.MongoDB
} }
catch (NotSupportedException e) catch (NotSupportedException e)
{ {
Logger.LogError("The current MongoDB database does not support transactions, All operations will be performed in non-transactions, This may cause errors."); Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e); Logger.LogException(e);
dbContext.ToAbpMongoDbContext().InitializeDatabase(database, client, null); dbContext.ToAbpMongoDbContext().InitializeDatabase(database, client, null);

Loading…
Cancel
Save