设为首页 收藏本站
查看: 595|回复: 0

[经验分享] 【tomcat】tomcat使用jndi,hibernate自动生成的DAO类用了jndi

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-9 09:41:50 | 显示全部楼层 |阅读模式
  在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+




  • Using JNDI-bound SessionFactory with Tomcat 4.1+

    • 1. Write Resource Factory Class
    • 2. Configure the Resource Factory with Tomcat in server.xml
    • 3. Configure Web Application in web.xml
    • 4. Use JNDI Lookup for the SessionFactory
    • Resources


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  element inside your application's  element or inside the  element of  or  element. With Tomcat 4.1 and 5.0, parameters for  element should be specified in  element, but with Tomcat 5.5  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  and  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  element.




...

...

  For Tomcat 5.0 and 5.5, the  and  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
  

3. Configure Web Application in web.xml
  Declare the JNDI name for the SessionFactory using  element in your application's web.xml file like this :




...

Sample Hibernate SessionFactory
hibernate/SampleSessionFactory
org.hibernate.SessionFactory

...

  The  element should be same with the name of resource in the above  server.xml or context.xml.
  The order of  element in web.xml is different according to the version of Tomcat. For Tomcat 4.1 based on Servlet 2.3,  precedes , , or . But, for Tomcat 5.0 and 5.5 which are based on Servlet 2.4,  should be after , , and . (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.
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-96263-1-1.html 上篇帖子: suse10.1下tomcat的安装与配置 下篇帖子: eclipse+tomcat+maven调式环境配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表