asp.net mvc 4-可以共享每个线程的DbContext吗? [英] asp.net mvc 4 - Okay to share DbContext per thread?

查看:101
本文介绍了asp.net mvc 4-可以共享每个线程的DbContext吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自每个Web请求一个DbContext ...为什么?

我的理解是,不应在并发Web请求之间共享DbContext实例,因此绝对不能在线程之间共享。
但是如何在非并行Web请求中共享它呢?

My understanding is that a DbContext instance should not be shared across concurrent web request, so definitely not across threads. But how about sharing it across non-concurrent web requests?

由于线程敏捷性 ASP.Net中的线程敏捷性是什么意思?),

如果这样,一个线程可以在死之前处理多个Web请求吗?如果这样,为每个线程依赖注入一个DbContext实例是否安全? ?

原因是我使用的是Unity,其中不包括每个请求生命周期选项。
来自 MVC,EF-DataContext单例实例在Unity中按网络请求,我想我可以使用自定义的LifetimeManager;我只是想知道使用PerThreadLifetimeManager是否安全且足够。

Reason for this is I'm using Unity, which does not include per-request lifetime option. From MVC, EF - DataContext singleton instance Per-Web-Request in Unity , I think I could use a custom LifetimeManager; I'm just wondering if it is safe and sufficient to use PerThreadLifetimeManager.

推荐答案


是否安全?依赖为每个线程注入一个DbContext实例?

is it safe to dependency inject a DbContext instance for each thread?

这取决于。如果您的想法是每个Web请求有一个 DbContext ,而您的应用程序的一致性取决于它,则有一个 DbContext 每个线程都是一个坏主意,因为单个Web请求仍然可以获取多个DbContext实例。 并且由于ASP.NET池线程,因此每个线程缓存的实例将在整个应用程序期间有效,这对于 DbContext (如在此处)中解释。

It depends. If your idea is to have one DbContext per web request, and the consistency of your application depends on it, having one DbContext per thread is a bad idea, since a single web request can still get multiple DbContext instances. And since ASP.NET pools threads, instances that are cached per thread, will live for the duration of the entire application, which is very bad for a DbContext (as explained here).

另一方面,您可能可以提出一种缓存方案,以确保单个 DbContext 用于单个Web请求,并在请求完成后返回到池中,以便其他Web请求可以将其接收。基本上,这就是.NET中连接池的工作方式。但是,由于 DbContext 实例缓存数据,该数据变得非常陈旧,因此,即使您能够提出线程安全的解决方案,您的系统仍然会在不一致的情况下运行这样,由于在某些看似随机的时刻,向用户显示了旧数据,而在随后的请求中,则显示了新数据。

On the other hand, you might be able to come up with a caching scheme that ensures that a single DbContext is used for a single web request, and is returned to a pool when the request is finished, so other web requests could pick it up. This is basically the way how connection pooling in .NET works. But since DbContext instances cache data, that data becomes stale very quickly, so even if you are able to come up with a thread-safe solution, your system still behaves in an inconsistent way, since at some seemingly random moments, old data is shown to the user, while in a following request new data is shown.

我认为可以清除Web请求开始时 DbContext 的缓存,但这基本上与创建新的 DbContext 对于该请求,但是性能会下降很多。

I think it is possible to clear the DbContext's cache at the beginning of a web request, but that would be basically be the same as creating a new DbContext for that request, but with the downside of much slower performance.


我只是想知道使用PerThreadLifetimeManager是否安全且足够

I'm just wondering if it is safe and sufficient to use PerThreadLifetimeManager.

不,由于上述原因,它并不安全。

No, it isn't safe because of the reasons described above.

但是实际上很容易在每个Web请求的基础上注册DbContext:

But it is actually quite easy to register a DbContext on a per-web request basis:

container.Register<MyApplicationEntities>(new InjectionFactory(c => {
    var context = (MyApplicationEntities)HttpContext.Current.Items["__dbcontext"];

    if (context == null) {
        context = new MyApplicationEntities();
        HttpContext.Current.Items["__dbcontext"] = context;
    }

    return context;
})); 

这篇关于asp.net mvc 4-可以共享每个线程的DbContext吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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