Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for System.Transactions #9561

Merged
merged 1 commit into from
Sep 7, 2017
Merged

Add support for System.Transactions #9561

merged 1 commit into from
Sep 7, 2017

Conversation

AndriySvyryd
Copy link
Member

The tests currently only run for SQL Server on net461

Fixes #5595

{
using (var transaction = TestStore.BeginTransaction())
{
using (var context = CreateContextWithConnectionString())
{
context.Database.AutoTransactionsEnabled = autoTransaction;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant test. AutoTransactionsEnabled only affects SaveChanges. This test doesn't call SaveChanges.

@@ -104,6 +104,53 @@ protected RelationalConnection([NotNull] RelationalConnectionDependencies depend
public virtual IDbContextTransaction CurrentTransaction { get; [param: CanBeNull] protected set; }

/// <summary>
/// The currently enlisted transaction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need more here to document what flavor of .NET transaction this "Transaction" type is. @divega?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Transactions.Transaction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@divega I was thinking of some more words than this to help people decide which to use...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I misunderstood. Stepping back I think these API additions can be very confusing. Not sure additional words in the member documentation are going to help.

I am thinking we should brainstorm something we can use in the API names as well to differentiate from the normal scenarios we already support. Right now "external transaction" comes to mind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach suggested by @anpete would be to make these extension methods in the System.Transactions namespace, so people have to add a using statement to light the API up.

// the connection. This will enlist the newly opened connection to the second transaction but will also close the reader being used
// by the first active query. As a result when trying to continue reading results from the first query the user will get an exception
// saying that calling "Read" on a closed data reader is not a valid operation.
AmbientTransaction = current;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing something here, but is this better than just throwing? In other words, what's the valid scenario where we need to do this?

Copy link
Member Author

@AndriySvyryd AndriySvyryd Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is EF6 behavior. If the query reader enlisted in the first transaction is never read from after this the second operation will still work. We can choose to throw instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, would like to discuss with @divega

@AndriySvyryd AndriySvyryd force-pushed the Issue5595 branch 3 times, most recently from 7e0f505 to 0d00876 Compare August 25, 2017 21:19
Copy link
Member

@ajcvickers ajcvickers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving with the changes to close/open discussed in person.

@AndriySvyryd AndriySvyryd force-pushed the Issue5595 branch 8 times, most recently from b6d4663 to 810aa01 Compare August 31, 2017 23:57
@AndriySvyryd
Copy link
Member Author

@divega, @ajcvickers I've updated with extension methods and connection keep-alive. Can you review again?

I found a scenario broken by the keep-alive:

using (new TransactionScope())
{
    // query

    using (new TransactionScope(TransactionScopeOption.Suppress))
    {
        // query
    }

    // SaveChanges
}

Now the middle query would still be executed in the transaction. If we tried to unenlist it would throw.

@divega
Copy link
Contributor

divega commented Sep 1, 2017

I found a scenario broken by the keep-alive:...

Is this a scenario in which you would really need more than one connection? Then it is kind of nice if it throws...

@AndriySvyryd AndriySvyryd force-pushed the Issue5595 branch 2 times, most recently from d897a89 to 6bf389d Compare September 5, 2017 17:55
@ajcvickers
Copy link
Member

Signing off on the latest changes.

Copy link
Contributor

@divega divega left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@divega
Copy link
Contributor

divega commented Sep 7, 2017

Upgrading to providers-beware since there is new API here (SupportsAmbientTransactions) providers would need to implement.

@AndriySvyryd AndriySvyryd merged commit 792ee4d into dev Sep 7, 2017
@AndriySvyryd AndriySvyryd deleted the Issue5595 branch September 7, 2017 22:56
@bruno-garcia
Copy link
Member

No milestone set so I wonder: When could we expect this on NuGet?

@AndriySvyryd
Copy link
Member Author

@bruno-garcia This is just a pull request, the corresponding issue is #5595 and it's in the 2.1.0 milestone

@bruno-garcia
Copy link
Member

My bad. I got to the issue after asking here.

@codematrix
Copy link

So, I guess this is still not supported? I'm currently using 2.02 and still getting the same issue.

@AndriySvyryd
Copy link
Member Author

@codematrix This feature is in 2.1.0-preview1

@roji
Copy link
Member

roji commented May 13, 2018

FYI, am adding support for this in Npgsql and wanted to share some info.

Some of the tests purposefully generated an error and then continue with the TransactionScope (SaveChanges_uses_enlisted_transaction, SaveChanges_uses_ambient_transaction_with_connectionString, SaveChanges_uses_ambient_transaction when closeConnection is true). PostgreSQL However, in PostgreSQL, a transaction is put into a failed state after any error, and is unrecoverable - it can only be rolled back. So these tests must be skipped for now (which isn't great because they test some important behavior).

Ideally these tests wouldn't depend on continuing after a failure. But it's no big deal - mostly wanted you to know that you cannot rely on a transaction being recoverable after an error across databases.

@ajcvickers
Copy link
Member

@AndriySvyryd Should we file a test issue for this?

@AndriySvyryd
Copy link
Member Author

Filed #12001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants