We're updating the issue view to help you get more done. 

NameNotFoundException when creating EntityManager using CDI

Description

I'm using CDI to build my EntityManager.

When building it, Hibernate doesn't manage to retrieve my DataSource using JNDI.

It's not a problem of my persistence.xml or context.xml because when my EntityManager is built sooner (in a @WebListener > .contextInitialized(ServletContextEvent)), the DataSource is correctly found using the same JNDI address.

I debugged the application and saw that new javax.naming.InitialContext().lookup("java:/comp/env/jdbc/myproject") was correctly returning the DataSource until Thread.currentThread().setContextClassLoader( aggregatedClassLoader ) in :

org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public <T> T withTccl(Work<T> work) { final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); boolean set = false; try { Thread.currentThread().setContextClassLoader( aggregatedClassLoader ); set = true; } catch (Exception ignore) { } try { return work.perform(); } finally { if ( set ) { Thread.currentThread().setContextClassLoader( tccl ); } } }

After the execution of that line, new javax.naming.InitialContext().lookup("java:/comp/env/jdbc/myproject") throws javax.naming.NameNotFoundException: Name java:/comp/env/jdbc/myproject is not bound in this Context.

Here is the complete stack :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 javax.persistence.PersistenceException: Unable to build entity manager factory at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) at com.mycompany.myproject.data.EntityManagerFactoryProducer.createEntityManagerFactory(EntityManagerFactoryProducer.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:90) at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstance(MethodInjectionPoint.java:83) at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:96) at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:151) at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:183) at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96) at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:98) at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:99) at org.jboss.weld.proxies.EntityManagerFactory$1746355237$Proxy$_$$_WeldClientProxy.createEntityManager(Unknown Source) at com.mycompany.myproject.data.EntityManagerFactoryProducer.createEntityManager(EntityManagerFactoryProducer.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:90) at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstance(MethodInjectionPoint.java:83) at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:96) at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:151) at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:183) at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96) at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:98) at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:99) at org.jboss.weld.proxies.EntityManager$1746355238$Proxy$_$$_WeldClientProxy.getTransaction(Unknown Source) at com.mycompany.myframework.transaction.TransactionalInterceptor.interceptTransactional(TransactionalInterceptor.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:74) at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNext(AbstractInterceptionChain.java:116) at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNextInterceptor(AbstractInterceptionChain.java:94) at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:43) at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:36) at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:51) at com.mycompany.myproject.service.ProfileService$Proxy$_$$_WeldSubclass.findAll(Unknown Source) at com.mycompany.myproject.service.ProfileService$Proxy$_$$_WeldClientProxy.findAll(Unknown Source) at com.mycompany.myproject.ws.administration.UserSearchWS.init(UserSearchWS.java:56) at com.mycompany.myproject.ws.administration.UserSearchWS$Proxy$_$$_WeldClientProxy.init(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:288) at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:242) at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:229) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744) Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:/comp/env/jdbc/myproject] at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) ... 79 more Caused by: javax.naming.NameNotFoundException: Name java:/comp/env/jdbc/myproject is not bound in this Context at org.apache.naming.NamingContext.lookup(NamingContext.java:820) at org.apache.naming.NamingContext.lookup(NamingContext.java:154) at javax.naming.InitialContext.lookup(InitialContext.java:415) at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114) ... 95 more

Here are the classes :

com.mycompany.myproject.ws.administration.UserSearchWS.java

1 2 3 4 5 6 7 8 9 10 11 12 @Path("/administration/userSearch") @ApplicationScoped public class UserSearchWS { @Inject private ProfileService profileService; @GET @Path("/init") public StreamingOutput init() { final List<Profile> profileList = profileService.findAll(); ...

com.mycompany.myproject.service.ProfileService.java

1 2 3 4 5 6 7 8 9 10 @Transactional @ApplicationScoped public class ProfileService extends JPAEntityService<Profile, Integer> { @Inject private Instance<EntityManager> entityManagerInstance; public List<Profile> findAll() { return entityManagerInstance.get().createQuery("from Profile").getResultList(); } }

com.mycompany.myframework.transaction.TransactionalInterceptor.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Interceptor @Transactional public class TransactionalInterceptor { @Inject Instance<EntityManager> entityManagerInstance; @AroundInvoke public Object interceptTransactional(final InvocationContext invocationContext) throws Exception { EntityManager entityManager = null; EntityTransaction transaction = null; try { entityManager = entityManagerInstance.get(); transaction = entityManager.getTransaction(); } catch (final ContextException e) { ...

com.mycompany.myproject.data.EntityManagerFactoryProducer.java

1 2 3 4 5 6 7 8 9 10 11 12 13 @ApplicationScoped public class EntityManagerFactoryProducer { @Produces @ApplicationScoped public EntityManagerFactory createEntityManagerFactory() { return Persistence.createEntityManagerFactory("com.mycompany.myproject"); } @Produces @RequestScoped public EntityManager createEntityManager(final EntityManagerFactory entityManagerFactory) { return entityManagerFactory.createEntityManager(); } ... // @Disposes code

persistence.xml

1 2 3 4 5 ... <persistence-unit name="com.mycompany.myproject" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <non-jta-data-source>java:/comp/env/jdbc/myproject</non-jta-data-source> ...

And when I use that DataInitializer, I'm getting no problem, the previous code passes because the EntityManagerFactory is correctly initialized :

com.mycompany.myproject.bootstrap.DataInitializer.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @WebListener public class DataInitializer implements ServletContextListener { @Override public void contextInitialized(final ServletContextEvent sce) { try { final Context ctx = new InitialContext(); final BeanManager beanManager = (BeanManager) ctx.lookup("java:comp/env/BeanManager"); final Bean<EntityManagerFactory> bean = (Bean<EntityManagerFactory>) beanManager.resolve(beanManager.getBeans(EntityManagerFactory.class)); final EntityManagerFactory entityManagerFactory = beanManager.getContext(bean.getScope()).get(bean, beanManager.createCreationalContext(bean)); final EntityManager entityManager = entityManagerFactory.createEntityManager(); } catch (final NamingException e) { throw new IllegalStateException("Problem while initializing", e); } } ...

In that case, here is the stack when correctly creating my EntityManagerFactory & EntityManager :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) at com.mycompany.myproject.data.EntityManagerFactoryProducer.createEntityManagerFactory(EntityManagerFactoryProducer.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:90) at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstance(MethodInjectionPoint.java:83) at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:96) at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:151) at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:183) at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96) at com.mycompany.myproject.bootstrap.DataInitializer.createSampleDatas(DataInitializer.java:29) at com.mycompany.myproject.bootstrap.DataInitializer.contextInitialized(DataInitializer.java:66) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)

I've tested with latest Tomcat (7.0.53) and still have the same behaviors.

Environment

Tomcat 7.0.47, Ubuntu 12.04 LTS

Status

Assignee

Unassigned

Reporter

Anthony Ogier

Components

Affects versions

4.3.4
4.3.5

Priority

Blocker