Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all thanks to user332768 and bert. I tried using AbstractRoutingDataSource as mentioned in the link provided by bert, but got lost trying to hook up my jpa layer (eclipselink). I reverted to my older approach with some modifications. The solution looks cleaner (IMHO) and is working fine. (switching database at runtime and also writing to multiple databases in the same transaction)</p> <pre><code>public class MyEntityManagerFactoryImpl implements MyEntityManagerFactory, ApplicationContextAware { private HashMap&lt;String, EntityManagerFactory&gt; emFactoryMap; public EntityManager getEntityManager(String releaseId) { return SharedEntityManagerCreator.createSharedEntityManager(emFactoryMap.get(releaseName)); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Map&lt;String, LocalContainerEntityManagerFactoryBean&gt; emMap = applicationContext.getBeansOfType(LocalContainerEntityManagerFactoryBean.class); Set&lt;String&gt; keys = emMap.keySet(); EntityManagerFactory entityManagerFactory = null; String releaseId = null; emFactoryMap = new HashMap&lt;String, EntityManagerFactory&gt;(); for (String key:keys) { releaseId = key.split("_")[1]; entityManagerFactory = emMap.get(key).getObject(); emFactoryMap.put(releaseId, entityManagerFactory); } } } </code></pre> <p>I now inject my DAO's with an instance (singleton) of MyEntityManagerFactoryImpl. The dao will then simply call createSharedEntityManager with the required release and will get the correct EntityManager for that database. (Note that i am now using application managed EntityManager and hence i have to explicitly close them in my dao)</p> <p>I also moved to jta transaction manager (to manage transaction across multiple databases) This is how my spring xml looks like now.</p> <pre><code>... &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="em_Rel1"&gt; &lt;property name="persistenceUnitName" value="PU1" /&gt; &lt;/bean&gt; &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="em_Rel2"&gt; &lt;property name="persistenceUnitName" value="PU2" /&gt; &lt;/bean&gt; &lt;bean class="org.springframework.transaction.jta.JtaTransactionManager" id="jtaTransactionManager"&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="jtaTransactionManager"/&gt; .... </code></pre> <p>Cheers! (comments are welcome)</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