JSF + Wildfly multi-bank connection dynamically

Asked

Viewed 287 times

2

Hello, I have a web application using JSF + CDI + JPA and Wildfly 8 as the application server. So far I’m letting the server manage transactions, where the connection information with the database, email services, authentication... is in the server settings (standalone.xml). Now my problem is that I need the system to access several databases. So that when the customer is logged in, they enter their login and password and also choose the database.

I’ve researched enough and solutions like multi-tenancy didn’t seem very interesting. I also thought of taking this responsibility of the server, making the connection in the application and still managing uses the injections normally, but I would have to make many changes in the system.

Any hint or idea how to solve this problem?

Thanks a lot, guys!

UPDATING

I am using an approach in which I create my Entitymanagers dynamically during the same application. So I change only the jta-data-source attribute and so I can change the database I access. It works, but I’m having 2 problems:

  1. When changing the database Eclipselink takes a long time, because it performs all those validations of when we start the server.
  2. Even with the slowness, I can connect to the bank normally and log in. The system lists the data registered correctly. However the problem is time to persist the data create/ change and delete. I have problems in the transaction and persistence does not happen.

This is an example of what I’m doing to create the Entity Managers dynamically (in my application only changes how to send the name of the bank, 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 entityManager becomes what I created dynamically. I am setting my datasources in the Wildfly standalone.xml and they are Xa-datasource.

  • Take a look at this link accessing-multiple-databases-with-jpa-e-cdi I’ve used this guide, it worked perfectly.

  • I’m doing something like this now... but I’m creating the Entity managers dynamically, just changing the jta-data-source attribute. I can’t do it according to the link you sent me because it sets the persistence units static. In my case I can have N clients and N database (it is a system requirement). So what I did was I changed the jta-data-source attribute when creating Entity manager. But I still have problems.

  • The problem with all this is that when I change the jta database the eclipse link connects to the other database and takes a long time. But in the end it as follows connect. The problem after that is that I had problems with data persistence. I can only retrieve data from the bank is no longer delete, alter or create. I will update the question by placing the code Sá new approach I’m trying.

  • I was able to solve the problem of slowness in connecting to the other banks. Now I have the second problem, after logging in I can not remove or create/edit the objects in the database.

  • The solution was to remove this folder in Wildfly (as suggested in this question and then I put the eclipselink validation-only attribute to "true" https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_validation_only.htm. I can now switch between banks and log in to each specific user of each bank. The problem remains that I can not save or delete the objects in the bank.

  • I solved the problem! I just needed to add entityManager.joinTransaction(); in my methods: save, update and delete ... so that’s it :) Now I can access several databases dynamically and is not slow.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.