JPA dynamic persistence unit name

后端 未结 4 817
长发绾君心
长发绾君心 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: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.

提交回复
热议问题