I need to switch the database dynamically on my application in runtime. So a do this class to producer my own entitymanager:
@ApplicationScoped
public class ApplicationResources {
@PersistenceContext
private EntityManager entityManager;
@Produces
@Default
@RequestScoped
public EntityManager produceEntityManager() {
Map<Object, Object> props = new HashMap<Object, Object>();
props.put(PersistenceUnitProperties.JTA_DATASOURCE, DATABASENAME);
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PU", props);
return entityManagerFactory.createEntityManager();
}
public void dispose(@Disposes EntityManager entityManager) {
entityManager.close();
}
}
So I use @Inject to get a entityManager and do all the persistence operations on database. I have two problems:
- When I change the jta-data-source the Eclipselink takes a lot of time to connect. And its run all validations again (like in application startup). I need to change connection more fast.
- I can't save or update objects when I'm logged in. I can update and get them all (objects) just it.
I used this approach for seeming simpler. Because my application is already quite large. And now I need multiple clients in other databases. Help me there .. maybe this could be very wrong, but the multitenancy approaches with eclipselink seemed very complex. I need the idea of multitenancy with a bunch of different data. But if I could only change the connected database when and where I wanted, it would solve a lot.
I am using the Wildfly Server, CDI, Eclipselink, JTA and JSF.
Thank you for your help.