-->
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.  [ 4 posts ] 
Author Message
 Post subject: Trying execute query directly through jdbc
PostPosted: Fri Sep 23, 2005 11:02 am 
Newbie

Joined: Thu Feb 10, 2005 2:33 pm
Posts: 7
Hello.

I'm facing a strange problem.
I'm developing an integration tool that will obtain data from other application and update values on a table.
To avoid overhead by hibernate class loading (there is about 7000 updates), I'm trying to execute update querys directly on database.
But on first query execution, the method freezes. Even debugging the code, the thread never finishes and the browser keeps "waiting answer from localhost". None error or exeption message is thrown. With any of the commented code it also happens.

Can somebody help me?

Hibernate version: 2.1.8

Code:
public void updateEstabelecimentosDMR(List<IntegracaoEstabDTO> dtos) throws DatabaseException {
Session session = getSession();
Transaction tx = null;
try{
//tx = session.beginTransaction();
Connection con = session.connection();
con.setAutoCommit(false);
Statement stmt = con.createStatement();
for (IntegracaoEstabDTO dto : dtos)
//stmt.executeUpdate("update Estabelecimento set totalAlunos = " + dto.getTotalAlunos() + " where municipio.codigo = " + dto.getCodigoMunicipio() + " and codigoEstabelecimento = " + dto.getCodigoEstabelecimento());
stmt.addBatch("update Estabelecimento set totalAlunos = " + dto.getTotalAlunos() + " where municipio.codigo = " + dto.getCodigoMunicipio() + " and codigoEstabelecimento = " + dto.getCodigoEstabelecimento());
//stmt.execute("update Estabelecimento set totalAlunos = " + dto.getTotalAlunos() + " where municipio.codigo = " + dto.getCodigoMunicipio() + " and codigoEstabelecimento = " + dto.getCodigoEstabelecimento());

stmt.executeBatch();
con.commit();
con.setAutoCommit(true);
stmt.close();
session.flush();
//tx.commit();
}
catch(HibernateException he){
try{
if(tx!=null) tx.rollback();
}
catch(HibernateException e){
log.error("Erro ao rollback a transação: "+e,e);
}
String erro = agrupaMensagens(he);
log.error("Erro ao atualizar total de alunos dos estabelecimentos (DMR): " + erro,he);
throw new DatabaseException(formataErro(he));
}
catch(SQLException sqle){
log.error("Erro ao atualizar total de alunos dos estabelecimentos (DMR) :" + sqle.getMessage(),sqle);
if(sqle.getNextException()!= null)
log.error("Erro (DMR) - Next Excepion :" + sqle.getNextException().getMessage(),sqle.getNextException());
throw new DatabaseException(sqle);
}
finally{
close(session);
}
}


Name and version of the database you are using: PostgreSQL 7.4.1


Top
 Profile  
 
 Post subject: H2 TX
PostPosted: Fri Sep 23, 2005 5:24 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Try to remove transaction management on connection and use standard Hibernate TX management (Note: it is not quite according to documentation. In H3 we must manage transaction boundaries on connection because H3 returns new connection, but H2 returns current H session connection)

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Performance problem
PostPosted: Wed Sep 28, 2005 4:28 pm 
Newbie

Joined: Thu Feb 10, 2005 2:33 pm
Posts: 7
Thank you very much for your answer.
This is the code resultig after your suggestion:

Code:
Transaction tx=null;
      try{
         tx = session.beginTransaction();
         Connection con = session.connection();
         Statement stmt = con.createStatement();
         for (Iterator iter = dtos.iterator(); iter.hasNext();) {
            IntegracaoEstabDTO dto = (IntegracaoEstabDTO) iter.next();
            //stmt.execute("update estabelecimento set totalAlunos = " + dto.getTotalAlunos() + " where municipio.codigo = " + dto.getCodigoMunicipio() + " and codigoEstabelecimento = " + dto.getCodigoEstabelecimento());
            stmt.addBatch("update Estabelecimento set totalAlunos = " + dto.getTotalAlunos() + " where municipio.codigo = " + dto.getCodigoMunicipio() + " and codigoEstabelecimento = " + dto.getCodigoEstabelecimento());
         }
         stmt.executeBatch();
         session.flush();
         tx.commit();
      }
      catch(HibernateException he){ ....


It works, executing all the querys.
The problem now is the time that every query spends running, about 0.75 seconds. Considering that there are about 7000 querys, it will take 1 hour to run. If I run this code, without get the connection from hibernate, all the process takes about 40 seconds.
Is something I'm doing wrong?
Thanks in advice.


Top
 Profile  
 
 Post subject: starnge
PostPosted: Thu Sep 29, 2005 1:57 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I am not sure what you mean by saying ‘every query’.
Did you try to profile this piece of code and see where time has gone?
As a suggestion: try preparing all your queries outside of transactional boundaries like:
List qs = new ArrayList();
Statement stmt = con.createStatement();
for (Iterator iter = dtos.iterator(); iter.hasNext();) {
IntegracaoEstabDTO dto = (IntegracaoEstabDTO) iter.next();

qs.add ("update Estabelecimento set totalAlunos = " + dto.getTotalAlunos() + " where municipio.codigo = " + dto.getCodigoMunicipio() + " and codigoEstabelecimento = " + dto.getCodigoEstabelecimento());
}

and then execute all the queries within a transaction.

As a wild guess: I do not know what JDBC driver and DB you are using but problem might be with addBatch/executeBatch because the methods are optional and their implementation might be suboptimal.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.