Accessing multiple database from a Java web application using JPA/EclipseLink/EJB

前端 未结 5 452
猫巷女王i
猫巷女王i 2020-12-29 15:21

I have built a simple SOAP java application(server side) and I am using Glassfish4,JPA/EclipseLink,EJB. I have set the db connections(resources/poo

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 15:52

    My solution would be to add a second persistence unit for the second database, then refactor your GenericDAO so that the EntityManager is not an attribute of the class, but passed into each method. I would then create facade objects for each of your databases which get the GenericDAO and the relevent EntityManager injected into them. If you really wanted you could have a common interface to keep the api the same. It might look like this:

    persistence.xml

    
    
    
        
            org.eclipse.persistence.jpa.PersistenceProvider
            jdbc/simfin
            org.demo.model.MemRegMcgEntity
            org.demo.model.SavAccHolderMcgEntity
            org.demo.model.SavAccMcgEntity
            org.demo.model.SavTransactionEntity
        
    
        
            org.eclipse.persistence.jpa.PersistenceProvider
            jdbc/other-jta-datasource
            org.demo.model.OtherEntityOne
            org.demo.model.OtherEntityTwo
            org.demo.model.OtherEntityThree
            org.demo.model.OtherEntityFour
        
    
    
    

    Generic DAO:

    public class GenericDAO {
    
    public void  save(EntityManager em, T entity) {
        em.persist(entity);
    }
    

    Entity Interface:

    public Interface IEntity {
        ....
    }
    

    Entity Class:

    public class SomeEntity implements IEntity {
        ....
    }
    

    DAO Facade Database One:

    public class GenericFacadeOne {
    
    @PersistenceContext(unitName = "SavingBalanceDemoServer_PU")
    private EntityManager em; 
    @Autowired
    private GenericDao dao;
    
    @Transactional(propogation=Propogation.REQUIRED)
    public void saveSomeEntity(SomeEntity entity) {
        getDao().save(getEm(), entity);
    }
    
    public void setEm(EntityManager em) {
        this.em = em;
    }   
    
    public EntityManager getEntityManager() {
        return this.em;
    }
    
    public void setDao(GenericDao dao) {
        this.em = em;
    }   
    
    public GenericDao getDao() {
        return this.dao;
    }
    }
    

    DAO Facade Database Two:

    public class GenericFacadeTwo {
    
    @PersistenceContext(unitName = "MySecondPersistenceUnit_PU")
    private EntityManager em; 
    @Autowired
    private GenericDao dao;
    
    @Transactional(propogation=Propogation.REQUIRED)
    public void saveSomeEntity(SomeEntity entity) {
        getDao().save(getEm(), entity);
    }
    
    public void setEm(EntityManager em) {
        this.em = em;
    }   
    
    public EntityManager getEntityManager() {
        return this.em;
    }
    
    public void setDao(GenericDao dao) {
        this.em = em;
    }   
    
    public GenericDao getDao() {
        return this.dao;
    }
    }
    

    Hopefully that makes sense, let me know if you need any clarification!

提交回复
热议问题