hibernate sessionfactory作为全球jndi资源 [英] hibernate sessionfactory as a global jndi resource

查看:111
本文介绍了hibernate sessionfactory作为全球jndi资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个tomcat实例中运行多个上下文,每个上下文都需要访问同一个数据库。



我遇到了兑现问题,因为每个上下文都有自己的hibernate和ehcache的实例。

这似乎是错误的,它们应该只是hibernate和ehcache的一个实例,这也会有更好的性能。 b
$ b

我想让一个hibernate会话工厂的单个实例可用于所有上下文,我认为这可以在tomcat中使用全局JNDI资源完成。



这是解决这个问题的好方法吗?

如果有人可以提供任何好的资源来学习如何做到这一点, 。更新:我已经设法将会话工厂绑定到全局JNDI,但在tomcat启动期间日志中会出现ConcurrentModificationException异常。

  ... 
INFO:架构更新完成
2012年1月11日下午2:03:19 PM org.hibernate.cache.UpdateTimestampsCache< init>
INFO:在地区开始更新时间戳缓存:org.hibernate.cache.UpdateTimestampsCache
2012年1月11日下午2时03分19秒org.hibernate.cache.StandardQueryCache< init>
INFO:在地区启动查询缓存:org.hibernate.cache.StandardQueryCache
构造会话工厂正常sf=org.hibernate.impl.SessionFactoryImpl@430e0ad7
2012年1月11日2:03: 19下午org.apache.catalina.mbeans.GlobalResourcesLifecycleListener createMBeans
SEVERE:RuntimeException java.util.ConcurrentModificationException
2012年1月11日下午2时03分19秒org.apache.catalina.core.StandardService startInternal
INFO:启动服务Catalina
一月11,2012 2:03:19 PM org.apache.catalina.core.StandardEngine startInternal
INFO:启动Servlet引擎:Apache Tomcat / 7.0.23
...


解决方案

LifecycleListener在启动时创建会话工厂的单例实例。

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

公共类SessionFactorys实现LifecycleListener {

private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory(){
return sessionFactory;

$ b @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 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
...

解决方案

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();
        }
    }

}

这篇关于hibernate sessionfactory作为全球jndi资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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