2 Replies Latest reply on Jul 21, 2009 10:43 AM by Zo Ge

    How to get EntityManager in my app?

    Gregory Nikle Apprentice

      Hi,
      My app connects to many, over four dabases at now, but this will grow to over 20dbs.
      I need to get EM from persistence.xml using JPA and RESOURCE LOCAL, on tomcat 6.
      So I think injectin over 20 Entity Managers for each class (over 50 classes in my project) is wrong idea.I think to declare static EntityManagersFactories for each database and create entity manager for each transaction, but I've no idea how to do this?


      Can anybody help me, show some source or idea how to do this?
      Here is my persistence.xml


      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                   version="1.0">
      
           <persistence-unit name="sguniAdmDatabase" transaction-type="RESOURCE_LOCAL">
               <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:comp/env/jdbc/sguni_adm</jta-data-source>
                     <class>pl.administration.model.Adv_Links</class>          
                     <class>pl.administration.model.Adv_Emails</class>
                     <class>pl.administration.model.News</class>
                     <class>pl.administration.model.Pacc_Passwords</class>
                     <class>pl.administration.model.Tutorial</class>
                     <class>pl.administration.model.Filter</class>
                     <exclude-unlisted-classes>true</exclude-unlisted-classes>               
                     <properties>
                          <property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                         <!-- These are the default for JBoss EJB3, but not for HEM: -->
                         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
                         <property name="hibernate.cache.use_second_level_cache" value="false" /> 
                       <property name="hibernate.cache.use_query_cache" value="false"/>
                       <property name="hibernate.cache.region_prefix" value="hibernate.test"/>
                         <property name="hibernate.cache.use_minimal_puts" value="true"/>
                          <property name="hibernate.cache.use_structured_entries" value="true"/>                    
                       <property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
                     </properties>
           </persistence-unit>


        • 1. Re: How to get EntityManager in my app?
          Gregory Nikle Apprentice

          How about this?



          public class EMController {
               public EMController() {}
               
              private static EntityManagerFactory sguniAdmEMF;
              private static EntityManagerFactory sguniOneEMF;
              private static EntityManagerFactory sguniTwoEMF;
              
              public static EntityManagerFactory getEntityManagerFactory(int emf) {
                  switch (emf) {
                       case (0) :
                            return sguniAdmEMF;
                       case (1) :
                            return sguniOneEMF;
                       case (2) :
                            return sguniTwoEMF;
                  }
                  return null;
              }
              
              public static void init() {
                   sguniAdmEMF = Persistence.createEntityManagerFactory("sguniAdmDatabase");
                   sguniOneEMF = Persistence.createEntityManagerFactory("sguniOneDatabase");
                   sguniTwoEMF = Persistence.createEntityManagerFactory("sguniTwoDatabase");
              }
              
              public void destroy() {
                   sguniAdmEMF.close();
                   sguniOneEMF.close();
                   sguniTwoEMF.close();
              }
          
              public static EntityManager getEM(int em) throws RuntimeException {
                  return getEntityManagerFactory(em).createEntityManager();
              }
          }