One solution is to setup N datasources each pointing to different database/schema:
DataSource ds1 = // setup ds 1
DataSource ds2 = // setup ds 2
and N entity manager factories (EMF) with 1-to-1 mapping with each ds :
EntityManagerFactory emf1 = // setup emf1 mapped to ds1
EntityManagerFactory emf2 = // setup emf2 mapped to ds2
Then you need N copies of each DAO classes injected with each EMF :
public class CustomerDAO {
private EntityManagerFactory emf;
public CustomerDAO(EntityManagerFactory emf) {
this.emf = emf;
}
// dao methods here..
}
CustomerDAO emf1CustomerDAO = // setup customer DAO injected with emf1
CustomerDAO emf2CustomerDAO = // setup customer DAO injected with emf2
Your application can now make a decision at runtime which database/schema to use by selecting the DAO