7 Replies Latest reply on Feb 14, 2006 6:33 PM by Bill Burke

    How to get EntityManagers dynamically in final EJB3

    Holger Casties Newbie

      Hi.

      Will it be further possible to get EntityManagers via JNDI lookup?

      Currently it is possible to get an DB-related EntityManager as follows:

      @Resource SessionContext ctx;
      ...
      EntityManager em = (EntityManager)ctx.lookup("java:/EntityManagers/" + unitName);
      

      This way, to get EntityManagers, is necessary for applications supporting multiple databases (dynamically created DBs).


      Holger

        • 1. Re: How to get EntityManagers dynamically in final EJB3
          Gavin King Master

          Yes, it will always be possible to do that. Except that in the next rev of EJB3, you will need to use a property setting in persistence.xml to determine the JNDI name. It is no longer done by default.

          • 2. Re: How to get EntityManagers dynamically in final EJB3
            Holger Casties Newbie

            Hi Gavin.

            Thanx a lot for your answer.

            I've just tried out that, using JBoss4.0.4RC1, but I don't get the EntityManager.

            I defined the following persistence.xml file:

            <?xml version="1.0" encoding="UTF-8"?>
            
            <entity-manager>
             <!-- Entity Manager definition -->
             <persistence-unit name="LibraryDB1">
             <!-- EJB3 classes -->
             <jar-file>../library.jar</jar-file>
             <!-- Data source name -->
             <jta-data-source>java:/PostgresDS#LibraryDB1</jta-data-source>
            
             <properties>
             <!-- Define JNDI entry to get the EntityManager via lookup -->
             <property name="jboss.entity.manager.jndi.name" value="java:/EntityManagers"/>
            
             <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
             <property name="hibernate.hbm2ddl.auto" value="update"/>
             </properties>
             </persistence-unit>
            </entity-manager>
            


            What is missing? What's wrong?

            Holger

            • 3. Re: How to get EntityManagers dynamically in final EJB3
              Gavin King Master

              How about this:

              <property name="jboss.entity.manager.jndi.name" value="java:/EntityManagers/LibraryDB1"/>




              • 4. Re: How to get EntityManagers dynamically in final EJB3
                Holger Casties Newbie

                Hi Gavin.

                I'm sorry, but that does not solve the problem.
                It's not possible to get the EntityManager via JNDI lookup!

                Here is the logging of JBoss-4.0.4RC1 when deploying my JAR files:

                08:14:57,178 INFO [JkMain] Jk running ID=0 time=0/141 config=null
                08:14:57,178 INFO [Server] JBoss (MX MicroKernel) [4.0.4RC1 (build: CVSTag=JBoss_4_0_4_RC1 date=200601230248)] Started in 29s:706ms
                08:15:32,347 INFO [Ejb3AnnotationHandler] found EJB3: ejbName=LibraryBean, class=control.LibraryBean, type=STATELESS
                08:15:32,456 INFO [Ejb3Deployment] EJB3 deployment time took: 265
                08:15:32,550 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:service=EJB3,jar=library.jar,name=LibraryBean with dependencies:
                08:15:33,206 INFO [EJB3Deployer] Deployed: file:/D:/EnterpriseApplications/JBoss/4.0.4RC1/server/default/deploy/library.jar
                08:15:33,253 FATAL [PersistenceXmlLoader] LibraryDB1 JTA
                08:15:33,268 INFO [Ejb3Deployment] EJB3 deployment time took: 46
                08:15:33,268 INFO [JmxKernelAbstraction] installing MBean: persistence.units:unitName=LibraryDB1 with dependencies:
                08:15:33,268 INFO [JmxKernelAbstraction] jboss.jca:name=PostgresDS#LibraryDB1,service=DataSourceBinding
                08:15:33,409 INFO [Environment] Hibernate 3.1
                08:15:33,409 INFO [Environment] hibernate.properties not found
                08:15:33,425 INFO [Environment] using CGLIB reflection optimizer
                08:15:33,425 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
                08:15:33,674 INFO [Ejb3Configuration] found EJB3 Entity bean: business.Article
                08:15:33,690 INFO [Ejb3Configuration] found EJB3 Entity bean: business.Author
                08:15:34,143 INFO [AnnotationBinder] Binding entity from annotated class: business.Article
                08:15:34,284 INFO [EntityBinder] Bind entity business.Article on table ARTICLES
                08:15:34,471 INFO [AnnotationBinder] Binding entity from annotated class: business.Author
                08:15:34,471 INFO [EntityBinder] Bind entity business.Author on table AUTHORS
                08:15:34,721 INFO [CollectionBinder] Mapping collection: business.Author.articles -> ARTICLES
                08:15:34,815 INFO [ConnectionProviderFactory] Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
                08:15:34,815 INFO [InjectedDataSourceConnectionProvider] Using provided datasource
                08:15:35,549 INFO [SettingsFactory] RDBMS: PostgreSQL, version: 8.1.0
                08:15:35,564 INFO [SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1devel JDBC3 with SSL (build 403)
                08:15:35,611 INFO [Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
                08:15:35,627 INFO [TransactionFactoryFactory] Using default transaction strategy (direct JDBC transactions)
                08:15:35,642 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
                08:15:35,642 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                08:15:35,642 INFO [SettingsFactory] Automatic flush during beforeCompletion(): enabled
                08:15:35,642 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
                08:15:35,642 INFO [SettingsFactory] JDBC batch size: 15
                08:15:35,642 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
                08:15:35,642 INFO [SettingsFactory] Scrollable result sets: enabled
                08:15:35,642 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
                08:15:35,642 INFO [SettingsFactory] Connection release mode: after_statement
                08:15:35,658 INFO [SettingsFactory] Default batch fetch size: 1
                08:15:35,658 INFO [SettingsFactory] Generate SQL with comments: disabled
                08:15:35,658 INFO [SettingsFactory] Order SQL updates by primary key: disabled
                08:15:35,658 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
                08:15:35,673 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
                08:15:35,673 INFO [SettingsFactory] Query language substitutions: {}
                08:15:35,673 INFO [SettingsFactory] Second-level cache: enabled
                08:15:35,673 INFO [SettingsFactory] Query cache: disabled
                08:15:35,673 INFO [SettingsFactory] Cache provider: org.hibernate.cache.HashtableCacheProvider
                08:15:35,673 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
                08:15:35,673 INFO [SettingsFactory] Structured second-level cache entries: disabled
                08:15:35,689 INFO [SettingsFactory] Statistics: disabled
                08:15:35,689 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
                08:15:35,689 INFO [SettingsFactory] Default entity-mode: pojo
                08:15:35,767 INFO [SessionFactoryImpl] building session factory
                08:15:36,657 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
                08:15:36,673 INFO [SchemaUpdate] Running hbm2ddl schema update
                08:15:36,673 INFO [SchemaUpdate] fetching database metadata
                08:15:37,298 INFO [SchemaUpdate] updating schema
                08:15:37,797 INFO [TableMetadata] table found: public.articles
                08:15:37,797 INFO [TableMetadata] columns: [title, articleid, authorid, body]
                08:15:37,797 INFO [TableMetadata] foreign keys: [fke566c23dd993b37f]
                08:15:37,797 INFO [TableMetadata] indexes: [articles_pkey]
                08:15:37,844 INFO [TableMetadata] table found: public.authors
                08:15:37,844 INFO [TableMetadata] columns: [userid, authorid, name]
                08:15:37,844 INFO [TableMetadata] foreign keys: []
                08:15:37,844 INFO [TableMetadata] indexes: [authors_pkey]
                08:15:37,844 INFO [SchemaUpdate] schema update complete
                08:15:37,844 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                08:15:37,860 INFO [EJB3Deployer] Deployed: file:/D:/EnterpriseApplications/JBoss/4.0.4RC1/server/default/deploy/postgres.LibraryDB1.jar
                08:15:37,954 INFO [TomcatDeployer] deploy, ctxPath=/library, warUrl=.../tmp/deploy/tmp13643library-exp.war/


                Holger

                • 5. Re: How to get EntityManagers dynamically in final EJB3
                  John Blackmore Newbie

                  It doesn't look like your persistence.xml was loaded properly, and your entity manager hasn't been deployed.

                  08:15:33,253 FATAL [PersistenceXmlLoader] LibraryDB1 JTA


                  Maybe an XML formatting error?

                  • 6. Re: How to get EntityManagers dynamically in final EJB3
                    Boyd Hutton Newbie

                    I'm having the same issue:

                    <persistence>
                     <persistence-unit name="em">
                     <jta-data-source>java:/MSSQLAppDSInterop</jta-data-source>
                     <provider>org.hibernate.ejb.HibernatePersistence</provider>
                     <class>com.xpel.hibernate.UserAccount</class>
                     <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
                     <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                     </properties>
                     </persistence-unit>
                    </persistence>


                    If it's an XML formatting error, I have not been able to find it.

                    15:37:29,796 FATAL [PersistenceXmlLoader] em JTA


                    • 7. Re: How to get EntityManagers dynamically in final EJB3
                      Bill Burke Master

                      ignore the fatal, it i s just a log message
                      to get em's now in jndi you must bind them

                      jboss.entity.manager.jndi.name
                      jboss.entity.manager.factory.jndi.name

                      see release notes, migration guide, tutorials, docbook