I currently use

@PersistenceUnit(unitName="foo")
private EntityManagerFactory emf;

with resin-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doctype>
<web-app xmlns="http://caucho.com/ns/resin";>
     ...
     <database jndi-name="foo">
         <driver type="com.mysql.jdbc.Driver">
<url>jdbc:mysql://127.0.0.1:3306/foo</url>
             <user>foo_user</user>
             <password>foo_password</password>
         </driver>
     </database>
</web-app>

and persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doctype>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"; version="1.0">
     <persistence-unit name="foo">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>foo</jta-data-source>
         <class>...</class>
         <exclude-unlisted-classes/>
       </persistence-unit>
</persistence>

With just em = emf.createEntityManager(); in code to get the em 
instance. This works great. The problem now is foo is to be replicated 
with different names which are resolved at runtime. How can I change 
this so the unitName is set dynamically please? I tried this, which 
always returns null, but am not even sure if JNDI is the best way to 
solve this problem:

public EntityManager initializeEM(String pUnitName) {
     Context iCtx = null;

     try {
         iCtx = new InitialContext();
     } catch (NamingException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }

     String lookUpString = "java:/comp/env/persistence/"+pUnitName;
     javax.persistence.EntityManager entityManager = null;

     try {
         entityManager = 
(javax.persistence.EntityManager)iCtx.lookup(lookUpString);
     } catch (NamingException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }

     return entityManager;
}

I didn't change any of the above xml files when I tried this code, in 
case that's significant. Anyway, thanks in advance...

-- Carl Whalley



_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest

Reply via email to