java SingletonEhCacheRegionFactory 与 EhCacheRegionFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6615790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:38:18  来源:igfitidea点击:

SingletonEhCacheRegionFactory vs EhCacheRegionFactory

javahibernateehcache

提问by Brad Cupit

This link(from the creator of Ehcache) says you should use SingletonEhCacheRegionFactory when you only have one Hibernate SessionFactory, and EhCacheRegionFactory when you have multiple.

这个链接(来自 Ehcache 的创建者)说当你只有一个 Hibernate SessionFactory 时你应该使用 SingletonEhCacheRegionFactory,当你有多个时应该使用 EhCacheRegionFactory。

But wouldn't EhCacheRegionFactory be a single instance anyway when you only have one SessionFactory?

但是,当您只有一个 SessionFactory 时, EhCacheRegionFactory 不会是单个实例吗?

So, what's betterabout SingletonEhCacheRegionFactory? Why not use EhCacheRegionFactory all the time since it can be used for one SessionFactory or multiple?

那么,SingletonEhCacheRegionFactory 有什么好处呢?为什么不一直使用 EhCacheRegionFactory ,因为它可以用于一个或多个 SessionFactory ?

FYI: I'm using Ehcache 2.4.2 and Hibernate 3.6.5

仅供参考:我正在使用 Ehcache 2.4.2 和 Hibernate 3.6.5

回答by Matej

The non-singleton EhCacheRegionFactoryallows you to configure EHCache separately for each Hibernate instance using net.sf.ehcache.configurationResourceNameproperty. The SingletonEhCacheRegionFactoryshares the same EHCache configuration among all Hibernate session factories.

非单例EhCacheRegionFactory允许您使用net.sf.ehcache.configurationResourceName属性为每个 Hibernate 实例单独配置 EHCache 。该SingletonEhCacheRegionFactory股所有的Hibernate会话工厂中相同的EHCache配置。

EHCache documentation recommendsto use the non-singleton version.

EHCache 文档建议使用非单例版本。

Edit:This information applies to EHCache 2.8. Not sure how many things have changed in the latest version.

编辑:此信息适用于 EHCache 2.8。不确定最新版本中发生了多少变化。

回答by Matej

I guess by using SingletonEhCacheRegionFactory you don't need to maintain (or make sure) that EhCacheRegionFactory is a singleton. You can create new SingletonEhCacheRegionFactory() anywhere, and it will still be a singleton within the entire application.

我想通过使用 SingletonEhCacheRegionFactory 你不需要维护(或确保) EhCacheRegionFactory 是一个单例。你可以在任何地方创建 new SingletonEhCacheRegionFactory() ,它在整个应用程序中仍然是一个单例。

While with EhCacheRegionFactory, you need to explicitly configure the object's scope accordingly to make sure that it's singleton.

使用 EhCacheRegionFactory 时,您需要相应地显式配置对象的范围以确保它是单例的。