My connection class to the database is taking a long time to open the connection, it takes almost 20 seconds. I use Hibernate and the Bank is Postgres running localhost.
Am I doing something wrong? or to 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;
}
}
Is it normal to take about 20 seconds to open on localhost?
EDIT:
I was able to improve the loading time by 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's taking so long?