2 Replies Latest reply on May 26, 2006 3:04 AM by richardyang

    NPE when looking up the Hibernate session factory in the JND

    kaage

      Hi,

      I'm posting here a message I wrote on the Hibernate forum, but where I could not find any solution. In fact, the problem has more to do with JBoss than with Hibernate, I guess.

      Hi,

      I'm using Hibernate 3.1 together with JBoss 4.0.3. I've deployed Hibernate as a MBean in JBoss and I've bound the session factory into the JNDI (I can see it with the JMX console). The problem occurs when I'm trying to make use of my factory from an application client inside an EAR. I wan to get my factory from the JNDI and not from a Hibernate configuration file. I obtain a null object when looking up the factory in the initial context. I've bound the factory with a global JNDI name, so normally it should work for any namespace inside the VM (and outside too).

      Here it is the hibernate service configuration file:

      <server>
       <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate2">
       <attribute name="DatasourceName">java:/DefaultDS</attribute>
       <attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute>
       <attribute name="SessionFactoryName">
       ExampleSessionFactory
       </attribute>
       <attribute name="CacheProviderClass">
       org.hibernate.cache.NoCacheProvider
       </attribute>
      
       <attribute name="Hbm2ddlAuto">create-drop</attribute>
       <attribute name="ShowSqlEnabled">false</attribute>
       </mbean>
      </server>
      


      .. and here the client code which throws NPE:


      public class TestHibernate {
      
       private static InitialContext ctx;
       private static String exFactoryLocation = "ExampleSessionFactory";
       private static SessionFactory sessionFactory;
      
       private InitialContext getContext() throws NamingException {
       Hashtable props = new Hashtable();
       props.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
       props.put(InitialContext.PROVIDER_URL, "jnp://localhost:1099");
       InitialContext initialContext = new InitialContext(props);
      
       return initialContext;
       }
      
       private void initialize(){
       try {
       ctx = getContext();
       System.out.println("Context is null:" + (ctx==null)); // here the context is not null
       } catch (Exception e) {
       // handle exception
       System.out.println("Initial Context creation failed. ");
       e.printStackTrace();
       }
       try {
       Object obj = ctx.lookup(exFactoryLocation);
       System.out.println("Object is null:" + (obj==null)); // here obj is null, but no exception! :((
       sessionFactory = (SessionFactory)obj;
       } catch (Exception ex) {
       // Make sure you log the exception, as it might be swallowed
       System.out.println("Initial SessionFactory creation failed." + ex);
       ex.printStackTrace();
       }
      
       }
      
       public void testEventEx(){
      
       initialize();
       Date today = new Date();
       try {
       Session session = sessionFactory.openSession(); // here I get NPE!
       session.beginTransaction();
       ....
      
       } catch (Exception e) {
       System.out.println("Error by retrieving current session: " + e);
       e.printStackTrace();
       }
       }
      


      And here it is the console output:


      Context is null:false
      log4j:WARN No appenders could be found for logger (org.hibernate.impl.SessionFactoryObjectFactory).
      log4j:WARN Please initialize the log4j system properly.
      Object is null:true
      Error by retrieving current session: java.lang.NullPointerException
      java.lang.NullPointerException
      at au.com.tusc.client.TestHibernate.testEventEx(TestHibernate.java:65)
      at au.com.tusc.client.TestHibernate.main(TestHibernate.java:88)


      Has the problem anything to do with the fact that I'm calling the session factory from another process than the hibenate service?

      Could anyone please help me with that? Thank you!
      Horia.