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?
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:
TransactionScope
is not the same as nested transactions.TransactionScope
in 2.1 anyway. Given how the original question was stated, I believe this is actually more of a duplicate of TransactionScope
support.
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 withTransactionScopeOption.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.