Various databases accessed at the same time JPA

Asked

Viewed 251 times

1

Today I have the following scenario: my customers enter my site and appears the login screen, where I ask the User Group, Login and Password.

For the Group I have a database, where it has the data of the user’s database ie, on which server it is allocated the name etc.

With that, I check if the group exists. If it exists I close the connection of my Group database, which is called Manager, and open the connection of the user’s database.

Each user has their database, so I open the connection of the user’s database after checking if there is a group and write to one HTTPSESSION (Obs : I record the EntityManegerFactory in session), so every time he goes to do a search, I do the EntityManager receive this Factory session.

I wonder if this can slow down the system, because my java server is crashing, I wonder if this is the reason why.

Here I write the customer database in the session:

 public EntityManager getEntityManager(String PU, String Local, String endfdb) {

    if (PU.equals("0")) {
        GestorEMF = Persistence.createEntityManagerFactory("GestorPU");
        return GestorEMF.createEntityManager();
    } else {
        Properties props = new Properties();

        props.setProperty("hibernate.connection.url", "jdbc:firebirdsql:"+endfdb+"/3050:" + Local);

        usrEMF = Persistence.createEntityManagerFactory(PU, props);
        HttpSession sess = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        sess.setAttribute("mitryusEMF", mitryusEMF);
        return mitryusEMF.createEntityManager();
    }
}

Here I give the EM every time the user needs:

 public EntityManager getEntityMitryus() {
    EntityManagerFactory usuario = (EntityManagerFactory) getSession().getAttribute("mitryusEMF");

    return usuario.createEntityManager();
}
  • You didn’t say which bank you use, but I’ve implemented a system similar to Postgresql using only one EntityManagerFactory, however varying the schema. Within a user request, each time a connection will be made, I check which is the schema suitable to suit the user, depending on a number of business rules, and modify the new connection (or withdrawal from the pool) by setting the schema selected.

  • Unfortunately the EMF It is expensive to create and maintain in session does not seem to me something very cool, because it means keep one per user (this if the user uses only one session), which is unnecessary and will decrease the scalability of the system. Another idea would be to keep one EMF by bank in a "pool" maintained by you. If you use Spring Framework it shouldn’t be difficult to create a facotry for this. Anyway, these are just considerations, because sometimes it’s better to rethink what we’re using than to keep trying to solve more complicated problems.

  • Buddy I get me an ex on this

No answers

Browser other questions tagged

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