Announcement Announcement Module
Collapse
No announcement yet.
Lookup EntityManagerFactory via JNDI in Glassfish 3.1/Spring 3.0 environment Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lookup EntityManagerFactory via JNDI in Glassfish 3.1/Spring 3.0 environment

    Hello,

    i want to lookup an EntityManagerFactory using JNDI in Spring. The factory is provided via Glassfish and the definition of its reference JNDI name in web.xml. The JNDI lookup in an session bean works and i can use the EMF without problems. But Spring keeps telling me that the EMF can not be found:

    web.xml:
    Code:
    	<persistence-unit-ref>
    		<persistence-unit-ref-name>persistence/erpEMF</persistence-unit-ref-name>
    		<persistence-unit-name>erpPU</persistence-unit-name>
    	</persistence-unit-ref>
    Spring cfg:
    Code:
    <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/erpEMF" />
    <!--jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/erpEMF" /-->
    Exception:
    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Invocation of init method failed; nested exception is javax.naming.NamingException: Lookup failed for 'java:comp/env/persistence/erpEMF' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    	... 70 more
    Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/persistence/erpEMF' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
    	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    	at javax.naming.InitialContext.lookup(InitialContext.java:392)
    	at javax.naming.InitialContext.lookup(InitialContext.java:392)
    	at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    	at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    	at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    	at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    	at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    	at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    	... 77 more
    Caused by: javax.naming.NamingException: Invocation exception: Got null ComponentInvocation 
    	at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.getComponentId(GlassfishNamingManagerImpl.java:873)
    	at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:742)
    	at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169)
    	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
    	... 90 more
    In my bean i lookup the EMF like this and i get a usable reference to work with:
    Code:
    		EntityManagerFactory emf = null;
    		try {
    			InitialContext ic = new InitialContext();
    			emf = (EntityManagerFactory) ic.lookup("java:comp/env/persistence/erpEMF");
    		} catch (NamingException e) {
    			...
    		}
    So, why it works in beans but not in the Spring environment? In the same application module?

    Thanks for any hint.

    Chris
Working...
X