-->
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.  [ 1 post ] 
Author Message
 Post subject: can't find session factory using HibernateUtil from book
PostPosted: Fri Feb 18, 2005 4:52 pm 
Newbie

Joined: Wed Feb 16, 2005 10:46 am
Posts: 13
Location: Lake Worth, FL
I am using Hibernate 2.1.8 and JBoss 4.0.1. I was using the session facade pattern and everything was working fine. Since I have the Hibernate in Action book, I read chapter 8 and decided to implement the DAO and Command patterns.

JBoss gives the following ...

Code:
...
15:20:06,622 INFO  [SessionFactoryObjectFactory] Factory name: java:/hibernate/SessionFactory
...


So when I used the session facade I hooked up to the session factory :



Code:
   public void ejbCreate() throws CreateException {
        try {
             InitialContext ctx      = new InitialContext();
            _sessionFactory  = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");           
        } catch (NamingException ex) {
           _ctx.setRollbackOnly();
            throw new CreateException("ManagerBean(ejbCreate):  Unable to get a Local Home interface.  "+
               "Probably a local ref or JNDI naming problem.  "+ex.getMessage());
        }      
     }


The session factory was found and used to get a session.

Now, I created a CommandHandlerBean and using HibernateUtil.getSession() in my DAO, i can't find the session factory.

Here is the HibernateUtil code :
Code:
   private static Configuration configuration;
   private static SessionFactory sessionFactory;
   private static final ThreadLocal threadSession = new ThreadLocal();
   private static final ThreadLocal threadTransaction = new ThreadLocal();
   private static final ThreadLocal threadInterceptor = new ThreadLocal();

   // Create the initial SessionFactory from the default configuration files
   static {
      try {
         // we can build the session factory ...
         //configuration = new Configuration();
         //sessionFactory = configuration.configure().buildSessionFactory();
         //System.out.println("session factory is "+sessionFactory);         
         
         // or We could also let Hibernate bind it to JNDI:
         System.out.println("get session factory");
         sessionFactory = getSessionFactory();
      } catch (Throwable ex) {
         // We have to catch Throwable, otherwise we will miss
         // NoClassDefFoundError and other subclasses of Error
         ex.printStackTrace();
         log.error("Building SessionFactory failed.", ex);
         throw new ExceptionInInitializerError(ex);
      }
   }

   /**
    * Returns the SessionFactory used for this static class.
    *
    * @return SessionFactory
    */
   public static SessionFactory getSessionFactory() {
      // Instead of a static variable, use JNDI:
      // /*
      SessionFactory sessionF = null;
      try {
         Context ctx = new InitialContext();
         System.out.println("looking up sf ...");
         sessionF = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
      } catch (NamingException ex) {
         throw new InfrastructureException(ex);
      }
      return sessionF;
      // */

      // Instead of JNDI, use static variable (comment out above code)
      //return sessionFactory;
   }


Even though the JNDI names are exactly the same, now I get the following exception.

Code:
get session factory
looking up sf ...
com.adcware.buers.exception.InfrastructureException: javax.naming.NamingException: Name not found java:/hibernate/SessionFactory
   at com.adcware.buers.util.HibernateUtil.getSessionFactory(HibernateUtil.java:70)
   at com.adcware.buers.util.HibernateUtil.<clinit>(HibernateUtil.java:43)
   at com.adware.buers.DAO.BranchDAO.<init>(BranchDAO.java:25)
   at com.adcware.junit.test.Tester.testCreateBranch(Tester.java:49)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)



Does anyone have a clue as to why the same JNDI name that was found before now is not able to be found?

Many thanks and kind regards,

Joe


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

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.