This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes glassfish vs. Hibernate: JNDI configuration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "glassfish vs. Hibernate: JNDI configuration " Watch "glassfish vs. Hibernate: JNDI configuration " New topic
Author

glassfish vs. Hibernate: JNDI configuration

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Hi,

I'm compiling a little project deployable as a enterprise archive using Hibernate and glassfish. I have troubles getting JNDI to work.

Here's my hibernate.cfg.xml


Here's my session factory:


I did configure a connection pool and a jndi-Name at the server's admin console, but I do get the following exception:

javax.naming.NameNotFoundException: No object bound to name java:comp/env/hibernate/SessionFactory

I'm confident that it's just a minor naming problem, but I can't figure it out. Any help is appreciated.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
sessionFactory = (SessionFactory) new InitialContext().lookup("java:comp/env/hibernate/SessionFactory");


How about:

<session-factory name="hibernate/SessionFactory">
[ September 15, 2008: Message edited by: James Clark ]
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
I tried

<session-factory name="hibernate/SessionFactory">

and didn't change my util class, same error.

I change the util class to

sessionFactory = (SessionFactory) new InitialContext().lookup("hibernate/SessionFactory");

The result is that the exception is slightly different, it only says:

javax.naming.NameNotFoundException

I changed both to "hibernate/SessionFactory" only, same error. The mroe I stare the less I see, I guess.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Did you try



with



?

Are there any Hibernate properties associated with using the name attribute of the session-factory element in the configuration file?

Are you verifying that the JNDI reference is created when Hibernate is configured? If a reference isn't configured, then your lookup will never find it.

Configuring a connection pool has nothing to do with configuration of Hibernate Session Factory in app server's JNDI namespace.
[ September 15, 2008: Message edited by: James Clark ]
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Hi,

I just tried your proposal, still getting

java.lang.ExceptionInInitializerError
at model.HibernateUtil.<clinit>(HibernateUtil.java:17)
at model.dao.NameHome.getSessionFactory(NameHome.java:32)
at model.dao.NameHome.<init>(NameHome.java:28)
at controller.NameService.<init>(NameService.java:11)
at controller.action.PersistAction.<init>(PersistAction.java:16)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.struts.chain.commands.util.ClassUtils.getApplicationInstance(ClassUtils.java:71)
at org.apache.struts.chain.commands.servlet.CreateAction.createAction(CreateAction.java:98)
at org.apache.struts.chain.commands.servlet.CreateAction.getAction(CreateAction.java:68)
at org.apache.struts.chain.commands.AbstractCreateAction.execute(AbstractCreateAction.java:90)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:272)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: javax.naming.NameNotFoundException: No object bound to name java:comp/env/hibernate/SessionFactory
at com.sun.enterprise.naming.NamingManagerImpl.lookup(NamingManagerImpl.java:834)
at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:173)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:396)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at model.HibernateUtil.<clinit>(HibernateUtil.java:12)
... 49 more

The complete hibernate.cfg.xml is shown above, there's no additional config file (xml or properties) in classpath or anywhere else. Do I need to configure the SessionFactory's name anywhere else? Besides that, I'm afraid I didn't get what you mean by verifying the JNDI reference is created. Can you elaborate on this, please?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187


With the class above, a Configuration object is not created and there is no call to Hibernate to configure or build a SessionFactory. Basically, it is looking for something that does not exist.

In regards to using app server's JNDI namespace to get a reference to a Hibernate SessionFactory, documentation states that by putting a name attribute on the session-factory element, the framework with register the JNDI name (specified in the value of the attribute) with the application server.

My suggestion was to check (via the app server's administration console) if the JNDI reference is actually created by Hibernate. If Hibernate successfully creates the JNDI entry for the SessionFactory, then you will see it in the admin console.

I'm not sure if Hibernate really does this for you so easily, simply by putting a name attribute in the session-factory element. Maybe this occurs when you configure Hibernate. As mentioned, in your HibernateUtil class, you are not creating Configuration or calling the configure method. This may be why your lookup is failing.

Hope this helps. Good luck!
[ September 16, 2008: Message edited by: James Clark ]
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Are you referring to Hibernate reference page 38? I doubt that Hibernate can do this automatically, it had to be aware of the app server it is deployed to.

Besides, I streamlined my code:
hibernate.cfg.xml


HibernateUtil.java


Still getting:
...
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
|#]

[#|2008-09-17T16:03:07.209+0200|WARNING|sun-appserver9.1|javax.enterprise.system.stream.err|_ThreadID=17;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=5110a506-68ff-475e-bfcb-f6a82a917613;|
javax.naming.NameNotFoundException: No object bound to name java:comp/env/hibernate/SessionFactory
at com.sun.enterprise.naming.NamingManagerImpl.lookup(NamingManagerImpl.java:834)
at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:173)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:396)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at model.HibernateUtil.getSessionFactory(HibernateUtil.java:30)
at controller.NameService.persistName(NameService.java:30)
at controller.action.PersistAction.execute(PersistAction.java:31)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:272)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
[ September 17, 2008: Message edited by: Mike Himstead ]
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Nothing in the code or XML that you have posted is creating the JNDI reference. How are you creating the JNDI reference?

Below you are trying to lookup a JNDI reference, but when do you create the reference.

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Hi James,

thank you for your time bothering with my problem. First, I solved the problem and I can access HSQLDB by the server. At the end it's easier than I thought, but that's always the case once you know how to do it. I'll post my working code/configuration for anyone else who might have problem getting it to work, so he or she can start from here:

My hibernate.cfg.xml


HibernateUtil.java
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Mike,
I don't see how you set your InitialContext. Without doing that you can't make any JNDI lookups at all.
This is the vendor specific part of JNDI. Glassfish has its own way for doing this as JBoss has its own way as any other App Server have their own ways to set their InitialContext.
Take a look at your Sun/Glassfish Application Server Administration Guide for more info on how to set the InitialContext.
[ October 28, 2008: Message edited by: Darya Akbari ]

SCJP, SCJD, SCWCD, SCBCD
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: glassfish vs. Hibernate: JNDI configuration
 
Similar Threads
JNDI error message
Doubt in using JNDI with Hibernate.
Problem with Hibernate3 on WAS 5.1
Hibernate annotations gives me a NoSuchMethodError exception
Hibernate generate select while storing object.