-->
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.  [ 10 posts ] 
Author Message
 Post subject: NPE when looking up the session factory in the JNDI of JBoss
PostPosted: Tue May 16, 2006 11:08 am 
Newbie

Joined: Tue May 16, 2006 10:43 am
Posts: 2
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:
Code:
<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:
Code:
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)




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


Last edited by horia on Wed May 17, 2006 4:32 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 1:39 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Try changing ExampleSessionFactory to java:/hibernate/ExampleSessionFactory in your code and in your service file


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 4:26 am 
Newbie

Joined: Tue May 16, 2006 10:43 am
Posts: 2
The-gtm, thank you for the tip.

I changed the name as you said, but now I get a NameNotFoundException before the NullPointerException, when I try to lookup the factory in the initial context. I've read somewhere that all names starting with "java:/" are local to the VM and therefore cannot be seen by other applications outside the server. But my client application runs on the same machine and on the same instance of the JBoss application server as the Hibernate service!! I can see the name bound to JNDI in the JMX console under Java namespace, like this:
Code:
+- hibernate (class: org.jnp.interfaces.NamingContext)
  |   +- ExampleSessionFactory (class: org.hibernate.impl.SessionFactoryImpl)


So I don't understand why the application code throws this exceptioin:

Code:
Initial SessionFactory creation failed.javax.naming.NameNotFoundException: hibernate not bound
javax.naming.NameNotFoundException: hibernate not bound
   at org.jnp.server.NamingServer.getBinding(NamingServer.java:514)
   at org.jnp.server.NamingServer.getBinding(NamingServer.java:522)
   at org.jnp.server.NamingServer.getObject(NamingServer.java:528)
   at org.jnp.server.NamingServer.lookup(NamingServer.java:252)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
   at sun.rmi.transport.Transport$1.run(Transport.java:153)
   at java.security.AccessController.doPrivileged(Native Method)
   at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
   at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
   at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
   at java.lang.Thread.run(Thread.java:595)
   at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
   at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
   at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
   at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
   at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
   at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
   at javax.naming.InitialContext.lookup(InitialContext.java:351)
   at au.com.tusc.client.TestHibernate.initialize(TestHibernate.java:45)
   at au.com.tusc.client.TestHibernate.testEventEx(TestHibernate.java:61)
   at au.com.tusc.client.TestHibernate.main(TestHibernate.java:88)


If I choose to bind the session factory to a global name, without "java:/" in front of it, I get the NPE as explained in my first post.

Could anyone help with some advice? Thanks a lot in advance.
Horia.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 2:39 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Try changing
InitialContext initialContext = new InitialContext(props);
to
InitialContext initialContext = new InitialContext();


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:20 am 
Newbie

Joined: Thu Feb 02, 2006 10:42 am
Posts: 5
Location: Slovenia
I had similar problem a while ago.

This solved it:
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3881015#3881015

Seems that you must not have duplicate hibernate3.jar in your deployment.

A question for the Hibernate gurus: does anyone know why this happens and maybe if it will be solved? The upper link solves the problem for an application that is deployed inside JBoss AS, but not if I try to access hibernate SessionFactory from an outside application. In a standalone java application I always have a duplicate hibernate3.jar (one in the JBoss AS and one in the application).


Top
 Profile  
 
 Post subject: Similar issue
PostPosted: Fri Jan 05, 2007 11:23 am 
Newbie

Joined: Fri Jan 05, 2007 11:15 am
Posts: 3
I am having the exact same issue as the original post. I read the thread on jboss forum where it says to remove the duplicate hibernate3.jar. But I don't have anything deployed at. I am executing a simple java application from eclipse and trying to lookup SessionFactory. I checked all the jboss directories and I only see one hibernate3.jar

Any ideas on what it could be? Has this been resolved in Jboss5 Beta1 ?

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 12:18 pm 
Regular
Regular

Joined: Thu Aug 17, 2006 4:50 am
Posts: 55
Location: Mallorca
Quote:
I checked all the jboss directories and I only see one hibernate3.jar


link provided by simon_c talks about hibernate jar or dependency libs (cglib, dom4j, etc), not only hibernate.3.jar.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 3:33 pm 
Newbie

Joined: Fri Jan 05, 2007 11:15 am
Posts: 3
Thanks for the reply dominicus.

I checked the dependency jars as well. Some of the jars are in two places, one in JBOSS\client and other in JBOSS\server\default\lib.

I checked the following jars:

cglib.jar - only one in JBOSS\server\default\lib
dom4j.jar - only one in JBOSS\server\default\lib
asm.jar - only one in JBOSS\server\default\lib
asm-attrs.jar - only one in JBOSS\server\default\lib
antlr-2.7.6.jar - present in TWO places, one in JBOSS\client and other in JBOSS\server\default\lib

commons-logging - present in Three places, one in JBOSS\client,JBOSS\server\default\lib,JBOSS\lib

I see this message in the jboss boot log:

11:29:41,234 INFO [SessionFactoryImpl] building session factory
11:29:41,250 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
11:29:41,250 INFO [NamingHelper] JNDI InitialContext properties:{}
11:29:41,265 INFO [Hibernate] SessionFactory successfully built and bound into JNDI [hibernate/SessionFactory]


Any further pointers ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 1:13 pm 
Regular
Regular

Joined: Thu Aug 17, 2006 4:50 am
Posts: 55
Location: Mallorca
You are using hibernate3 but in the mbean service you have declared hibernate2.

Code:
<mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=[b]Hibernate2[/b]">


Is that correct ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 4:37 pm 
Newbie

Joined: Fri Jan 05, 2007 11:15 am
Posts: 3
Yes, I did have it as 'Hibernate2' but I tried putting 'Hibernate' and 'Hibernate3' as shown in the example at:


[url]http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossHibernate3
[/url]

And it does not seem to help.

Thanks for your help dominicus


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.