zoukankan      html  css  js  c++  java
  • 【tomcat】tomcat使用jndi,hibernate自动生成的DAO类用了jndi

    在tomcat中,还是不太建议使用jndi。数据库连接有其他方式。

    public class TbHospitalHome {
    
        private static final Log log = LogFactory.getLog(TbHospitalHome.class);
    
        private final SessionFactory sessionFactory = getSessionFactory();
    
        protected SessionFactory getSessionFactory() {
            try {
                return (SessionFactory) new InitialContext()
                        .lookup("SessionFactory");
            } catch (Exception e) {
                log.error("Could not locate SessionFactory in JNDI", e);
                throw new IllegalStateException(
                        "Could not locate SessionFactory in JNDI");
            }
        }
    
        public void persist(TbHospital transientInstance) {
            log.debug("persisting TbHospital instance");
            try {
                sessionFactory.getCurrentSession().persist(transientInstance);
                log.debug("persist successful");
            } catch (RuntimeException re) {
                log.error("persist failed", re);
                throw re;
            }
        }
      ... }

    这一段代码是hibernate自动生成的,使用了jndi。

    而在tomcat里面,要使用jndi,需要另外设置过

    http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html  官方tomcat7配置jndi

    http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html  官方tomcat6配置jndi

    https://community.jboss.org/wiki/UsingJNDI-boundSessionFactorywithTomcat41?_sscc=t

    这是另外一篇外文的方法:

    Using JNDI-bound SessionFactory with Tomcat 4.1+

    1. Write Resource Factory Class

    The resource factory should implement javax.naming.spi.ObjectFactory inteface. This interface declares only one method getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment). For Tocmat, configured properties are contained obj parameter and  its type is always javax.naming.Reference.

    The resource factory would be like this.

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

    2. Configure the Resource Factory with Tomcat in server.xml

    To use above resource factory, you should configure both server.xml and web.xml.

    In server.xml, you can use <Resource> element inside your application's <Context> element or inside the <DefaultContext> element of <Host> or <Engine> element. With Tomcat 4.1 and 5.0, parameters for <Resource> element should be specified in <ResourceParams> element, but with Tomcat 5.5 <Resource> element can specify its parameters as attributes.

    For Tomcat 4.1 and 5.0, resource factory confiuration in a server.xml would be like this :

    <Context ...>
       ...
       <Resource name="hibernate/SampleSessionFactory" auth="Container"
                 type="org.hibernate.SessionFactory"/>
       <ResourceParams name="hibernate/SampleSessionFactory">
       <parameter>
          <name>factory</name>
          <value>myutil.hibernate.HibernateSessionFactoryTomcatFactory</value>
       </parameter>
       <parameter>
          <name>configuration</name>
          <value>hibernate-sample.cfg.xml</value>
       </parameter>
       </ResourceParams>
       ...
    </Context>

    Note the followings

    • The name of <Resource> and <ResourceParams> elements should be same and factory and configuration parameter should be specified.
    • The value of name attribute should be same with JNDI name in web.xml.
    • The value of type attribute should be org.hibernate.SessionFactory.
    • The value for factory parameter should be above resource factory and is used by Tomcat implicitly.
    • The value for configuration is used in above resource factory source code, so if you change the name, resource factory code should be modified.
    • hibernate-sample.cfg.xml should be located on your classpath of  runtime environment such as /WEB-INF/classes of your application directory.

     

    For Tomcat 5.5, you don't need <ResourceParams> element.

    <Context ...>
       ...
       <Resource name="hibernate/SampleSessionFactory"
                 auth="Container"
                 type="org.hibernate.SessionFactory"
                 factory="myutil.hibernate.HibernateSessionFactoryTomcatFactory"
                 configuration="hibernate-sample.cfg.xml"/>
       ...
    </Context>

    For Tomcat 5.0 and 5.5, the <Resource> and <ResourceParams> can be configured in per-web-application context XML file of Tomcat. The file should be located and named as /META-INF/context.xml under the application directory and its root element is <Context>

    3. Configure Web Application in web.xml

    Declare the JNDI name for the SessionFactory using <resource-env-ref> element in your application's web.xml file like this :

    <web-app ...>
    ...
       <resource-env-ref>
          <description>Sample Hibernate SessionFactory</description>
          <resource-env-ref-name>hibernate/SampleSessionFactory</resource-env-ref-name>
          <resource-env-ref-type>org.hibernate.SessionFactory</resource-env-ref-type>
       </resource-env-ref>
    ...
    </web-app>

    The <resource-env-ref-name> element should be same with the name of resource in the above  server.xml or context.xml.

    The order of <resource-env-ref-name> element in web.xml is different according to the version of Tomcat. For Tomcat 4.1 based on Servlet 2.3, <resource-env-ref-name> precedes <resource-ref>, <env-entry>, or <ejb-ref>. But, for Tomcat 5.0 and 5.5 which are based on Servlet 2.4, <resource-env-ref-name> should be after <env-entry>, <ejb-ref>, and <resource-ref>. (I don't understand why they change the order of elements in deployment descriptor.)

    4. Use JNDI Lookup for the SessionFactory

    Finally, you can get your hibernate SessionFactory using JNDI lookup in your application. Typical code snippet would like following :

    ...
    Context initialCntx = new InitialContext();
    SessionFactory sessionFactory = (SessionFactory)initialCntx
         .lookup("java:comp/env/hibernate/SampleSessionFactory");
    Session hibernateSess = sessionFactory.getCurrentSession();
    ...

    I tested above sample with Tomcat 5.5 and It worked correctly. It would work corretly with Tomat 4.1 and 5.0.

    The method getObjectInstance of resource factory is invoked at only first lookup for each declared resource in server.xml.  After first lookup, the resource remains bound to JNDI context, so Tomcat wouldn't invoke resource factory.

  • 相关阅读:
    nyoj 17 单调递增最长子序列
    nyoj 18 The Triangle
    nyoj 712 探 寻 宝 藏
    nyoj 61传纸条(一)
    nyoj 269 VF
    nyoj 44 子串和
    nyoj 252 01串
    nyoj 42 一笔画问题
    nyoj 756 重建二叉树
    Table 样式设置
  • 原文地址:https://www.cnblogs.com/549294286/p/2998640.html
Copyright © 2011-2022 走看看