private class HibernateIndexEntitiesIndexer implements IndexEntitiesIndexer { public void performIndex(CompassSession session, IndexEntity[] entities) { for (int i = 0; i < entities.length; i++) { HibernateEntityInfo entityInfo = (HibernateEntityInfo) entities; HibernateSessionWrapper sessionWrapper = doGetHibernateSessionWrapper(); ScrollableResults cursor = null; try{ sessionWrapper.open(); cursor = sessionWrapper.getSession().createQuery(entityInfo.getSelectQuery()).setFetchSize(fetchCount).scroll(ScrollMode.FORWARD_ONLY); int count = 0; while(cursor.next()){ Object item = cursor.get(0); session.create(item); sessionWrapper.getSession().evict(item); count++; if(count == fetchCount){ session.evictAll(); count=0; } } cursor.close(); sessionWrapper.close(); } catch (Exception e) { log.error(buildMessage("Failed to index the database"), e); if(cursor != null){ cursor.close(); } sessionWrapper.closeOnError(); if (!(e instanceof HibernateGpsDeviceException)) { throw new HibernateGpsDeviceException(buildMessage("Failed to index the database"), e); } throw (HibernateGpsDeviceException) e; } } } }