[英]NHibernate and potential caching problems
Ok so I have an n tiered model. 好的,所以我有一个n层模型。 (WPF, Asp.Net, etc) to backend services via WCF. (WPF,Asp.Net等)通过WCF进行后端服务。 These services use NHibernate to communicate with the database. 这些服务使用NHibernate与数据库进行通信。 Some of these services will be run in InstanceContextMode.Single mode. 其中一些服务将在InstanceContextMode.Single模式下运行。
Questions: 问题:
1-3: As far as I know, ISession objects are supposed to be light-weight, short-lived objects, which live only for the duration for which they're needed. 1-3:据我所知,ISession对象应该是重量轻,寿命短的对象,它们仅在需要它们的持续时间内存在。 I would advise AGAINST using the same ISession object for the whole lifetime of your service. 我建议AGAINST在您的服务的整个生命周期中都使用相同的ISession对象。
What I would suggest instead is using the same ISeessionFactory instance, and creating new ISessions from it as necessary (you can try something similar to Session-Per-Request pattern). 我建议使用的是相同的ISeessionFactory实例,并根据需要从中创建新的ISession(您可以尝试使用类似于Session-Per-Request模式的方法)。
If you enable 2nd level cache, you can have all the benefits of caching in this scenario. 如果启用第二级缓存,则在这种情况下可以享受缓存的所有好处。
5 Yep, pretty much. 5是的,差不多。 Also remember that 2nd level cache instance is per ISessionFactory instance . 还要记住,第二级缓存实例是每个ISessionFactory实例 。 that means that if you're using more than 1 ISessionFactory instance you'll have a lot of problems with your cache. 这意味着,如果您使用多个ISessionFactory实例,则您的缓存会遇到很多问题。
6 for 1st level cache you don't need to define anything. 对于1级缓存,您不需要定义6。
for 2nd level cache you need to enable the cache when you configure nHibernate (fluently, in my case): 对于二级缓存,您需要在配置nHibernate时启用缓存(以我为例):
.Cache(c => c.UseQueryCache()
.ProviderClass(
isWeb ? typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName //in web environment- use sysCache2
: typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName //in dev environmet- use stupid cache
))
)
and specify for each entity and each collection that you want to enable cache for them: 并为您要为其启用缓存的每个实体和每个集合指定:
mapping.Cache.ReadWrite().Region("myRegion");
and for a collection: 并进行收集:
mapping.HasMany(x => x.Something)
.Cache.ReadWrite().Region("myRegion");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.