Efcore: How to deal with nested transactions?

Created on 3 Aug 2016  路  5Comments  路  Source: dotnet/efcore

Hi,
I know that EF Core doesn't support TransactionScope anymore.
I also know how to do an explicit BeginTransaction().
What I'm struggling with now is situations where a code path may create a nested transaction.
TransactionScope handled this situation very cleanly; the explicit BeginTransaction() does not.
Do you have any recommended practices for dealing with the possibility of nested transactions?

closed-duplicate

Most helpful comment

Just to clarify things a bit...

Some databases do support true nested transactions (e.g. Oracle). Others support some version of nested transaction, such as SQL savepoints, which allow selective rollback within a transaction (PostgreSQL supports this).

Regardless, .NET TransactionScope doesn't really have anything to do with nested transactions - a TransactionScope is not a transaction. Nesting TransactionScopes is a way of _voting_ whether the root TransactionScope should commit or not. The root TransactionScope does correspond to a database transaction, while nested scopes with TransactionScopeOption.Require do not. If I understand things correctly, nesting a TransactionScope with TransactionScopeOption.RequireNew does involve creating a new transaction, but there is no nesting relationship between the two transactions - the inner and outer TransactionScopes correspond to completely unrelated database transactions.

While TransactionScope isn't really related to nested transactions, it's still important for other purposes - ambient transaction management can simplify code a lot (rather than passing a DbTransaction via the stack), and TransactionScope is also the only way to do distributed transactions (a single transaction involving more than one database/queue).

So EFCore TransactionScope support does seem to be an important feature, even if it's not really related to nested transactions.

All 5 comments

In case you don't know: .NET and SQL Server do not support true nested transactions at all. Both APIs just reference count the same physical transaction.

Even so, TransactionScope was very convenient to work with. BeginTransaction() blows up if you try to nest transactions.

Just to clarify things a bit...

Some databases do support true nested transactions (e.g. Oracle). Others support some version of nested transaction, such as SQL savepoints, which allow selective rollback within a transaction (PostgreSQL supports this).

Regardless, .NET TransactionScope doesn't really have anything to do with nested transactions - a TransactionScope is not a transaction. Nesting TransactionScopes is a way of _voting_ whether the root TransactionScope should commit or not. The root TransactionScope does correspond to a database transaction, while nested scopes with TransactionScopeOption.Require do not. If I understand things correctly, nesting a TransactionScope with TransactionScopeOption.RequireNew does involve creating a new transaction, but there is no nesting relationship between the two transactions - the inner and outer TransactionScopes correspond to completely unrelated database transactions.

While TransactionScope isn't really related to nested transactions, it's still important for other purposes - ambient transaction management can simplify code a lot (rather than passing a DbTransaction via the stack), and TransactionScope is also the only way to do distributed transactions (a single transaction involving more than one database/queue).

So EFCore TransactionScope support does seem to be an important feature, even if it's not really related to nested transactions.

Dupe of #3470?

Closing because:

  1. The various features commonly referred to as "nested transactions" are very database specific and we are not planning to do anything on EF Core to try to deal with them in a uniform way (in this aspect, this is indeed a duplicate of #3470). That said, if we ever hear from a database provider writer or a customer any specific feedback on something we can do on EF Core __to get out of the way of nested transactions from working__, we will consider it.
  2. As @roji explained already in https://github.com/aspnet/EntityFrameworkCore/issues/6233#issuecomment-242693262, nested TransactionScope is not the same as nested transactions.
  3. We are adding support for TransactionScope in 2.1 anyway. Given how the original question was stated, I believe this is actually more of a duplicate of TransactionScope support.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgribaudo picture bgribaudo  路  3Comments

MontyGvMC picture MontyGvMC  路  3Comments

ghost picture ghost  路  3Comments

miguelhrocha picture miguelhrocha  路  3Comments

leak picture leak  路  3Comments