Hello, I have a Web application using JSF + CDI + JPA and WildFly 8 as an application server. So far I'm letting the server manage the transactions, where database connection information, email services, authentication ... are in the server settings (standalone.xml). Now my problem is that I need the system to access multiple databases. So when the client is logged in, he will enter his login and password and also choose the database.
I've done a lot of research and solutions like multi-tenancy did not seem very interesting. I also thought about taking this responsibility from the server, making the connection in the application and still getting uses the injections normally, but I would have to make many changes to the system.
Any tips or ideas on how to solve this problem?
Thank you in advance!
UPDATE
I'm using an approach where I create my EntityManagers dynamically during the same application. So I only change the jta-data-source attribute and so I can change the database I access. It works, but I have 2 problems:
This is an example of what I'm doing to create Entity Managers dynamically (in my application it just changes how to send the name of the database, I just simplified the code):
@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();
}
}
After that I removed the @PersistenceContext annotations from my Stateless classes and put the @Inject annotations, so the entityManager happens to be the one I created dynamically. I am setting my datasources in the Wildfly standalone.xml and they are of the xa-datasource type.