EFCore - Memory not released for DbContexts taking externally created DbConnection

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
rmatbg
Posts: 8
Joined: Mon 11 Jun 2018 09:40

EFCore - Memory not released for DbContexts taking externally created DbConnection

Post by rmatbg » Tue 11 Sep 2018 11:25

Consider an application doing the following:

Code: Select all

while (true)
{
    using (var conn = new OracleConnection("your connection string"))
    using (var dbContext = new MyDbContext(conn))
        DoQueries(dbContext); // Actual work is irrelevant, as long as this is doing some DB access in order to prime the DbContext
}
With a DbContext configured as:

Code: Select all

    public class MyDbContext : DbContext
    {
        private readonly IDbConnection _dbConnection;

        public MyDbContext(IDbConnection dbConnection) => _dbConnection = dbConnection;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
                optionsBuilder.UseOracle(_dbConnection as DbConnection);
        }
        (...)
    }
Memory will continuously increase and will never be freed, ultimately resulting in an OutOfMemoryException.

The issue can be resolved if the DbContext takes ownership over the underlying DbConnection, e.g by configuring as:

Code: Select all

optionsBuilder.UseOracle("your connection string");
In which case disposing of the context will successfully release all associated resources.

To be fair I'm not sure if this is dotConnect's fault here or if it's EFCore. Since UseOracle is your extension I'm raising the issue here to start with.

Let me know if you need a sample project for this but since this is easy to reproduce the above should suffice.

Pinturiccio
Devart Team
Posts: 2420
Joined: Wed 02 Nov 2011 09:44

Re: EFCore - Memory not released for DbContexts taking externally created DbConnection

Post by Pinturiccio » Wed 12 Sep 2018 14:16

We have reproduced and fixed the bug with a memory leak in EF Core 2, when a connection is set for DbContext as an OracleConnection instance, and not via a connection string. We will post here when the corresponding build of dotConnect for Oracle is available for download.

rmatbg
Posts: 8
Joined: Mon 11 Jun 2018 09:40

Re: EFCore - Memory not released for DbContexts taking externally created DbConnection

Post by rmatbg » Wed 12 Sep 2018 15:00

Good stuff, glad to hear it's been sorted quickly!
Looking forward to that build.

Pinturiccio
Devart Team
Posts: 2420
Joined: Wed 02 Nov 2011 09:44

Re: EFCore - Memory not released for DbContexts taking externally created DbConnection

Post by Pinturiccio » Thu 20 Sep 2018 13:02

New build of dotConnect for Oracle 9.6.597 is available for download.
It can be downloaded from https://www.devart.com/dotconnect/oracle/download.html (trial version) or from Customer Portal (for users with valid subscription only).
We have also updated our NuGet package: https://www.nuget.org/packages/Devart.D ... le.EFCore/
For more information, please refer to viewtopic.php?t=37715

rmatbg
Posts: 8
Joined: Mon 11 Jun 2018 09:40

Re: EFCore - Memory not released for DbContexts taking externally created DbConnection

Post by rmatbg » Thu 20 Sep 2018 13:26

I have now tested v9.6.597 and can confirm the issue has been fixed.
Thank you!

Post Reply