How to Optimize Java Connection with Postgres

Asked

Viewed 193 times

0

My connection class with the database is taking too long to open the connection, it takes almost 20 seconds. Hibernate Usage and the Bank is Postgres running localhost.

Am I doing something wrong? or can you optimize and open faster?

My class:

  public class Connection {

    private static final String UNIT_NAME = "NameConection";
    private EntityManagerFactory emf = null;
    private EntityManager em = null;
    public EntityManager getEntityManager() {
        if (emf == null) {
            Map<String, String> map = new HashMap<String, String>();
            String user = ConfiguracoesFile.getInstancia().getUsuario();
            String pass = ConfiguracoesFile.getInstancia().getSenha();
            String banco = ConfiguracoesFile.getInstancia().getBanco();
            String server = ConfiguracoesFile.getInstancia().getServidor();
            String porta = ConfiguracoesFile.getInstancia().getPorta();
            map.put("hibernate.connection.password", pass);
            map.put("hibernate.connection.username", user);
            map.put("hibernate.connection.url", "jdbc:postgresql://" + server + ":" + porta + "/" + banco);
            emf = Persistence.createEntityManagerFactory(UNIT_NAME, map);
        }
        return em;
    }

}

It is normal to take about 20 seconds to open on localhost?

EDIT:

I managed to improve a little the loading time using the properties

<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
  <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>

But it’s still taking almost 10 seconds to open the connection.

Is there any other property that may be incorrect and why it is taking long?

  • Try to define this property: map.put("hibernate.temp.use_jdbc_metadata_defaults","false");&#xA;. Reference

  • Me returns this Exception: org.hibernate.Hibernateexception: Access to Dialectresolutioninfo cannot be null when 'Hibernate.dialect' not set

No answers

Browser other questions tagged

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