Vertago
Vertago

Reputation: 319

Efficient way to connect to database (by performance)

I want to build a programm, which connects to a database. Inprinciple, my code works. I use "Hibernante-4.3.1" and a postgresql-driver "postgresql-9.3-1100.jdbc41.jar".

My persistence.xml looks like this:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0">
<persistence-unit name="******" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
  <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
  <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
  <property name="hibernate.connection.username" value="******"/>
  <property name="hibernate.connection.password" value="******"/>
  <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/*******"/>          
  <property name="hibernate.hbm2ddl.auto" value="create"/>        
</properties>
</persistence-unit>
</persistence>

For localhost, it's okeyishly fast, but if I want to connect to a external server via internet, it takes about 30-60 seconds to establish the connection. Once it is initialised, all subsequent requests are executed fast enough, but the first call is taking way to long.

I could restructure the whole project as a WEB-Project and make a JBoss Datasource via JTA. That way, the connection is established before the programm starts and all would be fine. But I'd would like it a lot more to have if I didn't have to do that. What's the right way to connect like this?

Edit: Some more information: The line which takes the long time is:

javax.persistence.EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneGramService");

Greetings, Rhodarus

Upvotes: 1

Views: 599

Answers (1)

Sezin Karli
Sezin Karli

Reputation: 2525

Try to set hibernate.temp.use_jdbc_metadata_defaults property to false.

Upvotes: 1

Related Questions