Skip to content

Issue when re-opening a closed connection within a transaction #1163

Closed

Description

@warcha

When attempting to re-open a closed connection within a transaction, the following error is returned:

80013: Connection is not open

when the connection should have been open and the query executed.

The scenario I'm describing can be summarized as:

using (var scope = new TransactionScope()) {
  using (var conn1 = new NpgsqlConnection(...) {
    conn1.Open();
    // STUFF 1
  }
  using (var conn2 = new NpgsqlConnection(...) {
    conn2.Open();
  }
  scope.Complete();
}

Investigating this it seems like there is an incorrect check in the CheckConnectionOpen() method in the NpgsqlConnection.cs class.

There is a local variable (_postponingClose) which is only set to true if attempting to close a connection when in a transaction otherwise it defaults to false.

Now at the end of CheckConnetionOpen() method, there is a check:

if (_postponingClose || connector == null)
{
    throw new InvalidOperationException(resman.GetString("Exception_ConnNotOpen"));
}

This does not seem to make sense, if _postponingClose = true, then this means an actual connection should exist AND be open as this will only be set when attempting to close a connection which in a transaction. It seems incorrect that it should be throwing an error stating that Connection is not open in this instance.

I think the final check should just be (unless I'm missing some point for checking if _postponingClose has been set at this point):

if (connector == null)
{
    throw new InvalidOperationException(resman.GetString("Exception_ConnNotOpen"));
}

If I make the described change to the CheckConnectionOpen() method in the NpgsqlConnection.cs class then the functionality works as expected rather then throwing an error.

Can this issue be verified and that the proposed fix is correct?

I've verified that this does not work from at least NpgSql v2.2.5 to the current version v3.1.3

Activity

  1. roji commented on Jun 16, 2016

    @roji
    Member

    @warcha I know that the distributed transaction and the TransactionScope implementation is buggy, I've been wanting to redo it for a long time but other things were always more important. But it's definitely going to happen for 3.2 - unfortunately I'm not going to be able to work on that for the next few months.

    I'll try to take a look at your suggested ASAP though.

  2. roji commented on Dec 11, 2016

    @roji
    Member

    @warcha, I finally have some time and have been rewriting the support for System.Transactions these past few days. The new implementation properly handles reopening connections (and even recycles them to avoid unnecessary escalation). If you can help test the upcoming 3.2, you should be able to grab recent 3.2 CI nugets from the unstable feed, or you can wait until a beta is released (hopefully not too long from now).

  3. self-assigned this
    on Dec 11, 2016
  4. added this to the 3.2 milestone on Dec 11, 2016
  5. warcha commented on Dec 12, 2016

    @warcha
    ContributorAuthor

    Thanks @roji, I'll have a look at this asap and let you know if I find any issues.

  6. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions