How to point through Login, which database the system should connect via Hibernate

Asked

Viewed 40 times

0

I am developing a JAVA system with JSF, Primefaces and Hibernate with Mysql database. In this system the user must inform in the login the bank that wants to connect (User, password and bank). The problem is that when the user calls the Login screen Hibernate already starts the factory of sessions before the user informs the connection data. I wonder if there is a way that Hibernate only start the manufacture of sessions after the user click on Login.

  • Do you have several databases already created? And the user must log into the databases or use their data to log into the system?

  • I have several bases. In Login the person informs, user, password and the base you want to log in. Based on this information the system should connect on the basis indicated for making notes.

  • How are you declaring your connection factories..? It may be that the way it is, it automatically starts with the system.. here is a way to start it programmatically.. with this you can start whatever you want.. http://allandequeiroz.io/2017/02/05/creating-jpa-entity-manager-programmatically-with-hibernate-and-cdi/

  • I’ll try to post the code.

1 answer

0

    private static SessionFactory criarFabricaDeSessoes(String nomeDoArquivo) {
    try {
        Configuration configuracao = new Configuration().configure(nomeDoArquivo);

        ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(configuracao.getProperties())
                .build();

        SessionFactory fabrica = configuracao.buildSessionFactory(registro);

        return fabrica;
    } catch (Throwable ex) {
        System.err.println("A fábrica de sessões não pode ser criada." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

Browser other questions tagged

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