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 7751025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:26:33+00:00 2026-06-01T11:26:33+00:00

I need a way to dynamically specify the persistence unit in a EJB. Simplified

  • 0

I need a way to dynamically specify the persistence unit in a EJB.

Simplified example:

I have an application using multiple databases as data stores.
Each of the data stores are structurally the same.
Depending on the client connecting to the application I need to access the data from
a specific data store.

Therefore, I would like to use the same EJB so that the business logic is not duplicated,
but then just select the correct persistence unit based on the client.

Up to this point I’ve only directly injected the entity manager with the persistence unit name hard coded.
Is there a way I can dynamically inject the entity manager with the required persistence unit attached to the EJB?
Also, can persistence units be added dynamically during runtime?
I currently have to specify the persistence unit in the persistence.xml file.
Ideally I would like to create pools on the server jdbc/db1, jdbc/db2 etc as required while the system is running. Then just add these to the central client database and link it to a client, so that when the client connects, it will check the name of the pool, and use it when calling the EJB to get the persistence unit.

I’m still really new to Java EE development. Any help would be greatly appreciated.

  • 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-06-01T11:26:35+00:00Added an answer on June 1, 2026 at 11:26 am

    In the current JPA version, it’s unfortunately not possible to create persistence units dynamically. If this functionality it’s important for you, you could consider creating a JIRA issue for it at the JPA issue tracker: http://java.net/jira/browse/JPA_SPEC

    Using the @PersistenceContext annotation, it’s also not possible to dynamically select a specific persistence unit. This is actually the domain of sharding, which Hibernate once tried to address but then suddenly discontinued. See http://www.hibernate.org/subprojects/shards.html

    There are however a few thing you could do to get a similar effect.

    One approach is to create a Stateless EJB/CDI factory bean, that you inject with all your entity managers. The cost of this is marginal, since those beans will be pooled and entity managers are not that expensive to be created in the first place.

    If you also want to inject them based on some condition, then this condition will have to be available from the context or has to be specified at the injection point (but if you would do that, you could just as well inject the right entity manager directly).

    A kickoff example:

    @Stateless
    @TransactionAttribute(SUPPORTS)
    public class ShardingEntityManagerFactory {
    
        @Resource
        private SessionContext sessionContext;
    
        @PersistenceContext(unitName = "pu1")
        private EntityManager entityManager1;
    
        @PersistenceContext(unitName = "pu2")
        private EntityManager entityManager2;
    
        @Produces @TransactionScoped @ShardedPersistenceContext
        public EntityManager getEntityManager() {
            if (sessionContext.isCallerInRole("FOO")) {
                return entityManager1;
            } else {
                return entityManager2;
            }
        }
    }
    

    And then in your beans:

    @Stateless
    public class SomeBean {
    
        @Inject @ShardedPersistenceContext
        private EntityManager entityManager;
    
        // ...
    }
    

    Note that you would need Seam Persistence here for the @TransactionScoped annotation. It also might be easier, but a little more verbose, to forget about injecting the entity manager transparently and inject the ShardingEntityManagerFactory instead and obtain the right one from it manually.

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

Sidebar

Related Questions

I need a way to store application-level data (i.e. cross user sessions) in ASP.NET.
I need a way to dynamically change a reports file name based on a
Is there a way to dynamically impersonate a user in asp.net? I need to
I have a movieclip that I need to duplicate dynamically based on an outside
I need a way to recreate dynamically generated reports at some point in the
I have a dynamically generated path whose data is composed of a PathGeometry composed
Using NHibernate, I need to be able to configure my application to sort a
I need to build a form dynamically putting inputText field, I'm using this code:
I need a way to dynamically populate this query... to avoid repeating the same
I need a way for a single variable to represent two kinds of objects

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.