Skip to content

TransactionScope timeout does not work #495

Open

Description

@smmansoor

Summary:

Transaction does not rollback if scope timeout has elapsed.

Detailed description:

Consider the following sample. We declare the TransactionScope with scopeTimeOut.
When execute command and the time is over as specified in scopeTimeout. The transaction aborts but the command does not rollback.


using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 5)))
{
    using (conn1)
    {
        conn.Open();
        NpgsqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "update test_ts set c2='sssss' where c1=1";
        cmd.ExecuteNonQuery();                                 // the update operation is expected to rollback
        System.Threading.Thread.Sleep(6000);
    }
    scope.Complete();
}

Following is the error message and stack trace.


[Error Message]
The transaction has aborted.

[StackTrace]
   at System.Transactions.TransactionStateAborted.BeginCommit(InternalTransactio
n tx, Boolean asyncCommit, AsyncCallback asyncCallback, Object asyncState)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()

Following is the patch to fix the above mentioned issue.

https://gist.github.com/smmansoor/d5cd7067bdb6a6d25183#file-transactionscope_timeout_invalid-patch

Activity

  1. roji commented on Feb 20, 2015

    @roji
    Member

    Thanks for raising this. We're aware of some issues with TransactionScope support. Unfortunately our current work on the next major version (v3.0) is already too huge to allow us to tackle that as well, so I was planning a serious pass over TransactionScope for v3.1.

    After a very quick and incomplete review of your patch:

    • Your patch appears to make a lot of changes not related to this specific issue. Can you please separate the fix for this issue from any other changes and submit a minimal patch?
    • Can you please submit it as a standard github PR rather than a gist
    • If possible, please add a test reproducing the issue for regression testing (if not I'll do it).
  2. added this to the 2.2.5 milestone on Mar 4, 2015
  3. roji commented on Mar 4, 2015

    @roji
    Member

    I'll try to look at this soon and include it in a 2.2 release.

  4. roji commented on Mar 4, 2015

    @roji
    Member

    @smmansoor, I tried to reproduce your issue and could not. But I may have an idea: does you connection string include the enlist=true parameter? Npgsql's current support requires that you include this parameter for connection opened inside the TransactionScope. If you didn't include this parameter, your connection is completely disconnected from the TransactionScope, and the rollback has no effect on it.

    Note that you can also enlist a connection to a TransactionScope by calling Enlist() on it.

    If you're still encountering the error, please take a look at the (fb82fca)[test] I just pushed for this and try to help me reproduce.

  5. removed this from the 2.2.5 milestone on Mar 11, 2015
  6. roji commented on Mar 11, 2015

    @roji
    Member

    Have you had any time to check this @smmansoor?

  7. smmansoor commented on Mar 12, 2015

    @smmansoor
    Author

    Hi,

    Sorry for not responding earlier. I moved away on some other stuff. I will
    definitely have a go at it and will get back to you.

    Thanks,
    --Mansoor

    On Thu, Mar 12, 2015 at 12:08 AM, Shay Rojansky [email protected]
    wrote:

    Have you had any time to check this @smmansoor
    https://github.com/smmansoor?

    —
    Reply to this email directly or view it on GitHub
    #495 (comment).

  8. roji commented on May 8, 2016

    @roji
    Member

    Am closing for inactivity, feel free to take this up again.

  9. added and removed on May 8, 2016
  10. YohDeadfall commented on Mar 12, 2019

    @YohDeadfall
    Contributor

    Reproduced it. Probably it was because of disabled pooling in the connection string, and it's problem on our side due to a lack of the documentation. It says that Enlist means:

    Whether to enlist in an ambient TransactionScope.

    And nothing about pooling. ADO.NET documentation has nothing about it too and introduces false expectations due to this paragraph:

    Automatic enlistment is the default (and preferred) way of integrating ADO.NET connections with System.Transactions. A connection object will automatically enlist in an existing distributed transaction if it determines that a transaction is active, which, in System.Transaction terms, means that Transaction.Current is not null. Automatic transaction enlistment occurs when the connection is opened. It will not happen after that even if a command is executed inside of a transaction scope.

    SqlClient documentations states that it's a responsibility of the pooler (see Enlist in docs):

    true indicates that the SQL Server connection pooler automatically enlists the connection in the creation thread's current transaction context.

  11. self-assigned this
    on Mar 12, 2019
  12. roji commented on Mar 15, 2019

    @roji
    Member

    @YohDeadfall in theory we could look at enabling enlist even if the connection isn't pooled. This shouldn't be too hard, I think the main thing to look out for is when the connection is closed before the TransactionScope completes, in which case it cannot actually be closed (since we haven't committed or rolled back yet). Logic to keep the connection in a special state exists for the pooled scenario, but not for unpooled. I don't think anyone's complained because very few people disable pooling.

    Of course we can and should add a note in the meantime...

  13. added this to the Backlog milestone on Oct 14, 2019
  14. removed their assignment
    on Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions