Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:47:29+00:00 2026-05-14T20:47:29+00:00

My application data access layer is built using Spring and EclipseLink and I am

  • 0

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.

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

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
 }
}

My spring-beans xml looks like this..

<!--  First persistence unit  -->    
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emFactory1">
  <property name="persistenceUnitName" value="PU_1" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager1">
  <property name="entityManagerFactory" ref="emFactory1"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager1"/>

The above section is repeated for the second PU (with names like emFactory2, transactionManager2 etc).

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!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:47:30+00:00Added an answer on May 14, 2026 at 8:47 pm

    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)

    public class MyEntityManagerFactoryImpl implements MyEntityManagerFactory, ApplicationContextAware {
    
        private HashMap<String, EntityManagerFactory> emFactoryMap;
    
        public EntityManager getEntityManager(String releaseId) {
            return SharedEntityManagerCreator.createSharedEntityManager(emFactoryMap.get(releaseName));
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            Map<String, LocalContainerEntityManagerFactoryBean> emMap = applicationContext.getBeansOfType(LocalContainerEntityManagerFactoryBean.class);
            Set<String> keys = emMap.keySet();
            EntityManagerFactory entityManagerFactory = null;
            String releaseId = null;
            emFactoryMap = new HashMap<String, EntityManagerFactory>();
            for (String key:keys) {
                releaseId = key.split("_")[1];
                entityManagerFactory = emMap.get(key).getObject();
                emFactoryMap.put(releaseId, entityManagerFactory);
            }
        }
    }
    

    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)

    I also moved to jta transaction manager (to manage transaction across multiple databases)
    This is how my spring xml looks like now.

    ...
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="em_Rel1">
            <property name="persistenceUnitName" value="PU1" />
    </bean>
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="em_Rel2">
            <property name="persistenceUnitName" value="PU2" />
    </bean>
    
    <bean class="org.springframework.transaction.jta.JtaTransactionManager" id="jtaTransactionManager">
    </bean>
    <tx:annotation-driven transaction-manager="jtaTransactionManager"/>
    ....
    

    Cheers! (comments are welcome)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, the database character set and table character set are… May 16, 2026 at 10:52 am
  • Editorial Team
    Editorial Team added an answer I would try quoting all of the parameters, for example:… May 16, 2026 at 10:52 am
  • Editorial Team
    Editorial Team added an answer Try using a TransitioningContentContol instead of a ContentControl. How-to Video… May 16, 2026 at 10:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Can anyone suggest a simple Data Access Layer (C# .NET)? Not keen on using
I am using Spring MVC to build my web application, and I have a
I am trying to build a .NET web application using SQL to query AS400
I am working on a API to access data stored in a system. The
I have been using the Windsor IoC Container for my web-based application, to resolve
I am developing a small application using EF 4.0 and POCO. While testing my
Let's say that I'm currently designing an application where I will need to use
I'm writing a grails 1.3.2 application and implementing security with spring-security-core 1.0. For reasons
In a C++ application that can use just about any relational database, what would
I'm implementing a high traffic client web application that uses a lot of REST

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.