Hello forum, I'm configuring Hibernate to use JNDI properties. So, in hibernate.cfg.xml I set
session_factory_name to
"java:/hibernate/webshop". Mmm, I thank that if I set session_factory_name, the SessionFactory will be automatically bounded to this name in JNDI after it has been created. So, I've created a ServletContextListener as follow -->
Code:
/**
*
* @author Jordi
* @web.listener
* name="initContext"
* display-name="initContext"
* description="Inicialització de l'aplicació web."
*/
public class ServletContextInitializer implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Context init");
String[] files = new java.io.File("/").list();
for (int i = 0;i < files.length; i++) {
System.out.println(files[i]);
}
try {
new Configuration().configure("./webshop/WEB-INF/classes/hibernate.cfg.xml").buildSessionFactory();
}catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("End Context init");
}
}
So, when a context is initialized, I create a new sessionFactory and automatically would has to be bounded. But when I want to access to this SessionFactory ("java:/hibernate/webshop") tomcat dumps me a error -->
Quote:
Could not locate SessionFactory in JNDI
java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
Here is DAO class that want to access to SessionFactory as JNDI name-->
Code:
public class StatusDAO {
private static final Log log = LogFactory.getLog(StatusDAO.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("java:/hibernate/webshop");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(Status transientInstance) {
log.debug("persisting Status instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
Why Tomcat doesn't find my SessionFactory?
Thanks.
hibernate.cfg.xml -->
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">micbar2</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@80.37.210.66:1500:webshop</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.default_schema">APP</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="session_factory_name">java:/hibernate/webshop</property>
<mapping resource="com/gmsoft/model/Status.hbm.xml" />
</session-factory>
</hibernate-configuration>