-->
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.  [ 2 posts ] 
Author Message
 Post subject: increase time of execution when use addOrder method
PostPosted: Fri Nov 09, 2012 12:25 am 
Newbie

Joined: Thu Jan 05, 2012 2:13 pm
Posts: 2
Good evening everyone, I have a strange problem with addOrder method in the api Criteria , oracle throws ORA-02393: exceeded call limit on CPU usage , i'm using the following class with hibernate 4.1.4 and oracle 11g express edition :

Code:
public class Atendido {

   @Id
   @GeneratedValue
   @Column(name = "ID")
   private Long id;

   @OneToOne(fetch = FetchType.LAZY, cascade = { javax.persistence.CascadeType.ALL })
   @JoinColumn(name = "ID_PESSOA_FISICA")
   private PessoaFisica pessoaFisica;

   @OneToOne(fetch = FetchType.LAZY, cascade = { javax.persistence.CascadeType.ALL })
   @JoinColumn(name = "ID_PESSOA_JURIDICA")
   private PessoaJuridica pessoaJuridica;

   @OneToMany(fetch = FetchType.LAZY, cascade = { javax.persistence.CascadeType.ALL }, orphanRemoval = true)
   @BatchSize(size = 50)
   @JoinColumn(name = "ID_ATENDIDO")
   private List<RgCriminal> rgs;

        .
        .
        //getters and setters

}


Code:
public class PessoaFisica {

   @Id
   @GeneratedValue
   @Column(name = "ID")
   private Long id;
   
   @Column(name = "NOME_PAI")
   private String nomePai;

   @Column(name = "NOME_NASCIMENTO")
   private String nomeNascimento;

   @Column(name = "NOME_MAE")
   private String nomeMae;

   @Column(name = "NOME_SOCIAL")
   private String nomeSocial;

   
        .
        .
        //getters and setters

}


Code:
public class PessoaJuridica {

   @Id
   @GeneratedValue
   @Column(name = "ID")
   private Long id;

   @Column(name = "RAZAO_SOCIAL")
   private String razaoSocial;

   @Column(name = "NOME_FANTASIA")
   private String nomeFantasia;   
   
   @Column(name = "REPRESENTANTE_LEGAL")
   private String representanteLegal;

        .
        .
        //getters and setters
   
}


Code:
public class RgCriminal {

   @Id
   @GeneratedValue
   @Column(name = "ID")
   private Long id;

   @Column(name = "RG_CRIMINAL")
   private String rgCriminal;
 
        .
        .
        //getters and setters

}



now i have the following method to search an Atendido class

Code:
@SuppressWarnings("unchecked")
   private List<Atendido> listAll(String term, Integer firstResult,
         Integer maxResults) throws ParseException {
      Criteria criteria = this.session.createCriteria(Atendido.class);
      criteria.createCriteria("pessoaFisica", "pessoaFisica",
            JoinType.LEFT_OUTER_JOIN);
      criteria.createCriteria("pessoaJuridica", "pessoaJuridica",
            JoinType.LEFT_OUTER_JOIN);
      criteria.createCriteria("rgs", "rg", JoinType.LEFT_OUTER_JOIN);

   
         Disjunction or = Restrictions.disjunction();
         for (String token : term.split(";")) {
            token = token.trim();
            if (!StringUtils.isEmpty(token)) {
               
                  or.add(my.Restrictions
                        .ilike("pessoaFisica.nomeNascimento", token,
                              MatchMode.ANYWHERE));
                  or.add(my.Restrictions
                        .ilike("pessoaFisica.nomeSocial", token,
                              MatchMode.ANYWHERE));
                  or.add(my.Restrictions
                        .ilike("pessoaJuridica.nomeFantasia", token,
                              MatchMode.ANYWHERE));
                  or.add(my.Restrictions
                        .ilike("pessoaJuridica.razaoSocial", token,
                              MatchMode.ANYWHERE));
                  or.add(Restrictions
                        .ilike("pessoaJuridica.representanteLegal",
                              token, MatchMode.ANYWHERE));
                  or.add(my.Restrictions
                        .ilike("pessoaFisica.nomeMae", token,
                              MatchMode.ANYWHERE));
               }
            }
      criteria.add(or);
      if (firstResult != null && maxResults != null) {
         criteria.setFirstResult(firstResult).setMaxResults(maxResults);
      } else if (maxResults == null) {
         criteria.setMaxResults(50);
      } else {
         criteria.setMaxResults(maxResults);
      }

      /* if comment this block execution ocurrs about 2 seconds , if i uncomment this block oracle returns a error ORA-02393: exceeded call limit on CPU usage because the execution ocurrs more than 20 seconds . It's a kind of bug ?
      criteria.addOrder(Order.asc("pessoaFisica.nomeMae"));
      criteria.addOrder(Order.asc("pessoaFisica.nomeNascimento"));
      criteria.addOrder(Order.asc("pessoaJuridica.razaoSocial"));
       */
      return criteria.list();

   }


