Hey there. While it is permitted, using Persistence.createEntityManagerFactory() in a JEE environment is a somewhat treacherous road to walk, as the application developer bears the full responsibility of the EMF's lifecycle, including closing the EMF when it is no longer needed. Applications that create lots of EMFs and fail to close() them when they are no longer needed (garbage collecting does not close EMFs) will often encounter out-of-memory situations.
If this is a J2EE application, I'd recommend simply adding an injection entry in ejb-jar.xml: <persistence-unit-ref> <persistence-unit-ref-name>jpa/BusinessUnit1</persistence-unit-ref-name> <persistence-unit-name>BusinessUnit1</persistence-unit-name> </persistence-unit-ref> The above is functionally equivalent to adding a @PersistenceUnit annotation to your EJB/Servlet class definition. In your application's java code, you can perform a JNDI lookup on the ref name (java:comp/env/jpa/BusinessUnit1 using the example above) and cache the EMF in a local variable in your EJB (EMFs are thread safe, so this is okay to do with SL EJBs and servlets). This way, your business logic can determine which EMF it needs to use for the persistence operation by whichever business unit identifier your application uses, and you still gain the benefit of the application server managing the life cycle of the EMF. Pseudocode Example (does not check for errors): @EJB public class MyEJB { private EntityManagerFactory getEMF(String businessUnitName) throws Throwable { InitialContext ic = new InitialContext(); return (EntityManagerFactory) ic.lookup("java:comp/env/jpa/" + businessUnitName); } public void doSomeJPAWork(String businessUnitName, MyEntity persistMe) { EntityManagerFactory emf = getEMF(businessUnitName); em = emf.createEntityManager(); tx.begin(); em.joinTransaction(); // Necessary for JTA tran, ignore if using a resource-local PU em.persist(persistMe); tx.commit(); em.close(); // Because this is an application managed emf, you don't have to emf.close()! } } On Mon, Sep 26, 2011 at 2:00 PM, chintan4181 <chintan4...@gmail.com> wrote: > hi, > > We have requirement in which each business department have it's own > persistence unit. At present we have 2 business unit ( and in future it may > arise). Since we have to support dynamic persistent units defined based on > business department, we have replaced @persistenceContext(unitname="x") > with > > EntityManagerFactory factory = > Persistence.createEntityManagerFactory(unitName); and we are getting unit > name from xml file. > > this is the case of Application managed entity manager. After replacing > container managed entity manager with application managed we are facing > many > issues. > > Here is business scenario, we want to handle with application managed > entity > manager. > > 1) EJB A initiate em.persist( functionDO) > 2) It extract Obj1 from functionDO and call our em.persist(obj1) > 3) it extract obj2 from functionDO and call our em.persist(obj2) > > Sample reference code would be helpful. > > Thanks > unmarshall > > > -- > View this message in context: > http://openjpa.208410.n2.nabble.com/How-to-handle-Application-managed-entity-manager-tp6833056p6833056.html > Sent from the OpenJPA Users mailing list archive at Nabble.com. >