Skip to content

Null Reference Exception when the TransactionScope exits from the scope #870

Closed

Description

@federicoorlandini

I'm using Npgsql 3.0.3 and a PostgreSQL server 9.3 and I'm trying to use a distributed transaction to orchestrate some data movement between two different database servers.
The code I'm using the the following:

using (var sourceDbConnection = new NpgsqlConnection(SourceConnectionString))
using (var destinationDbConnection = new NpgsqlConnection(DestinationConnectionString))
using (var scope = new TransactionScope())
    {
        sourceDbConnection.Open();
        destinationDbConnection.Open();

        Logger.Info("Moving data for the {0} table.", TableName.ToUpper());
        var innerStopWatch = new Stopwatch();
        innerStopWatch.Start();

        foreach (var entity in entities)
        {
            var etlEntity = new EtlInfoItem
            {
                MigratedEntityId = category.RowId,
                ProjectId = category.ProjectId,
                ExecutionDatetime = DateTime.Now
            };

            // Insert into the destination database
            var isRowMigrated = InsertEntity(entity, DestinationSchema, destinationDbConnection);

            if (isRowMigrated)
            {
                // Update the ETL migration table
                InsertCompletedEtlMigrationEntity(etlEntity, EtlSchema, sourceDbConnection);
            }
            else
            {
                // Update the ETL migration table
                InsertFailedEtlMigrationEntity(etlEntity, EtlSchema, sourceDbConnection);
            }
        }

        Logger.Info("Data moved in {0} sec.", innerStopWatch.Elapsed);

        Logger.Info("Committing transaction to the source database");
                innerStopWatch.Restart();

        scope.Complete();

        innerStopWatch.Stop();
        Logger.Info("Transaction committed in {0} sec.", innerStopWatch.Elapsed);
    }

When the TransactionScope exits from the scope (when exiting the using statement), I get a Null Reference Exception with the following stack trace:
Server stack trace:
at Npgsql.NpgsqlConnector.Cleanup()
at Npgsql.NpgsqlConnector.Break()
at Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage)
at Npgsql.NpgsqlConnector.ReadExpectingT
.........
It happens randomly.

Activity

  1. roji commented on Nov 17, 2015

    @roji
    Member

    @damageboy, want this?

  2. added this to the 3.0.5 milestone on Dec 14, 2015
  3. roji commented on Dec 14, 2015

    @roji
    Member

    @damageboy do you want to take a look at this?

  4. modified the milestone: 3.0.5 on Jan 1, 2016
  5. added this to the 3.2 milestone on May 4, 2016
  6. roji commented on May 4, 2016

    @roji
    Member

    I'm really sorry this hasn't received an answer sooner. I'm hard at work preparing 3.1 for a beta release and won't be able to look at this soon. However, one of 3.2's major goals is a cleanup of how distributed transactions work, and this will be definitely be taken care of.

  7. roji commented on Dec 11, 2016

    @roji
    Member

    Am finally getting around to work on System.Transactions support - am pretty much rewriting the whole thing to be safer and simpler. A lot has changed regardless in Npgsql since this issue was opened, so I'm going to close it. 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).

    If you encounter the same issue with these new versions, please post back here and I'll reopen.

  8. removed this from the 3.2 milestone on Dec 11, 2016
  9. 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

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions