Hibernate SessionFactory:如何在Tomcat中配置JNDI?

发布于 2024-08-25 17:18:59 字数 352 浏览 3 评论 0原文

这就是会话工厂的获取方式:

    protected SessionFactory getSessionFactory() {
        try {
            return (SessionFactory) new InitialContext()
                    .lookup("SessionFactory");
        } catch (Exception e) {
        }
    }

请提供一个简单的解决方案,让Tomcat6能够获取SessionFactory 通过 Java 代码中的简单 jndi 查找。 Tomcat这边的什么文件应该写什么?

that's how the session factory should be gotten:

    protected SessionFactory getSessionFactory() {
        try {
            return (SessionFactory) new InitialContext()
                    .lookup("SessionFactory");
        } catch (Exception e) {
        }
    }


Please provide a simple solution for Tomcat6 to be able to get SessionFactory
thru simple jndi lookup in Java code.
What should be written in what file on the side of Tomcat ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

晨曦慕雪 2024-09-01 17:18:59

这是一个链接
但是

其他答案也欢迎。

Here's a link
http://community.jboss.org/wiki/UsingJNDI-boundSessionFactorywithTomcat41

But other answers are welcome as well.

花桑 2024-09-01 17:18:59

Tomcat 文档

Tomcat提供了一个只读的InitialContext,而Hibernate需要
读写以管理多个会话工厂。雄猫是
显然遵循非托管容器的规范。如果
如果您想将会话工厂绑定到 JNDI 对象,您可以
必须转移到托管服务器(Glassfish、JBoss 等),或搜索
在 Internet 上查找一些已发布的解决方法。

Hibernate 文档的建议是离开
使用时输出 hibernate.session_factory_name 属性
Tomcat 不尝试绑定到 JNDI。

Hibernate 文档说相同

将 SessionFactory 绑定到 JDNI 命名空间非常有用。在
大多数情况下,可以使用 hibernate.session_factory_name
您的配置中的属性。但是,对于 Tomcat 你不能使用
hibernate.session_factory_name 属性,因为Tomcat提供
只读 JNDI 实现。将 JNDI 绑定的 SessionFactory
Tomcat,您应该为其编写自定义资源工厂类
SessionFactory 并设置 Tomcat 的配置。

所以你需要像这样定制 SessionFactory :

package myutil.hibernate;  

import java.util.Hashtable;  
import java.util.Enumeration;  
import javax.naming.Name;  
import javax.naming.Context;  
import javax.naming.NamingException;  
import javax.naming.Reference;  
import javax.naming.RefAddr;  
import javax.naming.spi.ObjectFactory  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  

public class HibernateSessionFactoryTomcatFactory implements ObjectFactory{  
   public Object getObjectInstance(Object obj, Name name, Context cntx, Hashtable env)   
                 throws NamingException{  

      SessionFactory sessionFactory = null;  
      RefAddr addr = null;  

      try{  
         Enumeration addrs = ((Reference)(obj)).getAll();  

         while(addrs.hasMoreElements()){  
            addr = (RefAddr) addrs.nextElement();  
            if("configuration".equals((String)(addr.getType()))){  
               sessionFactory = (new Configuration())  
                    .configure((String)addr.getContent()).buildSessionFactory();  
            }  
         }  
      }catch(Exception ex){  
         throw new javax.naming.NamingException(ex.getMessage());  
      }  

      return sessionFactory;  
   }  
}  

Tomcat documentation says:

Tomcat provides a read-only InitialContext, while Hibernate requires
read-write in order to manage multiple session factories. Tomcat is
apparently following the specification for unmanaged containers. If
you want to bind the session factory to a JNDI object, you'll either
have to move to a managed server (Glassfish, JBoss, etc.), or search
on the Internet for some posted work-arounds.

The recommendation from the Hibernate documentation is to just leave
out the hibernate.session_factory_name property when working with
Tomcat to not try binding to JNDI.

And Hibernate documentation says the same:

It is very useful to bind your SessionFactory to JDNI namespace. In
most cases, this is possible to use hibernate.session_factory_name
property in your configuration. But, with Tomcat you cann't use
hibernate.session_factory_name property, because Tomcat provide
read-only JNDI implementation. To use JNDI-bound SessionFactory with
Tomcat, you should write custom resource factory class for
SessionFactory and setup it Tomcat's configuration.

So you need to make custom SessionFactory like that:

package myutil.hibernate;  

import java.util.Hashtable;  
import java.util.Enumeration;  
import javax.naming.Name;  
import javax.naming.Context;  
import javax.naming.NamingException;  
import javax.naming.Reference;  
import javax.naming.RefAddr;  
import javax.naming.spi.ObjectFactory  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  

public class HibernateSessionFactoryTomcatFactory implements ObjectFactory{  
   public Object getObjectInstance(Object obj, Name name, Context cntx, Hashtable env)   
                 throws NamingException{  

      SessionFactory sessionFactory = null;  
      RefAddr addr = null;  

      try{  
         Enumeration addrs = ((Reference)(obj)).getAll();  

         while(addrs.hasMoreElements()){  
            addr = (RefAddr) addrs.nextElement();  
            if("configuration".equals((String)(addr.getType()))){  
               sessionFactory = (new Configuration())  
                    .configure((String)addr.getContent()).buildSessionFactory();  
            }  
         }  
      }catch(Exception ex){  
         throw new javax.naming.NamingException(ex.getMessage());  
      }  

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