3 Replies Latest reply on Jan 31, 2006 8:49 AM by Bill Burke

    Unable to get EntityManagerFactory via JNDI lookup

    Holger Casties Newbie

      Hi.

      I'm using JBoss-4.0.4RC1 with EJB-3.0_RC4-PFD.

      It is not possible to get an EntityManagerFactory via JNDI lookup!

      Here is the code inside my session bean:

      // Create entity manager via entity manager factory
      EntityManagerFactory emf = (EntityManagerFactory)getSessionContext().lookup("java:/EntityManagerFactory/" + unitName);
      EntityManager entityManager = emf.createEntityManager();

      (I prefer this way, to get an EntityManager, to have the ability to define an extended EntityManager for SFSBs.)

      That's the corresponding persistence.xml:
      <?xml version="1.0" encoding="UTF-8"?>
      
      <entity-manager>
       <!-- Entity Manager definition -->
       <persistence-unit name="LibraryDB1" transaction-type="JTA">
       <!-- Entity classes -->
       <jar-file>../library.jar</jar-file>
       <!-- Data source name -->
       <jta-data-source>java:/LibraryDB1</jta-data-source>
      
       <properties>
       <!-- JNDI entry to get the Entity Manager Factory via lookup -->
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactory" />
      
       <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
       <property name="hibernate.hbm2ddl.auto" value="update" />
       </properties>
       </persistence-unit>
      </entity-manager>

      That's the logging of JBoss when deploying my JAR files (consider the red-colored line):
      10:45:22,564 INFO [Ejb3AnnotationHandler] found EJB3: ejbName=LibraryBean, class=control.LibraryBean, type=STATELESS
      10:45:22,564 INFO [Ejb3Deployment] EJB3 deployment time took: 32
      10:45:22,579 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:service=EJB3,jar=library.jar,name=LibraryBean with dependencies:
      10:45:22,735 INFO [EJB3Deployer] Deployed: file:/D:/EnterpriseApplications/JBoss/4.0.4RC1/server/default/deploy/library.jar
      10:45:22,782 INFO [TomcatDeployer] deploy, ctxPath=/library, warUrl=.../tmp/deploy/tmp52626library-exp.war/
      10:45:38,157 FATAL [PersistenceXmlLoader] LibraryDB1 JTA
      10:45:38,157 INFO [Ejb3Deployment] EJB3 deployment time took: 0
      10:45:38,173 INFO [JmxKernelAbstraction] installing MBean: persistence.units:unitName=LibraryDB1 with dependencies:
      10:45:38,173 INFO [JmxKernelAbstraction] jboss.jca:name=LibraryDB1,service=DataSourceBinding
      10:45:38,188 INFO [Ejb3Configuration] found EJB3 Entity bean: business.Article
      10:45:38,188 INFO [Ejb3Configuration] found EJB3 Entity bean: business.Author
      10:45:38,204 INFO [AnnotationBinder] Binding entity from annotated class: business.Article
      10:45:38,204 INFO [EntityBinder] Bind entity business.Article on table ARTICLES
      10:45:38,391 INFO [AnnotationBinder] Binding entity from annotated class: business.Author
      10:45:38,391 INFO [EntityBinder] Bind entity business.Author on table AUTHORS
      10:45:38,391 INFO [CollectionBinder] Mapping collection: business.Author.articles -> ARTICLES
      10:45:38,407 INFO [ConnectionProviderFactory] Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
      10:45:38,423 INFO [InjectedDataSourceConnectionProvider] Using provided datasource
      10:45:38,423 INFO [SettingsFactory] RDBMS: PostgreSQL, version: 8.1.0
      10:45:38,423 INFO [SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1devel JDBC3 with SSL (build 403)
      10:45:38,423 INFO [Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
      10:45:38,423 INFO [TransactionFactoryFactory] Using default transaction strategy (direct JDBC transactions)
      10:45:38,423 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
      10:45:38,423 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
      10:45:38,423 INFO [SettingsFactory] Automatic flush during beforeCompletion(): enabled
      10:45:38,423 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
      10:45:38,423 INFO [SettingsFactory] JDBC batch size: 15
      10:45:38,423 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
      10:45:38,423 INFO [SettingsFactory] Scrollable result sets: enabled
      10:45:38,423 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
      10:45:38,423 INFO [SettingsFactory] Connection release mode: after_statement
      10:45:38,423 INFO [SettingsFactory] Default batch fetch size: 1
      10:45:38,423 INFO [SettingsFactory] Generate SQL with comments: disabled
      10:45:38,423 INFO [SettingsFactory] Order SQL updates by primary key: disabled
      10:45:38,423 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
      10:45:38,423 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
      10:45:38,423 INFO [SettingsFactory] Query language substitutions: {}
      10:45:38,423 INFO [SettingsFactory] Second-level cache: enabled
      10:45:38,423 INFO [SettingsFactory] Query cache: disabled
      10:45:38,423 INFO [SettingsFactory] Cache provider: org.hibernate.cache.HashtableCacheProvider
      10:45:38,423 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
      10:45:38,423 INFO [SettingsFactory] Structured second-level cache entries: disabled
      10:45:38,423 INFO [SettingsFactory] Statistics: disabled
      10:45:38,423 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
      10:45:38,423 INFO [SettingsFactory] Default entity-mode: pojo
      10:45:38,423 INFO [SessionFactoryImpl] building session factory
      10:45:38,501 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
      10:45:38,501 INFO [SchemaUpdate] Running hbm2ddl schema update
      10:45:38,501 INFO [SchemaUpdate] fetching database metadata
      10:45:38,501 INFO [SchemaUpdate] updating schema
      10:45:38,563 INFO [TableMetadata] table found: public.articles
      10:45:38,563 INFO [TableMetadata] columns: [title, articleid, authorid, body]
      10:45:38,563 INFO [TableMetadata] foreign keys: [fke566c23dd993b37f]
      10:45:38,563 INFO [TableMetadata] indexes: [articles_pkey]
      10:45:38,595 INFO [TableMetadata] table found: public.authors
      10:45:38,595 INFO [TableMetadata] columns: [userid, authorid, name]
      10:45:38,595 INFO [TableMetadata] foreign keys: []
      10:45:38,595 INFO [TableMetadata] indexes: [authors_pkey]
      10:45:38,595 INFO [SchemaUpdate] schema update complete
      10:45:38,595 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
      10:45:38,595 INFO [EJB3Deployer] Deployed: file:/D:/EnterpriseApplications/JBoss/4.0.4RC1/server/default/deploy/LibraryDB1.jar

      The following error occurs when executing the session bean:
      INFO [main] (Client.java:96) - Entering application:
      DEBUG [main] (SecurityAssociation.java:143) - Using ThreadLocal: false
      javax.ejb.EJBException: java.lang.NullPointerException
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:219)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:580)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:450)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:290)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:330)
       at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:202)
       Caused by: java.lang.NullPointerException
       at control.LibraryBean.getEntityManager(LibraryBean.java:37)
       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 org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:219)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:580)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:450)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:290)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:330)
       at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:202)
       at org.jboss.remoting.RemoteClientInvoker.invoke(RemoteClientInvoker.java:163)
       at org.jboss.remoting.Client.invoke(Client.java:258)
       at org.jboss.remoting.Client.invoke(Client.java:221)
       at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:41)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:46)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:40)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:65)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:102)
       at $Proxy0.getEntityManager(Unknown Source)
       at client.Client.createAuthors(Client.java:56)
       at client.Client.main(Client.java:101)DEBUG [main] (Client.java:247) - invoke called, but our invoker is disconnected, discarding and fetching another fresh invoker for: InvokerLocator [socket://129.103.124.235:3873/]
      DEBUG [main] (RemoteClientInvoker.java:248) - connect called for: org.jboss.remoting.transport.socket.SocketClientInvoker@192b996

      BTW: It is possible to get an EntityManager via injection:
      @PersistenceContext(unitName="LibraryDB1") private EntityManager entityManager;

      But that's not suitable for my application which must be able to dynamically create databases!

      Any ideas?

      Holger