persistence@glassfish.java.net

Re: getSingleResult() . It's very slow...

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Thu, 10 Apr 2008 15:44:19 -0700

There might be several reasons:
- the query runs on a large table but the field is not mapped to a PK or an
indexed column;
- it's the 1st connection being created, or you are not using pooled connection,
and each time a new physical connection is created;
- a network problem between the client and the database;
- your timing includes more than just a query...

Regards,
-marina

MrFishKill wrote:
> Hi,
>
> I have a problem with getSingleResult(). It seems very slow , around 400
> milliseconds.
>
> This is a piece of my code: (Do you see anything wrong in it?):
>
> public <T> T findUniqueByX(Class<T> source_class, String field, Object
> value)
> {
> Object o = null;
> T ot = null;
>
> StringBuffer sbQuery = new StringBuffer();
> sbQuery.append("SELECT x FROM ");
> sbQuery.append(source_class.getSimpleName());
> sbQuery.append(" x WHERE ");
> sbQuery.append("x.");
> sbQuery.append(field);
> sbQuery.append("=?1");
>
> Query q = em.createQuery(sbQuery.toString());
> q.setParameter(1, value);
> try
> {
> o = q.getSingleResult();
> ot = source_class.cast(o);
> }
> catch (javax.persistence.NoResultException e)
> {
> ot = null;
> }
>
> return ot;
> }
>
> Thanks in advance