question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TransactionScope seems not to work correctly from v4.0.0 when Enlist=true and pooling=false.

See original GitHub issue

Hello.

I noticed that TransactionScope feature cannot be used correctly from v4.0.0. When we connect with Enlist=true and pooling=false, we can’t use its feature.

Steps to reproduce

  • Install and set Npgsql v3.2.7 or v4.0.0
  • Refer System.Transactions assembly.
  • .NET Framework v4.5.1 project.
var connString = "Host=xxx;Port=xxx;Username=xxx;Password=xxx;Database=xxx;Enlist=true;pooling=false";


// Delete all data from table.
using (var conn = new NpgsqlConnection(connString))
using (var cmd = new NpgsqlCommand("TRUNCATE test", conn))
{
    conn.Open();
    cmd.ExecuteNonQuery();
}

// Insert data but it should not be committed.
using (TransactionScope scope = new TransactionScope())
using (var conn = new NpgsqlConnection(connString))
using (var cmd = new NpgsqlCommand("INSERT INTO test VALUES (1)", conn))
{
    conn.Open();
    cmd.ExecuteNonQuery();

    // scope.Complete() is not called, thus INSERT should not be committed.
}

// But we can see "1" in table when V4.0.0
// while we can't see "1" when V3.2.7.
using (var conn = new NpgsqlConnection(connString))
using (var cmd = new NpgsqlCommand("SELECT a FROM test", conn))
{
    conn.Open();

    using (var reader = cmd.ExecuteReader())
        while (reader.Read())
            Console.WriteLine(reader.GetInt32(0));
}

The issue

In the above test, INSERT in the transaction scope should not be committed because scope.Complete() is not called but it is actually committed when v4.0.0. I tested against v4.0.0 and v3.2.7. It works correctly at v3.2.7 while it doesn’t at v4.0.0.

Further technical details

Npgsql version: v3.2.7 and v4.0.0 PostgreSQL version: 13

It seems that the connection doesn’t issue “BEGIN” when its connection opens. “BEGIN” is issued in the deep level of EnlistTransaction, but from v4.0.0, EnlistTransaction is not called when pooling=false while it is called even when pooling=false at v3.2.7. https://github.com/npgsql/npgsql/blob/640341e3689ea8c698933c301031a7970e783b8a/src/Npgsql/NpgsqlConnection.cs#L312-L313 https://github.com/npgsql/npgsql/blob/f26b14af68d631f0855d86b358e5a53888be46e8/src/Npgsql/NpgsqlConnection.cs#L249-L250

This commit changes the behavior. https://github.com/npgsql/npgsql/commit/19c436e41a64392c7e9a068882976880e2d3d96b#diff-a20021dd873eba1b7ce9bc0ae39356c51f7e7e1c9c8c15b4a438d375735d4545R307

Is this an intended change or not?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maimai3478commented, Feb 6, 2021

@roji Wow! Thank you for fixing it! I looked at commit a little bit and it seems reasonable. I have some unit tests of TransactionScope and if this fix will be backported, I will test it and report it.

0reactions
rojicommented, Feb 6, 2021

@maimai3478 I’ve backported this to 4.1.9 - you can test this by using nuget 4.1.9-ci.20210206T112541 from our patch feed. Let us know if it works for you, and you can just use that package until 4.1.9 is released, which may take a while (packages from that feed are generally considered very stable).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Npgsql - freshcode.club
It's implemented completely in C# 3.0 and thus does not require a client ... TransactionScope seems not to work correctly from v4.0.0 when...
Read more >
Untitled
Web24 thg 9, 2011 · Npgsql does not commit transaction after failed command. ... TransactionScope seems not to work correctly from v4.0.0 when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found