-->
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: Solution to pagination using Future
PostPosted: Wed Aug 11, 2010 8:28 am 
Newbie

Joined: Thu Feb 14, 2008 3:26 am
Posts: 18
Hi,

I've create an EJB to work with pagination querys. It uses Futures to work.

Example of use:
Code:
@EJB private KyrianPagingUtility pagingUtility;
...
javax.persistence.Query qMisEmpresas = entityManager.createQuery(query.toString());
javax.persistence.Query qCountMisEmpresas = entityManager.createQuery(queryCount.toString());

return pagingUtility.getPage(qMisEmpresas, qCountMisEmpresas, new Interval(start,limit));


It works ok, but in some cases, it returns this exception:
Code:
javax.ejb.EJBException
   at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
   at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5017)
   at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4805)
   at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2004)
   at com.sun.ejb.containers.EjbAsyncTask.call(EjbAsyncTask.java:110)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
   at java.lang.Thread.run(Thread.java:619)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:250)
   at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getResultList(CriteriaQueryCompiler.java:260)
   at org.kyrian.dao.util.KyrianPagingUtility.list(KyrianPagingUtility.java:51)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1056)
   at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1128)
   at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5292)
   at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:615)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
   at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:567)
   at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:157)
   at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:139)
   at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:858)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
   at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:367)
   at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5264)
   at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5252)
   at com.sun.ejb.containers.EjbAsyncTask.call(EjbAsyncTask.java:97)
   ... 5 more
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.loader.Loader.doList(Loader.java:2297)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
   at org.hibernate.loader.Loader.list(Loader.java:2167)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
   ... 28 more
Caused by: java.sql.SQLException: Connection closed
   at com.sun.gjc.spi.base.ConnectionHolder.checkValidity(ConnectionHolder.java:703)
   at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:533)
   at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:251)
   at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:48)
   at com.sun.gjc.spi.ManagedConnection.prepareCachedStatement(ManagedConnection.java:880)
   at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:169)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
   at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1596)
   at org.hibernate.loader.Loader.doQuery(Loader.java:717)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
   at org.hibernate.loader.Loader.doList(Loader.java:2294)
   ... 36 more


Top
 Profile  
 
 Post subject: Re: Solution to pagination using Future
PostPosted: Wed Aug 11, 2010 8:29 am 
Newbie

Joined: Thu Feb 14, 2008 3:26 am
Posts: 18
KyrianPagingUtility.class

Code:
package org.kyrian.dao.util;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.kyrian.data.ifc.data.pagination.IPage;
import org.kyrian.utility.util.logs.KyrianLoggerFactory;
import org.slf4j.Logger;






/**
* Session Bean implementation class KyrianPagingUtility
*/
@Stateless
@LocalBean
public class KyrianPagingUtility {
   
   private static final Logger log = KyrianLoggerFactory.getLogger(KyrianPagingUtility.class);
   
   @EJB private KyrianPagingUtility _this;

   /**
    * Devuelve los datos en la ventana
    * especificada por el intervalo
    * @param <T>
    * @param query
    * @param intervalo
    * @return
    */
   @SuppressWarnings("unchecked")
   @Asynchronous
    public <T extends Serializable> Future<List<T>> list(Query query,int start,int results){
      
      query.setFirstResult(start);
      query.setMaxResults(results);
      
      return new AsyncResult<List<T>>(query.getResultList());
             
    }
   
   
   /**
    * Devuelve el número total de los registros en una query
    * @param query
    * @return
    */
   @Asynchronous
   public Future<Long> count(Query query){
      
      Object oTotal = null;
      try{
         oTotal = query.getSingleResult();
      }catch(NoResultException e){
         
      }
      if(oTotal != null){
         Long lTotal = Long.valueOf(((Number) oTotal).longValue());
         return new AsyncResult<Long>(lTotal);
      }
      return new AsyncResult<Long>(0L);
             
    }
   
   
   /**
    *
    * @param <T>
    * @param query
    * @param countQuery
    * @param interval
    * @return
    */
   public <T extends Serializable> IPage<T> getPage(Query query,Query countQuery,Interval interval){
      Future<Long> total = _this.count(countQuery);
      Future<List<T>> listado = _this.list(query, interval.getStart(),interval.getLimit());
         
      DefaultPage<T> page = new DefaultPage<T>();
      try {
         page.setElements(listado.get());
         page.setTotalElements(total.get().intValue());
      } catch (InterruptedException e) {
         log.error("GetPage de forma asíncrona",e);
      } catch (ExecutionException e) {
         log.error("GetPage de forma asíncrona",e);
      }
      
      return page;
   }
}


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.