【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+
[*]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 andits 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 useelement inside your application'selement or inside theelement oforelement. With Tomcat 4.1 and 5.0, parameters forelement should be specified inelement, but with Tomcat 5.5element 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 ofandelements 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 ofruntime environment such as /WEB-INF/classes of your application directory.
For Tomcat 5.5, you don't needelement.
...
...
For Tomcat 5.0 and 5.5, theandcan 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 usingelement in your application's web.xml file like this :
...
Sample Hibernate SessionFactory
hibernate/SampleSessionFactory
org.hibernate.SessionFactory
...
Theelement should be same with the name of resource in the aboveserver.xml or context.xml.
The order ofelement 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]