-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: session_factory_name.Cant access to SessionFactory with JNDI
PostPosted: Thu Nov 02, 2006 6:56 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
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>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 10:23 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Tomcat's JNDI tree is read-only. You can't bind your SessionFactory to Tomcat's JNDI tree.

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 6:14 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
Solved thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.