users@glassfish.java.net

entityManager does not persist if obtained through EntityManagerFactory

From: <glassfish_at_javadesktop.org>
Date: Tue, 21 Sep 2010 09:01:25 PDT

Hi,

we encountered problem with dynamic instanciation of EntityManager.
The aim is to dynamically choose the appropriate persistence unit at runtime, according to parameters given to the EJB method.

Due to this constraint, we cannot use injection for our EntityManager.

So we have defined several persistence units in persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        version="1.0">
        <persistence-unit name="pu1_datasource" transaction-type="JTA">
                <jta-data-source>jdbc/pu1</jta-data-source>
                <class>com.mycompany.Foo</class>
                <exclude-unlisted-classes>false</exclude-unlisted-classes>
                <properties>
                        <property name="eclipselink.target-database" value="PostgreSQL" />
                        <property name="eclipselink.ddl-generation" value="create-tables" />
                </properties>
        </persistence-unit>
        <persistence-unit name="pu2_datasource" transaction-type="JTA">
                <jta-data-source>jdbc/pu2</jta-data-source>
                <class>com.mycompany.Foo</class>
                <exclude-unlisted-classes>false</exclude-unlisted-classes>
                <properties>
                        <property name="eclipselink.target-database" value="PostgreSQL" />
                        <property name="eclipselink.ddl-generation" value="create-tables" />
                </properties>
        </persistence-unit>
</persistence>

In the Stateless EJB, we tried to obtained the persistence unit through the Persistence.createEntityManagerFactory
@Stateless
@EJB(name="java:global/FooManager", beanInterface=FooManagerRemote.class)
public class FooManager implements FooManagerRemote {

        public String createFoo(int clientReference, int id, String fooName) {
                Foo foo = new Foo(id, fooName);
                getEntityManager(clientReference).persist(foo);
                return fooName + " created";
        }
        
        public EntityManager getEntityManager(int clientReference) {
                EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pu" + clientReference + "_datasource");
                return entityManagerFactory.createEntityManager();
        }
        
}

We have a small remote client which calls the createFoo method. All seems to be done at the client side (got a result), but Foo data are not persisted in database (and table is not created).

To be sure that database and datasource are ok, we have deploy another version of application that use the injection instead of dynamically retrieve entityManager (@PersistenceContext use). In that case, table is created and data are persisted.

Anyone of you have already encountered such problem?
[Message sent by forum member 'i20']

http://forums.java.net/jive/thread.jspa?messageID=483264