JPA dynamic persistence unit name

后端 未结 4 710
长发绾君心
长发绾君心 2021-02-04 06:17

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 th

相关标签:
4条回答
  • 2021-02-04 06:19

    You can use the same persistence unit for this. You just need to provide a properties Map when you call createEntityManagerFactory() with the url/datasource you want to use.

    0 讨论(0)
  • 2021-02-04 06:29

    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 讨论(0)
  • 2021-02-04 06:29

    This probably is of no help to solve the problem, but you may like to know that this kind of problems is being discussed for the implementation of JPA 2.1

    This sounds like one of those cases of multitenancy:

    Proposal for Multitenancy Support in JPA 2.1 JSR-338

    0 讨论(0)
  • 2021-02-04 06:37

    Just an idea, not sure if it'll help: You may open a inputStream to read the persistence.xml file and replace these lines:

    <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/agendasccv2?zeroDateTimeBehavior=convertToNull"/>
    <property name="javax.persistence.jdbc.password" value="btxbtxbtx"/>
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    <property name="javax.persistence.jdbc.user" value="root"/>
    

    Then splash a connection config screen where the user logs in, and set the connection according to the user privileges on that file, then start the main application

    Not sure if that will help, it's just and idea. Depends on your application and business logic.

    0 讨论(0)
提交回复
热议问题