-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate + PostgreSQL : Slow at session.beginTransaction().
PostPosted: Thu Oct 31, 2013 1:53 pm 
Newbie

Joined: Thu Oct 31, 2013 1:05 pm
Posts: 3
So,

I've been searching for examples of Hibernate and learning a little...
Now, I have some working examples with 2 simple tables with a OneToMany relation, using Hibernate 4.2.7 and Posgresql JDBC 9.2.

I'm facing some problems with load time.

The first problem that I encountered was a slow connection time at this line:

Quote:
Oct 31, 2013 3:07:35 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=es2p1, password=****, autocommit=true, release_mode=auto}


I found a solution, was fixed adding this to the hibernate.cfg.xml:

Quote:
<!-- Avoid JDBC to load all MetaData -->
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>


Now... the actual problem that I've not found a solution is on this line:

Quote:
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Oct 31, 2013 3:18:40 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
main.java: Starting insert at 2013/10/31 15:35:44
DepartamentoService.java: Inserting Record
DepartamentoService.java: session.beginTrasaction()


It takes at least 2 and a half minute to go on... immediately after it, all the queries are executed almost instantly.
Please take a loot at the printed times.

Quote:
main.java: Starting insert at 2013/10/31 15:35:44
DepartamentoService.java: Inserting Record
DepartamentoService.java: session.beginTrasaction()
DepartamentoService.java: session.save()
Hibernate: select nextval ('controle_academico_t01.departamento_id_seq')
DepartamentoService.java: tx.commit()
Hibernate: insert into controle_academico_t01.departamento (nome, id) values (?, ?)
DepartamentoService.java: Done
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
DepartamentoService.java: Inserting Record
DepartamentoService.java: session.beginTrasaction()
DepartamentoService.java: session.save()
Hibernate: select nextval ('controle_academico_t01.departamento_id_seq')
DepartamentoService.java: tx.commit()
Hibernate: insert into controle_academico_t01.departamento (nome, id) values (?, ?)
DepartamentoService.java: Done
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
main.java: Ending insert at 2013/10/31 15:37:51



My code structure:
Image

Hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
      
<hibernate-configuration>
   <session-factory>

        <!-- Database connection settings -->   
      <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
      <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
      <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/controleacademico</property>
      <property name="hibernate.default_schema">controle_academico_t01</property>
      <property name="hibernate.connection.username">es2p1</property>
      <property name="hibernate.connection.password">123456</property>

      <!-- Avoid JDBC to load all MetaData -->
      <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
       
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
       
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

      <!-- Logging SQL to stdout -->
      <property name="show_sql">true</property>
      
      <!-- Mapping files -->
      <mapping class="com.entities.Curso" />
      <mapping class="com.entities.Departamento" />
      
   </session-factory>
</hibernate-configuration>


The method being stucked (without println's):
Code:
   public Integer adicionarDepartamento(Departamento departamento) {
      session = sessionFactory.openSession();
      
      Integer departamentoID = -1;
      Transaction tx = null;

      try{
         tx = session.beginTransaction();
         departamentoID = (Integer) session.save(departamento);
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace();
      } finally {
         session.close();
      }
      return departamentoID;
   }


The sessionFactory comes from HibernateFactory...
Code:
   public HibernateFactory() {
      sessionFactory = null;
      serviceRegistry = null;
      try
      {
         // Hibernate Configuration
         Configuration configuration = new Configuration();
         configuration.configure();
         serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
         sessionFactory = configuration.buildSessionFactory(serviceRegistry);
      }
      catch (Throwable ex)
      {
         System.err.println("Failed to create sessionFactory object."+ ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public SessionFactory getSessionFactory() {
      return this.sessionFactory;
   }
}


My main (without println's):
Code:
   public static void main(String[] args) {
      
      // Hibernate configurator
      HibernateFactory factory = new HibernateFactory();
      
      Departamento departamento1 = new Departamento("Departamento 1");
      Departamento departamento2 = new Departamento("Departamento 2");
      
      DepartamentoService dService = new DepartamentoService(factory.getSessionFactory());
      
      dService.adicionarDepartamento(departamento1);
      dService.adicionarDepartamento(departamento2);
      
      Curso curso1 = new Curso("Curso1", 10, departamento1);
      Curso curso2 = new Curso("Curso2", 500, departamento1);
      Curso curso3 = new Curso("Curso3", 100, departamento2);
      
      CursoService cService = new CursoService(factory.getSessionFactory());
      
      cService.adicionarCurso(curso1);
      cService.adicionarCurso(curso2);
      cService.adicionarCurso(curso3);

      cService.listCursos();
   }


If you need more data, please, ask me...
I'm stucked at this little problem.

Thank you in advance!


Top
 Profile  
 
 Post subject: Re: Hibernate + PostgreSQL : Slow at session.beginTransaction().
PostPosted: Thu Oct 31, 2013 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I don't have enough info to understand the details of what's wrong, but I would suggest to use a connection pool.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate + PostgreSQL : Slow at session.beginTransaction().
PostPosted: Thu Oct 31, 2013 2:32 pm 
Newbie

Joined: Thu Oct 31, 2013 1:05 pm
Posts: 3
sanne.grinovero wrote:
I don't have enough info to understand the details of what's wrong, but I would suggest to use a connection pool.


What do you need?
The problem is, the first insertion take 2 minutes to go on.
It's a simple insert, 2 fields, ID (auto-generated) and Name.
After that, the others insertions and selections goes instantly.

Thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate + PostgreSQL : Slow at session.beginTransaction().
PostPosted: Thu Oct 31, 2013 6:03 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
You would need to split that, and figure at which operation all that time is spent.
I suspect it's the connection opening, which is why using a proper Connection Pool to reuse connections would basically avoid the problem.

Why that happens, I don't know. Try simply opening the JDBC connection from the same machine with some simple Java code and measure how long it takes just to open the connection, I suspect Hibernate has nothing to do with it.

Note that using a connection pool is strongly recommended anyway ;-)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate + PostgreSQL : Slow at session.beginTransaction().
PostPosted: Thu Oct 31, 2013 7:27 pm 
Newbie

Joined: Thu Oct 31, 2013 1:05 pm
Posts: 3
sanne.grinovero wrote:
You would need to split that, and figure at which operation all that time is spent.
I suspect it's the connection opening, which is why using a proper Connection Pool to reuse connections would basically avoid the problem.

Why that happens, I don't know. Try simply opening the JDBC connection from the same machine with some simple Java code and measure how long it takes just to open the connection, I suspect Hibernate has nothing to do with it.

Note that using a connection pool is strongly recommended anyway ;-)


Got it,
The DB is at localhost, as you can see in the hibernate.cfg.xml.
I will run some isolated tests, create a connection pool c3p0 I think, and I will return with some results.

Thanks! =D


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.