I have a database with multiple schemas, so I have a persistence.xml file with short values <persistence-unit /> (named 01, 02, ...).
Problem: I want to create a dynamic EntityManager function for some custom criteria.
I tested 2 cases.
The first case: basically, I tested this code (inside a stateless EAN server):
String criteria = "01"; EntityManagerFactory emf = Persistence.createEntityManagerFactory(criteria); EntityManager em = emf.createEntityManager(); Joueur joueur = new Joueur();
but I got an exception:
Raised: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal exception: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée les commandes sont ignorées jusqu'à la fin du bloc de la transaction
I thought (with some Stackoverflow posts) that my EntityManager was not associated with my transaction context due to the fact that it was not nested by the container.
Second case: thus, I used the injection:
@PersistenceContext(unitName="00") private EntityManager em00; @PersistenceContext(unitName="01") private EntityManager em01;
The code in my function is:
String criteria = "01"; EntityManager em = getEm(criteria); ...
and getEm () method:
private EntityManager getEm(String criteria){ if (criteria == "00") return em00; else if (criteria == "01") return em01; return null; }
No problem, this works, but I have to introduce so many EntityManager that I have a persistence unit.
- What will be the cost if I have 50 schemes ...?
- Is there a way to manage entity managers dynamically? (Only 1 EntityManger)
- If I need to create 1 EntityManager for each scheme, even if I do not use it, how can I improve my code to consume the least amount of resources?
Thanks for the tips and feedback.
EDIT:
My configuration files:
Persistence.xml:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="00" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>jdbc/sim/00</jta-data-source> <mapping-file>orm_00_beta.xml</mapping-file> <class>com.sim.entities.Joueur</class> <properties> <property name="eclipselink.ddl-generation" value="create-tables" /> </properties> </persistence-unit> <persistence-unit name="01" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>jdbc/sim/01</jta-data-source> <mapping-file>orm_01_beta2.xml</mapping-file> <class>com.sim.entities.Joueur</class> <properties> <property name="eclipselink.ddl-generation" value="create-tables" /> </properties> </persistence-unit> </persistence>
orm_00_beta.xml:
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd" version="2.0"> <persistence-unit-metadata> <persistence-unit-defaults> <schema>beta</schema> </persistence-unit-defaults> </persistence-unit-metadata>
orm_01_beta2.xml:
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd" version="2.0"> <persistence-unit-metadata> <persistence-unit-defaults> <schema>beta2</schema> </persistence-unit-defaults> </persistence-unit-metadata>