'Using Dapper with EF Core properly to avoid stale database connections

I have gone through this and this which are somewhat related to my question, but implementation is different. So thought to ask this question.

In my application I am using Dapper for reads. I have added below extension to my DbContext to use with Dapper;

    public static IDbConnection UsingDapper(this DbContext context)
    {
        return context.Database.GetDbConnection();
    }

So when I want to run a Query (I am using CQRS pattern); from my Query class I simply do;

    var retVal= await dBContext.UsingDapper().[Any method provided by dapper]

Note that in my Query classes; I do not explicitly OPEN/CLOSE database connection. According to my understanding as long as you don't OPEN the connection yourself, you don't have to worry about the CLOSING.

The whole setup is working perfectly fine. Recently I came across Finding db connection leaks article and I wanted to see how my application behaves.

When running the queries given in the above article, I noticed only 3 connections were active for about 5 mins and then it was gone. Those queries logged are done by Dapper(using with extension method).

Now I am not sure whether this is because of connection pooling or Because the way I use Dapper. Keep in mind that I don't explicitly call OPEN/CLOSE.

Is there anything I should be careful about? or change in the way I use Dapper?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source