Page 1 of 1

EFCore - Memory not released for DbContexts taking externally created DbConnection

Posted: Tue 11 Sep 2018 11:25
by rmatbg
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.

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

Posted: Wed 12 Sep 2018 14:16
by Pinturiccio
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.

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

Posted: Wed 12 Sep 2018 15:00
by rmatbg
Good stuff, glad to hear it's been sorted quickly!
Looking forward to that build.

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

Posted: Thu 20 Sep 2018 13:02
by Pinturiccio
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

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

Posted: Thu 20 Sep 2018 13:26
by rmatbg
I have now tested v9.6.597 and can confirm the issue has been fixed.
Thank you!