hibernate sessionfactory 作为全局 jndi 资源
我在一个 tomcat 实例中运行多个上下文,每个上下文都需要访问同一数据库。
我遇到了兑现问题,因为目前每个上下文都有自己的 hibernate 和 ehcache 实例。
这似乎是错误的,它们应该只是 hibernate 和 ehcache 的一个实例,这也会有更好的性能。
我想让 hibernate 会话工厂的单个实例可用于所有上下文,我认为这可以使用 tomcat 中的全局 JNDI 资源来完成。
这是解决这个问题的好方法吗?
此外,如果有人可以提供任何好的资源来学习如何做到这一点,我们将不胜感激。
更新:我已成功将会话工厂绑定到全局 JNDI,但在 tomcat 启动期间日志中出现 ConcurrentModificationException。
...
INFO: schema update complete
Jan 11, 2012 2:03:19 PM org.hibernate.cache.UpdateTimestampsCache <init>
INFO: starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache
Jan 11, 2012 2:03:19 PM org.hibernate.cache.StandardQueryCache <init>
INFO: starting query cache at region: org.hibernate.cache.StandardQueryCache
Constructed session factory ok sf=org.hibernate.impl.SessionFactoryImpl@430e0ad7
Jan 11, 2012 2:03:19 PM org.apache.catalina.mbeans.GlobalResourcesLifecycleListener createMBeans
SEVERE: RuntimeException java.util.ConcurrentModificationException
Jan 11, 2012 2:03:19 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 11, 2012 2:03:19 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.23
...
I have a multiple contexts running in one tomcat instance each context need access to the same database.
I am running into problems with cashing because each context has its own instance of hibernate and ehcache at the moment.
This seems wrong they should only be one instance of hibernate and ehcache , this would also have better performance.
I would like to make a single instance of hibernate session factory available to all contexts, I think this can be done using a global JNDI resource in tomcat.
Is this a good way to solve this problem?
Also if anybody can provide any good resources for learning how to do this it would be much appreciated.
Update: I have managed to bind session factory to a global JNDI but a ConcurrentModificationException appears in the log during startup of tomcat.
...
INFO: schema update complete
Jan 11, 2012 2:03:19 PM org.hibernate.cache.UpdateTimestampsCache <init>
INFO: starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache
Jan 11, 2012 2:03:19 PM org.hibernate.cache.StandardQueryCache <init>
INFO: starting query cache at region: org.hibernate.cache.StandardQueryCache
Constructed session factory ok sf=org.hibernate.impl.SessionFactoryImpl@430e0ad7
Jan 11, 2012 2:03:19 PM org.apache.catalina.mbeans.GlobalResourcesLifecycleListener createMBeans
SEVERE: RuntimeException java.util.ConcurrentModificationException
Jan 11, 2012 2:03:19 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 11, 2012 2:03:19 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.23
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用 LifecycleListener 在启动时创建会话工厂的单例实例解决了该问题。
I have solved the problem by using a LifecycleListener to create a singleton instance of session factory on start up.