hibernate sessionfactory 作为全局 jndi 资源

发布于 2024-12-26 06:44:14 字数 1260 浏览 1 评论 0原文

我在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2025-01-02 06:44:14

我通过使用 LifecycleListener 在启动时创建会话工厂的单例实例解决了该问题。

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactorys implements LifecycleListener  {

    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public void lifecycleEvent(LifecycleEvent arg0) {
        if (Lifecycle.AFTER_START_EVENT==arg0.getType()) {
            sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        }
        if (Lifecycle.BEFORE_STOP_EVENT==arg0.getType()) {  
            sessionFactory.close();
        }
    }

}

I have solved the problem by using a LifecycleListener to create a singleton instance of session factory on start up.

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactorys implements LifecycleListener  {

    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public void lifecycleEvent(LifecycleEvent arg0) {
        if (Lifecycle.AFTER_START_EVENT==arg0.getType()) {
            sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        }
        if (Lifecycle.BEFORE_STOP_EVENT==arg0.getType()) {  
            sessionFactory.close();
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文