How do I connect to multiple databases using JPA?

前端 未结 1 1229
说谎
说谎 2020-12-14 08:13

I have an application using Java servlets/JSP\'s. There are multiple clients using my app, however each client has a separate database. All the databases have the same schem

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 09:01

    1) Create several persistent units in your persistence.xml with different names.

    2) Create necessary number of EntityManagerFactorys (1 per persistence-unit) and specify which persistence-unit should be used for concrete factory:

    
       
    
    

    3) Create necessary number of TransactionManager s:

    
       
    
    

    4) In your DAO's classes specify with which persistence-unit (and so with which EntityManagerFactory) you want to work:

    public class AbstractAuthDao { 
    
       @PersistenceContext (unitName = "SpringSecurityManager")
       protected EntityManager em;
    
        ...
    }
    

    5) In your service-objects specify which TransactionManager should be used (this feature is supported only in Spring 3.0):

    @Transactional (value = "authTransactionManager", readOnly = true)
    public class UserServiceImpl implements UserService {
    
       ...
    }
    

    6) If you have OpenEntityManagerInViewFilter in your web.xml, then specify in its init-param name of necessary EntityManagerFactory (or create several filters with correspondent init-blocks):

    
        entityManagerFactoryBeanName
        authEntityManagerFactory
    
    

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