TransactionScope seems not to work correctly from v4.0.0 when Enlist=true and pooling=false.
See original GitHub issueHello.
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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
@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.
@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).