Code:
package br.gov.sp.defensoria.common.hibernate.criterion;

import org.hibernate.Criteria;

import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.TypedValue;

import br.gov.sp.defensoria.common.util.StringUtil;

public class LikeExpression implements Criterion {
   private static final long serialVersionUID = 1L;
   private final String propertyName;
   private final Object value;
   private final Character escapeChar;
   private final boolean ignoreCase;

   public LikeExpression(String propertyName, String value,
         Character escapeChar, boolean ignoreCase) {
      this.propertyName = propertyName;
      this.value = value;
      this.escapeChar = escapeChar;
      this.ignoreCase = ignoreCase;
   }

   protected LikeExpression(String propertyName, String value) {
      this(propertyName, value, null, false);
   }

   protected LikeExpression(String propertyName, String value,
         MatchMode matchMode) {
      this(propertyName, matchMode.toMatchString(value));
   }

   protected LikeExpression(String propertyName, String value,
         MatchMode matchMode, Character escapeChar, boolean ignoreCase) {
      this(propertyName, matchMode.toMatchString(value), escapeChar,
            ignoreCase);
   }

   public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
         throws HibernateException {
      String searchFor = "áàâãªéèêóòôõºúùûçÁÀÂÃÉÈÊÓÒÔÕÚÙÛÇ";
      String replaceTo = "aaaaaeeeooooouuucaaaaeeeoooouuuc";
      Dialect dialect = criteriaQuery.getFactory().getDialect();
      String[] columns = criteriaQuery.findColumns(propertyName, criteria);
      if (columns.length != 1) {
         throw new HibernateException(
               "Like may only be used with single-column properties");
      }
      String escape = escapeChar == null ? "" : " escape \'" + escapeChar
            + "\'";
      String column = columns[0];

      if (ignoreCase) {
         if (dialect.supportsCaseInsensitiveLike()) {
            return "translate(" + column + ",'" + searchFor + "','"
            + replaceTo + "')" + dialect.getCaseInsensitiveLike() + " ?"
                  + escape;
         } else {
            return dialect.getLowercaseFunction() + '(' + "translate(" + column + ",'" + searchFor + "','"
            + replaceTo + "')" + ')'
                  + " like ?" + escape;
         }
      } else {
         return "translate(" + column + ",'" + searchFor + "','"
               + replaceTo + "') like ?" + escape;
      }
   }

   public TypedValue[] getTypedValues(Criteria criteria,
         CriteriaQuery criteriaQuery) throws HibernateException {
      return new TypedValue[] { criteriaQuery.getTypedValue(
            criteria,
            propertyName,
            ignoreCase ? StringUtil.removeAccentuation(value.toString()
                  .toLowerCase()) : StringUtil.removeAccentuation(value
                  .toString())) };
   }
}


Code:
package br.gov.sp.defensoria.common.hibernate.criterion;

import org.hibernate.criterion.Criterion;

import org.hibernate.criterion.MatchMode;

public class Restrictions {   
   public static Criterion ilike(String propertyName, String value,MatchMode matchMode) {
      return new LikeExpression(propertyName, value, matchMode, null, true);
   }
}


My table atendido( for class Atendido) has more than 400.000 lines . Anyone can explain this strange behaviour ?

Thanks


Top
 Profile  
 
 Post subject: Re: increase time of execution when use addOrder method
PostPosted: Wed Nov 14, 2012 6:10 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
ORA-02393: exceeded call limit on CPU usage because the execution ocurrs more than 20 seconds . It's a kind of bug ?


It's a feature, not a bug ;-)
It's a percautional feature of Oracle, which prevents sql's from taking to long to execute and therefore from
taking to many resources harming other users in their work.
If this happens often, you could ask the database administrator to increase the CPU_PER_CALL limit of the user profile.
But in first line I suggest to investigate why the query takes so much longer with the order clauses.
As first I would define on database an index on razaoSocial and a combined index on columns nomeMae and nomeNascimento. This may help Oracle to build the ordering faster.
If this does not help, I suggest to analyze the effective sql produced by Hiberante.
As there are more JOIN's involved, it could be that the resulting query is not optimal for the ordering clauses.
To detect the effective sql produced by Hiberante, I suggest you to use a jdbc sniffer like p6spy.


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