博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【tomcat】tomcat使用jndi,hibernate自动生成的DAO类用了jndi
阅读量:5997 次
发布时间:2019-06-20

本文共 6249 字,大约阅读时间需要 20 分钟。

在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,需要另外设置过

  官方tomcat7配置jndi

  官方tomcat6配置jndi

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

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.ObjectFactoryimport 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 :

...
factory
myutil.hibernate.HibernateSessionFactoryTomcatFactory
configuration
hibernate-sample.cfg.xml
...

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.

...
...

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 :

...
Sample Hibernate SessionFactory
hibernate/SampleSessionFactory
org.hibernate.SessionFactory
...

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.

 

转载地址:http://sqmlx.baihongyu.com/

你可能感兴趣的文章
STM32F407VG (五)定时器
查看>>
Asp.Net实现JS前台带箭头的流程图方法总结!(个人笔记,信息不全)
查看>>
lambda表达式
查看>>
【Maven安装】centos安装maven
查看>>
第四章 类加载机制
查看>>
hdu4059The Boss on Mars 容斥原理
查看>>
如何对react进行性能优化
查看>>
java对象与json对象间的相互转换
查看>>
网络-数据链路层的三个基本概念
查看>>
Amazon EC2云端服务器的使用方法
查看>>
es6-class
查看>>
DTO
查看>>
cesium之加载地形图Terrain篇
查看>>
【RS】Using graded implicit feedback for bayesian personalized ranking - 使用分级隐式反馈来进行贝叶斯个性化排序...
查看>>
我想学什么技术该怎样学
查看>>
POJ 3155 Hard Life(最小割-最大密集子图)
查看>>
HDU 3516 Tree Construction(四边形不等式)
查看>>
Windows Phone 7 3D开发中使用纹理贴图
查看>>
[转]Android ListView常用用法
查看>>
让treeview控件的滚动条移动到顶部的位置
查看>>