I currently use

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">

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">

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

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

     try {
         entityManager = 
     } catch (NamingException e) {
         // TODO Auto-generated catch block

     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

Reply via email to