EF DBContext处理不关闭连接 [英] EF DBContext dispose not closing the connection

查看:657
本文介绍了EF DBContext处理不关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF 6.1.0

I am using the EF 6.1.0

我在下面的自定义DBContex对象中将其作为DBEntites

I have below custom DBContex object as DBEntites

public partial class DbEntities : DbContext
{
    public DbEntities()
        : base("name=DbEntities")
    {
        ////Configuration.LazyLoadingEnabled = true;
        ////Configuration.ProxyCreationEnabled = false;
    }

    //// I have ALL Entites added as below
    ////public virtual IDbSet<CCode> CCodes { get; set; }
}

我对上下文对象有以下操作

I have the below operations on context object

using (var context = new DbEntities())
        {
            var entitySet = context.Set<T>();
            var res = entitySet.Where<T>(predicate).ToList();
            if (context.Database.Connection.State == ConnectionState.Open)
            {
                context.Database.Connection.Close();
            }

            return res;
        }

但是在配置上下文对象之后,我仍然可以看到活动的数据库连接。在连接状态下,我可以看到该连接已经关闭(该连接从未为真)。

But after disposing the context object still i can see a active DB Connection. On connection state condition i can see that the connection is already closed(the connection had never true).

我正在使用以下查询查看SQL上的连接。 / p>

I am using the below query to see the connection on SQL.

select db_name(dbid) , count(*) 'connections count'
from master..sysprocesses
where spid > 50 and spid != @@spid
group by db_name(dbid)
order by count(*) desc

在以下语句中,SQL连接计数增加。但是,即使在处置之后,它也从未消失过。 (我的意思是在使用块计算后,它应该关闭连接。)

At the below statement a sql connection count increased. But it was never down even after disposing . (I mean after using block excuted it supposed to close the connection).

var res = entitySet.Where<T>(predicate).ToList();

任何帮助将不胜感激。

推荐答案

正如注释中指出的那样,其原因确实是.NET执行的连接池。出于性能方面的原因,.NET会为您在应用程序中使用的每个连接字符串维护一个连接池(因为打开和关闭连接在性能方面通常可能会很昂贵)。该池具有一定的最小和最大大小(由 MinPoolSize MaxPoolSize 连接字符串参数控制)。当您打开一个连接(通过 SqlConnection.Open )时-它可能会从池中移出,并没有真正重新打开。当您关闭连接时(也可以通过放置EF上下文来完成)-连接可能会改为放入池中,并没有真正关闭。当连接闲置了一定时间(大约5分钟)时,它可能会从池中删除。

As was figured out in comments, the reason is indeed connection pooling performed by .NET. .NET maintains a pool of connections for every connection string you use in your application, for perfomance reasons (because opening and closing connections often might be costly in terms of perfomance). That pool has certain minimum and maximum size (controlled by MinPoolSize and MaxPoolSize connection string parameters). When you open a connection (via SqlConnection.Open) - it might be taken out of the pool and not really opened afresh. When you close connection (which is also done by disposing EF context) - connection might be put into the pool instead, and not really closed. When connection is idle for certain time (about 5 minutes) - it might be removed from the pool.

如果您(出于某种原因)想要避免这种情况,则可以将连接字符串的MaxPoolSize设置为0,或者通过 SqlConnection.ClearPool SqlConnection.ClearAllPools 显式清除池。

If you (for some reason) want to avoid that, you can either set MaxPoolSize to 0 for your connection string, or clear pool explicitly by SqlConnection.ClearPool or SqlConnection.ClearAllPools.

这篇关于EF DBContext处理不关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