question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support nested transactions

See original GitHub issue

Hi. Is nesting transactions supported?

I’m trying to implement 2 following scenarios with nested transactions: 1)

var rootTransaction = context.Database.BeginTransaction();
// Add 1st part of data to DB 
var innerTransaction = context.Database.BeginTransaction();
// Add 2nd part of data to DB 
innerTransaction.Rollback();
rootTransaction.Commit();

In this case all will be ok. In DB will be stored 1st part of data only.

var rootTransaction = context.Database.BeginTransaction();
// Add 1st part of data to DB 
var innerTransaction = context.Database.BeginTransaction();
// Add 2nd part of data to DB 
innerTransaction.Commit();
rootTransaction.Rollback();

I assume that all data should be removed from DB as I made rollback root transaction. But 2nd part of data still exist in DB.

How to implement nested transactions?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
StrangeWillcommented, Nov 18, 2017

Kind of mixed on this – nested transactions can be really useful. Mostly for automated tests. I like setting up a database in a state, starting a transaction, running a test, then rolling it back. Makes integration tests really nice.

But some of our more complex stuff uses transactions itself, then it explodes due to nested transactions.

Pondering if I have other options but a nested transaction would make this work really well.

Implicit transactions sort of work, but there are times we may read a few pieces of work then writing to the database, and we don’t want a race conditions between the read/write cycle.

I understand the annoyance of various database engines having spotty support for this if any.

2reactions
ShadySadekcommented, Aug 17, 2016

Thanks @ajcvickers for your reply Just to give you more context, in the following code i am assuming that for-each will be executed twice: -first loop will be executed successfully and commit the inner transaction. -second loop will fail and Rollback the inner transaction.

I am expected to find the added rows by the first transaction before rollback the outer transaction but looks like what is happening is rolling back second loop will roll back also first loop

looks like using BeginTransaction within TransactionScope has some issues in addition to the issues with nested transaction in EF6

Any thoughts on this? FYI: there is another post that is reporting the same issue : [http://stackoverflow.com/questions/36396570/using-transactionscope-and-ef-transactions]

 using (var OuterTransactionScope = new TransactionScope())
            {
                using (DbContext = new DbContext())
                {
                    foreach (var x in list) // assume this will be executed twice 
                    {
                        try
                        {
                            var innerTransaction = DbContext.Database.BeginTransaction(isolationLevel);
                            DbContext.SaveChanges(); //Add row
                            DbContext.SaveChanges(); //Add row
                            innerTransaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            innerTransaction.Rollback();
                        }
                    }
                    //Expecting to find two rows here but Actually no data get added
                }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does SQL Server support nested transactions?
Yes transaction can be nested. But once your outer transaction fails, your inner transaction will be rollback too. Nested transactions in Sql ...
Read more >
Nested transactions in CockroachDB 20.1
Nested transactions are invisible to concurrent clients, due to the atomicity and isolation of their surrounding, “outermost” transaction.
Read more >
Nested Transactions in SQL Server
In this article I am going to discuss Nested Transactions in SQL Server with Examples. When we put one transaction within another transaction....
Read more >
9.2.13. About Nested Transactions JBoss Enterprise ...
Nested Transactions are only supported as part of the Java Transaction Service (JTS) API, and not part of the Java Transaction API (JTA)....
Read more >
Nested Transactions
A nested transaction occurs when a new transaction is started on a session that is already inside the scope of an existing transaction....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found