views:

1669

answers:

1

I have a perfectly working application client deployed to a glassfish v2 server inside an ear with some EJBs, Entities, etc. I'm using eclipselink.

Currently I have in my persistence.xml:

<persistence-unit name="mysource">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/mysource</jta-data-source>
    <class>entities.one</class>
    <class>entities.two</class>
    ...
    <properties>
      <property name="eclipselink.target-server" value="SunAS9"/>
      <property name="eclipselink.logging.level" value="FINE"/>
    </properties>
</persistence-unit>

And this works fine when I inject the EntityManager into the EJB:

@PersistenceContext(unitName="mysource")
private EntityManager em;

Now I have a requirement to dynamically switch persistence units/databases. I figure I can get an EntityManager programatically:

em = Persistence.createEntityManagerFactory("mysource").createEntityManager();

but I get the following error:

Unable to acquire a connection from driver [null], user [null] and URL [null]

Even "overriding" javax.persistence.jtaDataSource" to "jdbc/mysource" in a Map and calling createEntityManagerFactory("mysource", map) doesn't make a difference.

What am I missing?

A: 

You are trying to circumvent the container with creating an entity manager programmatically and this means you'll most probably create a non-JTA data source (as it's outside the container, the transaction type should be RESOURCE_LOCAL), thus your original config is useless.

Try injecting an entity manager with a different unitName property or create a RESOURCE_LOCAL transaction type persistence unit.

Tamás Mezei
What I'm *trying* to do is exactly what the annotation does, but in code so I can set the unitName programatically. I basically just want to ask the container for an entity manager for a given PU by name. If there's another way to do this (ie without createEntityManagerFactory), that's fine.
Draemon