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

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

Closed
federicoorlandini opened this issue Nov 17, 2015 · 5 comments

Comments

@federicoorlandini
Copy link

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.

@roji
Copy link
Member

roji commented Nov 17, 2015

@damageboy, want this?

@roji
Copy link
Member

roji commented Nov 17, 2015

@roji roji added this to the 3.0.5 milestone Dec 14, 2015
@roji
Copy link
Member

roji commented Dec 14, 2015

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

@roji roji modified the milestone: 3.0.5 Jan 1, 2016
@roji roji added the bug label May 4, 2016
@roji roji added this to the 3.2 milestone May 4, 2016
@roji
Copy link
Member

roji commented May 4, 2016

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.

@roji
Copy link
Member

roji commented Dec 11, 2016

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.

@roji roji closed this as completed Dec 11, 2016
@roji roji removed this from the 3.2 milestone Dec 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants