Skip to content

NpgsqlTransaction.Connection should be accessible after the transaction has completed #3384

Closed

Description

@huseyingoztok

I update in the foreach loop and commit after the loop is over. The previous version did not have this error. Can you give information about the reason? Thanks for help.

Activity

  1. YohDeadfall commented on Nov 27, 2020

    @YohDeadfall
    Contributor

    Could you provide a sample code?

  2. huseyingoztok commented on Nov 27, 2020

    @huseyingoztok
    Author

    There is an update process to two different tables in the code. Indent has been bad. Sorry.

    `foreach (var item in items)
    {
    try
    {
    var fooList = Db.GetDefaultRepo().GetMany(a => !a.IsDeleted && a.PropId == item.PropId).FetchAll(trackChanges: true);

                    if (item.UpdatePeriodId != null)
                    {
                        item.NextUpdateDate = item.UpdatePeriodStartDate?.Date;
                        otherRepo.Update(item);
                    }
    
                    foreach (var foo in fooList)
                    {
                        var fooProperties = foo.FooProperties?.ToList<FooPropertyModel>() ?? new List<FooPropertyModel>();
                        var fooProperty = fooProperties.FirstOrDefault(f => f.FooId == item.Id);
    
                        if (item.IsDeleted || item.UpdatePeriodId == null)
                        {
                            fooProperties.Remove(fooProperty);
                        }
                        else
                        {
                            if (fooProperty == null)
                            {
                                fooProperties.Add(new FooPropertyModel
                                {
                                    FooId = item.Id,
                                    IsUpdatedOnTime = true
                                });
                            }
                            else
                            {
                                fooProperty.IsUpdatedOnTime = true;
                            }
                        }
    
                        foo.FooProperties = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(fooProperties));
                        Db.GetDefaultRepo<FooEntity>().Update(foo);
                    }
    
                    Db.Commit();
    
                    if (item.NextUpdateDate == null) continue;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }`
    
  3. roji commented on Nov 29, 2020

    @roji
    Member

    The issue seems to be that we throw when NpgsqlTransaction.Connection is accessed on a completed transaction:

    [Test, IssueLink("https://github.com/npgsql/efcore.pg/issues/1593")]
    public async Task AccessConnectionOnCompletedTransaction()
    {
        using var conn = await OpenConnectionAsync();
        using var tx = await conn.BeginTransactionAsync();
        tx.Commit();
        Assert.That(tx.Connection, Is.SameAs(conn));
    }

    @vonzshik are you interested in taking a look at this?

    Minimal EF6 repro
    using var ctx = new BlogContext();
    ctx.Database.Delete();
    ctx.Database.Create();
    
    class BlogContext : DbContext
    {
        public BlogContext() : base("Host=localhost;Username=test;Password=test")
        {
        }
    
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    class NpgsqlConfiguration : DbConfiguration
    {
        public NpgsqlConfiguration()
        {
            SetProviderFactory("Npgsql", NpgsqlFactory.Instance);
            SetProviderServices("Npgsql", NpgsqlServices.Instance);
            SetDefaultConnectionFactory(new NpgsqlConnectionFactory());
        }
    }
  4. transferred this issue fromnpgsql/efcore.pgon Nov 29, 2020
  5. added this to the 5.0.1 milestone on Nov 29, 2020
  6. vonzshik commented on Nov 29, 2020

    @vonzshik
    Contributor
  7. izvictor commented on Dec 2, 2020

    @izvictor

    Hello,
    Do you have a relase date planned for 5.0.1 milestone?
    Thanks

  8. roji commented on Dec 5, 2020

    @roji
    Member

    This will happen in the coming days, probably right after .NET and EF Core 5.0.1 are released

  9. added 2 commits that reference this issue on Dec 9, 2020
    fcd2e50
    9c0d5d6
  10. changed the title [-]Npgsql 5.0.0.0 transaction completed and it is no longer available[/-] [+]NpgsqlTransaction.Connection should be accessible after the transaction has completed[/+] on Dec 11, 2020
  11. 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