Note that there are some explanatory texts on larger screens.

plurals
  1. POAbility to switch Persistence Unit dynamically within the application (JPA)
    primarykey
    data
    text
    <p>My application data access layer is built using Spring and EclipseLink and I am currently trying to implement the following feature - Ability to switch the current/active persistence unit dynamically for a user. I tried various options and finally ended up doing the following. </p> <p>In the persistence.xml, declare multiple PUs. Create a class with as many EntityManagerFactory attributes as there are PUs defined. This will act as a factory and return the appropriate EntityManager based on my logic</p> <pre><code>public class MyEntityManagerFactory { @PersistenceUnit(unitName="PU_1") private EntityManagerFactory emf1; @PersistenceUnit(unitName="PU_2") private EntityManagerFactory emf2; public EntityManager getEntityManager(int releaseId) { // Logic goes here to return the appropriate entityManeger } } </code></pre> <p>My spring-beans xml looks like this..</p> <pre><code>&lt;!-- First persistence unit --&gt; &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emFactory1"&gt; &lt;property name="persistenceUnitName" value="PU_1" /&gt; &lt;/bean&gt; &lt;bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager1"&gt; &lt;property name="entityManagerFactory" ref="emFactory1"/&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager1"/&gt; </code></pre> <p>The above section is repeated for the second PU (with names like emFactory2, transactionManager2 etc).</p> <p>I am a JPA newbie and I know that this is not the best solution. I appreciate any assistance in implementing this requirement in a better/elegant way!</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload