Example usage for org.hibernate ScrollableResults close

List of usage examples for org.hibernate ScrollableResults close

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults close.

Prototype

void close();

Source Link

Document

Release resources immediately.

Usage

From source file:org.eclipse.emf.cdo.server.internal.hibernate.HibernateQueryHandler.java

License:Open Source License

/**
 * Executes hql queries. Gets the session from the {@link HibernateStoreAccessor} creates a hibernate query and sets
 * the parameters taken from the {@link CDOQueryInfo#getParameters()}. Takes into account the
 * {@link CDOQueryInfo#getMaxResults()} and the {@link IHibernateStore#FIRST_RESULT} values for paging.
 *
 * @param info//w w w .  ja va2  s  .com
 *          the object containing the query and parameters
 * @param context
 *          the query results are placed in the context
 * @see IQueryHandler#executeQuery(CDOQueryInfo, IQueryContext)
 */
public void executeQuery(CDOQueryInfo info, IQueryContext context) {
    // get a transaction, the hibernateStoreAccessor is placed in a threadlocal
    // so all db access uses the same session.
    final Session session = hibernateStoreAccessor.getHibernateSession();
    try {
        // create the query
        final Query query = session.createQuery(info.getQueryString());
        query.setReadOnly(true);

        // get the parameters with some parameter conversion
        int firstResult = -1;
        boolean cacheResults = true;
        for (String key : info.getParameters().keySet()) {
            if (key.compareToIgnoreCase(IHibernateStore.CACHE_RESULTS) == 0) {
                try {
                    cacheResults = (Boolean) info.getParameters().get(key);
                } catch (ClassCastException e) {
                    throw new IllegalArgumentException("Parameter " + IHibernateStore.CACHE_RESULTS //$NON-NLS-1$
                            + " must be a boolean. errorMessage " + e.getMessage());
                }
            } else if (key.compareToIgnoreCase(IHibernateStore.FIRST_RESULT) == 0) {
                final Object o = info.getParameters().get(key);
                if (o != null) {
                    try {
                        firstResult = (Integer) o;
                    } catch (ClassCastException e) {
                        throw new IllegalArgumentException(
                                "Parameter firstResult must be an integer but it is a " + o //$NON-NLS-1$
                                        + " class " + o.getClass().getName()); //$NON-NLS-1$
                    }
                }
            } else {
                // in case the parameter is a CDOID get the object from the db
                final Object param = info.getParameters().get(key);
                if (param instanceof CDOID && HibernateUtil.getInstance().isStoreCreatedID((CDOID) param)) {
                    final CDOID id = (CDOID) param;
                    final String entityName = HibernateUtil.getInstance().getEntityName(id);
                    final Serializable idValue = HibernateUtil.getInstance().getIdValue(id);
                    final CDORevision revision = (CDORevision) session.get(entityName, idValue);
                    query.setEntity(key, revision);
                    if (cacheResults) {
                        addToRevisionCache(revision);
                    }
                } else {
                    query.setParameter(key, param);
                }
            }
        }

        // set the first result
        if (firstResult > -1) {
            query.setFirstResult(firstResult);
        }

        // the max result
        if (info.getMaxResults() != CDOQueryInfo.UNLIMITED_RESULTS) {
            query.setMaxResults(info.getMaxResults());
        }

        final ScrollableResults scroller = query.scroll(ScrollMode.FORWARD_ONLY);

        // and go for the query
        // future extension: support iterate, scroll through a parameter
        int i = 0;
        try {
            while (scroller.next()) {
                Object[] os = scroller.get();
                Object o;
                if (os.length == 1) {
                    o = handleAuditEntries(os[0]);
                } else {
                    o = handleAuditEntries(os);
                }

                final boolean addOneMore = context.addResult(o);
                if (cacheResults && o instanceof CDORevision) {
                    addToRevisionCache((CDORevision) o);
                }
                if (o instanceof InternalCDORevision) {
                    ((InternalCDORevision) o).freeze();
                }

                // clear the session every 1000 results or so
                if (i++ % 1000 == 0) {
                    session.clear();
                }

                if (!addOneMore) {
                    return;
                }
            }
        } finally {
            scroller.close();
        }
    } finally {
        session.close();
    }
}

From source file:org.eurocarbdb.action.core.SearchGlycanSequence.java

License:Open Source License

public List<GlycanSequence> getQueryResults() {
    Criteria query = createCriteria();//  w w w  . jav a  2s.  com

    log.info("Performing query: " + query.toString());
    setMessage(query.toString());

    ScrollableResults scroll = null;
    try {

        scroll = query.scroll();

        scroll.last();
        setTotalResults(scroll.getRowNumber() + 1);

        int count = getTotalResults();

        int first = getOffset();
        int max = getMaxResults();

        if (first > 0) {
            query.setFirstResult(first);
        }

        if (max > 0) {
            query.setMaxResults(max);
        }

        List<GlycanSequence> ret = (List<GlycanSequence>) query.list();

        log.debug("query executed ok, results count=" + ret.size());
        return ret;
    } catch (HibernateException e) {
        log.warn("Caught " + e.getClass().getName() + " performing query:", e);

        return Collections.emptyList();
    } finally {
        if (scroll != null)
            scroll.close();
    }
}

From source file:org.gbif.portal.dao.DAOUtils.java

License:Open Source License

/**
 * Process results, scrolling through each record in turn only loading that specific record.
 * //  www . ja  v a2  s .  c o m
 * @param resultsOutputter
 * @param session
 * @param query
 * @param associationTraverser
 * @param batchSize
 * @throws IOException
 */
public static void scrollResults(final ResultsOutputter resultsOutputter, Session session, Query query,
        AssociationTraverser associationTraverser, int batchSize) throws IOException {
    query.setReadOnly(true);
    query.setFetchSize(batchSize);

    //Using scrollable results to prevent initiation of all model objects
    ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);

    //go to beginning of resultset
    boolean isNotEmpty = sr.first();
    if (!isNotEmpty) {
        //this is necessary due to a bug with ScrollableResults
        //allowing continuous scrolling over an empty resultset. genius.
        return;
    }

    //iterate through the results
    processScrollableResults(resultsOutputter, session, sr, associationTraverser, batchSize);

    //check for a chain
    if (resultsOutputter instanceof ChainableResultsOutputter) {
        ChainableResultsOutputter cro = (ChainableResultsOutputter) resultsOutputter;
        ResultsOutputter nextResultsOutputter = cro.getNextResultsOutputter();
        while (nextResultsOutputter != null && !cro.isChainInOnePass()) {
            //back to the start
            sr.first();
            processScrollableResults(nextResultsOutputter, session, sr, associationTraverser, batchSize);
            if (resultsOutputter instanceof ChainableResultsOutputter) {
                cro = (ChainableResultsOutputter) resultsOutputter;
                if (!cro.isChainInOnePass())
                    nextResultsOutputter = cro.getNextResultsOutputter();
                else
                    nextResultsOutputter = null;
            } else {
                nextResultsOutputter = null;
            }
        }
    }
    if (associationTraverser != null)
        associationTraverser.reset();
    //close the results set
    sr.close();
}

From source file:org.inbio.neoportal.index.Importer.java

License:Open Source License

/**
 * /*from   w  w  w .  j a  va2s  . c om*/
 */
@Transactional
public void indexOccurrences() {

    Session session = sessionFactory.getCurrentSession();
    //      FullTextSession fullTextSession = Search.getFullTextSession(session);

    // config session for bash job
    session.setFlushMode(FlushMode.MANUAL);
    //      fullTextSession.setFlushMode(FlushMode.MANUAL);

    logger.log(org.apache.log4j.Level.DEBUG, "Starting importOccurrences process");

    //get current date for dateLastModified field
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date date = new Date();
    String dateLastModified = dateFormat.format(date);

    DateFormat sourceDateFormat = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);

    int firstResult = 0;
    int setCounter = 0; //every 100 (the jdbc batch size) call flush 

    DataProvider dp = dataProviderDAO.findAll().get(0);

    logger.log(org.apache.log4j.Level.DEBUG, "importOccurrences Begin Transaction");

    ScrollableResults scroll = session.createCriteria(ImportDwc.class).setFetchSize(BATCH_SIZE)
            .setCacheMode(CacheMode.IGNORE).setReadOnly(true).scroll(ScrollMode.FORWARD_ONLY);

    boolean update;

    int batch = 0;
    int rowsCounter = 0;

    while (scroll.next()) {
        batch++;
        rowsCounter++;

        ImportDwc importDwc = (ImportDwc) scroll.get(0);
        logger.trace("ImportDwc after scroll.get");

        try {

            //avoid repeated occurrenceId
            OccurrenceDwc occurrence = occurrenceDAO
                    .findByCatalogNumberHql(importDwc.getCatalogNumber().replace("\"", ""));
            logger.trace("OccurrenceDwc after findByCatalogNumber "
                    + importDwc.getCatalogNumber().replace("\"", ""));

            if (occurrence != null) {

                update = true;
                //                    continue;
            } else {
                update = false;
                occurrence = new OccurrenceDwc();
            }

            Taxon taxon = null;
            //check if taxonId is empty (unidentify specimens)
            if (importDwc.getTaxonId().isEmpty()) {
                taxon = null;
            } else {
                // check if is the same taxon already associated with the occurrence
                if (update && occurrence.getTaxonId().equals(importDwc.getTaxonId().replace("\"", ""))) {
                    taxon = occurrence.getTaxon();
                    logger.trace("Occurrence update with same taxon");
                } else {

                    // find taxon entity
                    // taxon = taxonNewDAO.findById(new BigDecimal(importDwc.getTaxonId().replace("\"", "")));
                    List<Taxon> taxonList = taxonDAO.findByDefaultName(importDwc.getScientificName());
                    logger.trace("Taxon after findByDefaultName");
                    if (taxonList.size() == 1)
                        taxon = taxonList.get(0);
                    else if (taxonList.size() > 1) {
                        for (Taxon taxon2 : taxonList) {
                            if (taxon2.getKingdom().equals(importDwc.getKingdom())) {
                                taxon = taxon2;
                                break;
                            }
                        }
                    }
                }
            }

            // TODO: fix, use specimenId instead
            occurrence.setOccurrenceId(importDwc.getCatalogNumber().replace("\"", ""));

            occurrence.setDataProvider(dp);
            occurrence.setTaxon(taxon);

            //find or create location
            Location location = locationDAO.findById(new BigDecimal(importDwc.getLocationId()));
            logger.trace("Location after findById");

            if (location == null) {
                location = new Location(new BigDecimal(importDwc.getLocationId()));
                location.setHigherGeographyId(importDwc.getHigherGeographyId());
                location.setHigherGeography(importDwc.getHigherGeography());
                location.setContinent(importDwc.getContinent());
                location.setWaterBody(importDwc.getWaterBody());
                location.setIslandGroup(importDwc.getIslandGroup());
                location.setIsland(importDwc.getIsland());
                location.setCountry(importDwc.getCountry());
                location.setCountryCode(importDwc.getCountryCode());
                location.setStateProvince(importDwc.getStateProvince());
                location.setCounty(importDwc.getCounty());
                location.setMunicipality(importDwc.getMunicipality());
                location.setLocality(importDwc.getLocality());
                location.setVerbatimLocality(importDwc.getVerbatimLocality());
                location.setVerbatimElevation(importDwc.getVerbatimElevation());
                location.setMinimumElevationInMeters(importDwc.getMinimumElevationInMeters());
                location.setMaximumElevationInMeters(importDwc.getMaximumElevationInMeters());
                location.setVerbatimDepth(importDwc.getVerbatimDepth());
                location.setMinimumDepthInMeters(importDwc.getMinimumDepthInMeters());
                location.setMaximumDepthInMeters(importDwc.getMaximumDepthInMeters());
                location.setMinimumDistanceAboveSurfaceInMeters(
                        importDwc.getMinimumDistanceAboveSurfaceInMeters());
                location.setMaximumDistanceAboveSurfaceInMeters(
                        importDwc.getMaximumDistanceAboveSurfaceInMeters());
                location.setLocationAccordingTo(importDwc.getLocationAccordingTo());
                location.setLocationRemarks(importDwc.getLocationRemarks());
                location.setVerbatimCoordinates(importDwc.getVerbatimCoordinates());
                location.setVerbatimLatitude(importDwc.getVerbatimLatitude());
                location.setVerbatimLongitude(importDwc.getVerbatimLongitude());
                location.setVerbatimCoordinateSystem(importDwc.getVerbatimCoordinateSystem());
                location.setVerbatimSRS(importDwc.getVerbatimSRS());
                if (!importDwc.getDecimalLatitude().isEmpty())
                    location.setDecimalLatitude(Double.valueOf(importDwc.getDecimalLatitude()));
                if (!importDwc.getDecimalLongitude().isEmpty())
                    location.setDecimalLongitude(Double.valueOf(importDwc.getDecimalLongitude()));
                location.setGeodeticDatum(importDwc.getGeodeticDatum());
                location.setCoordinateUncertaintyInMeters(importDwc.getCoordinateUncertaintyInMeters());
                location.setCoordinatePrecision(importDwc.getCoordinatePrecision());
                location.setPointRadiusSpatialFit(importDwc.getPointRadiusSpatialFit());
                location.setFootprintWKT(importDwc.getFootprintWKT());
                location.setFootprintSRS(importDwc.getFootprintSRS());
                location.setFootprintSpatialFit(importDwc.getFootprintSpatialFit());
                location.setGeoreferencedBy(importDwc.getGeoreferencedBy());
                location.setGeoreferencedDate(importDwc.getGeoreferencedDate());
                location.setGeoreferenceProtocol(importDwc.getGeoreferenceProtocol());
                location.setGeoreferenceSources(importDwc.getGeoreferenceSources());
                location.setGeoreferenceVerificationStatus(importDwc.getGeoreferenceVerificationStatus());
                location.setGeoreferenceRemarks(importDwc.getGeoreferenceRemarks());

                locationDAO.create(location);
                // increment batch because location should be inserted
                batch++;
            }
            occurrence.setLocation(location);

            occurrence.setType(importDwc.getType());
            occurrence.setModified(importDwc.getModified());
            occurrence.setLanguage(importDwc.getLanguage());
            occurrence.setRights(importDwc.getRights());
            occurrence.setRightsHolder(importDwc.getRightsHolder());
            occurrence.setAccessRights(importDwc.getAccessRights());
            occurrence.setBibliographicCitation(importDwc.getBibliographicCitation());
            occurrence.setReferences(importDwc.getReferences());
            occurrence.setInstitutionId(importDwc.getInstitutionId());
            occurrence.setCollectionId(importDwc.getCollectionId());
            occurrence.setDatasetId(importDwc.getDatasetId());
            occurrence.setInstitutionCode(importDwc.getInstitutionCode());
            occurrence.setCollectionCode(importDwc.getCollectionCode());
            occurrence.setDatasetName(importDwc.getDatasetName());
            occurrence.setOwnerInstitutionCode(importDwc.getOwnerInstitutionCode());
            occurrence.setBasisOfRecord(importDwc.getBasisOfRecord());
            occurrence.setInformationWithheld(importDwc.getInformationWithheld());
            occurrence.setDataGeneralizations(importDwc.getDataGeneralizations());
            occurrence.setDynamicProperties(importDwc.getDynamicProperties());

            occurrence.setOccurrenceId(importDwc.getOccurrenceId().toString());
            occurrence.setCatalogNumber(importDwc.getCatalogNumber());
            occurrence.setOccurrenceRemarks(importDwc.getOccurrenceRemarks());
            occurrence.setRecordNumber(importDwc.getRecordNumber());
            occurrence.setRecordedBy(importDwc.getRecordedBy());
            occurrence.setIndividualId(importDwc.getIndividualId());
            occurrence.setIndividualCount(importDwc.getIndividualCount());
            occurrence.setSex(importDwc.getSex());
            occurrence.setLifeStage(importDwc.getLifeStage());
            occurrence.setReproductiveCondition(importDwc.getReproductiveCondition());
            occurrence.setBehavior(importDwc.getBehavior());
            occurrence.setEstablishmentMeans(importDwc.getEstablishmentMeans());
            occurrence.setOccurrenceStatus(importDwc.getOccurrenceStatus());
            occurrence.setPreparations(importDwc.getPreparations());
            occurrence.setDisposition(importDwc.getDisposition());
            occurrence.setOtherCatalogNumbers(importDwc.getOtherCatalogNumbers());
            occurrence.setPreviousIdentifications(importDwc.getPreviousIdentifications());
            occurrence.setAssociatedMedia(importDwc.getAssociatedMedia());
            occurrence.setAssociatedReferences(importDwc.getAssociatedReferences());
            occurrence.setAssociatedOccurrences(importDwc.getAssociatedOccurrences());
            occurrence.setAssociatedSequences(importDwc.getAssociatedSequences());
            occurrence.setAssociatedTaxa(importDwc.getAssociatedTaxa());

            occurrence.setEventId(importDwc.getEventId());
            occurrence.setSamplingProtocol(importDwc.getSamplingProtocol());
            occurrence.setSamplingEffort(importDwc.getSamplingEffort());
            occurrence.setEventDate(importDwc.getEventDate());
            occurrence.setEventTime(importDwc.getEventTime());
            occurrence.setStartDayOfYear(importDwc.getStartDayOfYear());
            occurrence.setEndDayOfYear(importDwc.getEndDayOfYear());
            occurrence.setYear(importDwc.getYear());
            occurrence.setMonth(importDwc.getMonth());
            occurrence.setDay(importDwc.getDay());
            occurrence.setVerbatimEventDate(importDwc.getVerbatimEventDate());
            occurrence.setHabitat(importDwc.getHabitat());
            occurrence.setFieldNotes(importDwc.getFieldNumber());
            occurrence.setFieldNotes(importDwc.getFieldNotes());
            occurrence.setEventRemarks(importDwc.getEventRemarks());

            occurrence.setGeologicalContextId(importDwc.getGeologicalContextId());
            occurrence.setEarliestEonOrLowestEonothem(importDwc.getEarliestEonOrLowestEonothem());
            occurrence.setLatestEonOrHighestEonothem(importDwc.getLatestEonOrHighestEonothem());
            occurrence.setEarliestEraOrLowestErathem(importDwc.getEarliestEraOrLowestErathem());
            occurrence.setLatestEraOrHighestErathem(importDwc.getLatestEraOrHighestErathem());
            occurrence.setEarliestPeriodOrLowestSystem(importDwc.getEarliestPeriodOrLowestSystem());
            occurrence.setLatestPeriodOrHighestSystem(importDwc.getLatestPeriodOrHighestSystem());
            occurrence.setEarliestEpochOrLowestSeries(importDwc.getEarliestEpochOrLowestSeries());
            occurrence.setLatestEpochOrHighestSeries(importDwc.getLatestEpochOrHighestSeries());
            occurrence.setEarliestAgeOrLowestStage(importDwc.getEarliestAgeOrLowestStage());
            occurrence.setLatestAgeOrHighestStage(importDwc.getLatestAgeOrHighestStage());
            occurrence.setLowestBiostratigraphicZone(importDwc.getLowestBiostratigraphicZone());
            occurrence.setHighestBiostratigraphicZone(importDwc.getHighestBiostratigraphicZone());
            occurrence.setLithostratigraphicTerms(importDwc.getLithostratigraphicTerms());
            occurrence.setGroup(importDwc.getGroup());
            occurrence.setFormation(importDwc.getFormation());
            occurrence.setMember(importDwc.getMember());
            occurrence.setBed(importDwc.getBed());

            occurrence.setIdentificationId(importDwc.getIdentificationId());
            occurrence.setIdentifiedBy(importDwc.getIdentifiedBy());
            if (importDwc.getDateIdentified() != null && importDwc.getDateIdentified().length() > 0)
                occurrence.setDateIdentified(sourceDateFormat.parse(importDwc.getDateIdentified()));
            occurrence.setIdentificationReferences(importDwc.getIdentificationReferences());
            occurrence.setIdentificationVerificationStatus(importDwc.getIdentificationVerificationStatus());
            occurrence.setIdentificationRemarks(importDwc.getIdentificationRemarks());
            occurrence.setIdentificationQualifier(importDwc.getIdentificationQualifier());
            occurrence.setTypeStatus(importDwc.getTypeStatus());

            occurrence.setTaxonId(importDwc.getTaxonId());
            occurrence.setScientificNameId(importDwc.getScientificNameId());
            occurrence.setAcceptedNameUsageId(importDwc.getAcceptedNameUsageId());
            occurrence.setParentNameUsageId(importDwc.getParentNameUsageId());
            occurrence.setOriginalNameUsageId(importDwc.getOriginalNameUsageId());
            occurrence.setNameAccordingToId(importDwc.getNameAccordingToId());
            occurrence.setNamePublishedInId(importDwc.getNamePublishedInId());
            occurrence.setTaxonConceptId(importDwc.getTaxonConceptId());
            occurrence.setScientificName(importDwc.getScientificName());
            occurrence.setAcceptedNameUsage(importDwc.getAcceptedNameUsage());
            occurrence.setParentNameUsage(importDwc.getParentNameUsage());
            occurrence.setOriginalNameUsage(importDwc.getOriginalNameUsage());
            occurrence.setNameAccordingTo(importDwc.getNameAccordingTo());
            occurrence.setNamePublishedIn(importDwc.getNamePublishedIn());
            occurrence.setNamePublishedInYear(importDwc.getNamePublishedInYear());
            occurrence.setHigherClassification(importDwc.getHigherClassification());
            occurrence.setKingdom(importDwc.getKingdom());
            occurrence.setPhylum(importDwc.getPhylum());
            occurrence.setClass_(importDwc.getClass_());
            occurrence.setOrder(importDwc.getOrder());
            occurrence.setFamily(importDwc.getFamily());
            occurrence.setGenus(importDwc.getGenus());
            occurrence.setSubgenus(importDwc.getSubgenus());
            occurrence.setSpecificEpithet(importDwc.getSpecificEpithet());
            occurrence.setInfraspecificEpithet(importDwc.getInfraspecificEpithet());
            occurrence.setTaxonRank(importDwc.getTaxonRank());
            occurrence.setVerbatimTaxonRank(importDwc.getVerbatimTaxonRank());
            occurrence.setScientificNameAuthorship(importDwc.getScientificNameAuthorship());
            occurrence.setVernacularName(importDwc.getVernacularName());
            occurrence.setNomenclaturalCode(importDwc.getNomenclaturalCode());
            occurrence.setTaxonomicStatus(importDwc.getTaxonomicStatus());
            occurrence.setNomenclaturalStatus(importDwc.getNomenclaturalStatus());
            occurrence.setTaxonRemarks(importDwc.getTaxonRemarks());

            if (!update)
                occurrenceDAO.create(occurrence);
            else
                occurrenceDAO.update(occurrence);

            // clear objects 
            occurrence.setImages(null);
            occurrence = null;
            taxon = null;
            location = null;

        } catch (NonUniqueResultException ex) {
            logger.warn("NonUniqueResultException occurrenceId " + importDwc.getCatalogNumber());

        } catch (NumberFormatException ex) {
            logger.error("NumberFormatException occurrenceId " + importDwc.getCatalogNumber());
            ex.printStackTrace();

            System.exit(-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //            } // end for, 1000 importDwc rows

        session.evict(importDwc);

        if (batch >= BATCH_SIZE) {
            batch = 0;

            SessionStatistics statistics = session.getStatistics();
            logger.trace("Entities before flush: " + String.valueOf(statistics.getEntityCount()));

            session.flush();

            logger.trace("Entities before clear: " + String.valueOf(statistics.getEntityCount()));

            //               fullTextSession.flushToIndexes();
            session.clear();

            logger.trace("Entities after clear: " + String.valueOf(statistics.getEntityCount()));

            //               System.exit(1);
        }

        if (rowsCounter % maxResults == 0) {
            //              fullTextSession.flushToIndexes();
            logger.info("Occurrences added " + rowsCounter);

            SessionStatistics statistics = session.getStatistics();
            logger.debug("Entities: " + String.valueOf(statistics.getEntityCount()));

            logger.debug("Collections: " + String.valueOf(statistics.getCollectionCount()));

        }

        // ******* for debug only ***********
        //            if(rowsCounter == 1) {
        //               session.getTransaction().rollback();
        //               scroll.close();
        //               System.exit(1);
        //            }

        //            firstResult += maxResults;

        //            occurrencesDwcList =
        //                    importDwcDAO.scrollAll(ImportDwc.class,
        //                        maxResults,
        //                        firstResult);
    } // end while, no more importDwc rows

    scroll.close();
    //        transactionManager.commit(status);
    session.flush();
    session.clear();

    logger.info("Total occurrences processed " + rowsCounter);
    //        session.getTransaction().commit();
    //        session.close();
}

From source file:org.j2free.jpa.Controller.java

License:Apache License

/**
 * It is critical that batchSize matches the hibernate.search.worker.batch_size you set
 *
 * @param <T>/*from  w ww.ja  va  2  s.c  o m*/
 * @param entityClass
 * @param batchSize
 */
public <T> void hibernateSearchIndex(Class<T> entityClass, int batchSize) {
    FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(getSession());
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);

    ScrollableResults results = fullTextSession.createCriteria(entityClass).setFetchSize(batchSize)
            .scroll(ScrollMode.FORWARD_ONLY);

    try {
        int index = 0;
        while (results.next()) {
            index++;
            fullTextSession.index(results.get(0)); //index each element

            //clear every batchSize since the queue is processed
            if (index % batchSize == 0) {
                fullTextSession.flushToIndexes();
                fullTextSession.clear();
            }
        }
    } finally {
        results.close();
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

License:Open Source License

/**
 * /*w ww. jav  a2  s .com*/
 * @throws PersistenceException
 */
public void generateIndex() throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        Long total = (Long) entityManager.createQuery("SELECT count(entryId) FROM Document").getSingleResult();
        logger.info("Total Entities to be indexed : " + total);

        if (total > 0) {
            Transaction tx = fullTextSession.beginTransaction();
            fullTextSession.purgeAll(Document.class);
            tx.commit();
            logger.info("Removed all documents.");
            Integer numberOfElements = 100;

            Integer numberOfElementsBeforeGC = 1000;
            String queryJPA = "FROM Document ORDER BY entryId asc";

            org.hibernate.Query query = session.createQuery(queryJPA);
            tx = fullTextSession.beginTransaction();
            query.setReadOnly(true);
            query.setLockMode("a", LockMode.NONE);
            results = query.scroll(ScrollMode.FORWARD_ONLY);
            Integer resultNumber = 0;
            while (results.next()) {
                Document document = (Document) results.get(0);
                fullTextSession.index(document);
                resultNumber++;

                if (resultNumber % numberOfElements == 0) {
                    logger.info("Initiating Lucene Index Flush... ");
                    fullTextSession.flushToIndexes();
                    fullTextSession.clear();
                    logger.info("Finished the Lucene Index Flush...  Total records on index " + resultNumber
                            + "/" + total);
                }

                if (resultNumber % numberOfElementsBeforeGC == 0) {
                    System.gc();
                    logger.info("Invoked Garbage collector to prevent OutOfMemory Errors");
                }
            }

            fullTextSession.flushToIndexes();
            fullTextSession.clear();
            tx.commit();

            logger.info("Initiating Lucene Index Optimze...");
            SearchFactory searchFactory = fullTextSession.getSearchFactory();
            searchFactory.optimize(Document.class);
            logger.info("Finished Lucene Index Optimze");
        } else {
            logger.info("No Entities found to be indexed.");
        }
        logger.info("Indexing documents terminated without errors.");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        if (fullTextSession != null) {
            fullTextSession.close();
        }
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

License:Open Source License

/**
 * //from   w  w  w.j ava  2  s . c  o m
 * @throws PersistenceException
 */
@Override
public void updateIndex(Date fromDate) throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        Query queryTotal = entityManager
                .createQuery("SELECT count(entryId) FROM Document where lastUpdate>=:lastUpdate");
        queryTotal.setParameter("lastUpdate", fromDate);
        Long total = (Long) queryTotal.getSingleResult();
        logger.info("Total Entities to be updated : " + total);

        if (total > 0) {
            Integer numberOfElements = 50;
            Integer numberOfElementsBeforeGC = 1000;
            org.hibernate.Query query = session.createQuery("FROM Document where lastUpdate>=:lastUpdate");
            query.setParameter("lastUpdate", fromDate);

            Transaction tx = fullTextSession.beginTransaction();
            query.setReadOnly(true);
            query.setLockMode("a", LockMode.NONE);
            results = query.scroll(ScrollMode.FORWARD_ONLY);
            Integer resultNumber = 0;
            while (results.next()) {
                Document document = (Document) results.get(0);
                fullTextSession.delete(document);
                fullTextSession.index(document);
                resultNumber++;

                if (resultNumber % numberOfElements == 0) {
                    flushingFullTextSession(total, resultNumber, fullTextSession);
                }

                if (resultNumber % numberOfElementsBeforeGC == 0) {
                    System.gc();
                    logger.info("Invoked Garbage collector to prevent OutOfMemory Errors");
                }
            }

            flushingFullTextSession(total, resultNumber, fullTextSession);

            /*            logger.info("Initiating Lucene Index Optimze...");
                          SearchFactory searchFactory = fullTextSession.getSearchFactory();
                          searchFactory.optimize(Document.class);
            */
            logger.info("Finished Lucene Index Optimze");

            tx.commit();
        } else {
            logger.info("No Entities found to be indexed.");
        }
        logger.info("Indexing documents terminated without errors.");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        /*if (fullTextSession.isOpen()) {
           fullTextSession.close();
        }*/
        if (session.isOpen()) {
            session.close();
        }
    }
}

From source file:org.medici.bia.dao.JpaDao.java

License:Open Source License

/**
 * /*  w w w  .j av a  2s  . c o m*/
 */
public void optimizeIndex() throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        logger.info("Initiating Lucene Index Optimze...");
        SearchFactory searchFactory = fullTextSession.getSearchFactory();
        searchFactory.optimize(entityClass);
        logger.info("Finished Lucene Index Optimze");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        if (fullTextSession.isOpen()) {
            fullTextSession.close();
        }
        if (session.isOpen()) {
            session.close();
        }
    }
}

From source file:org.medici.bia.dao.JpaDao.java

License:Open Source License

/**
 * //  w w  w . ja v a 2  s  .c  o m
 * @param fromDate
 * @throws PersistenceException
 */
public void updateIndex(Date fromDate) throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        Query queryTotal = entityManager
                .createQuery("SELECT count(*) FROM " + entityClass + " where lastUpdate>=:lastUpdate");
        queryTotal.setParameter("lastUpdate", fromDate);
        Long total = (Long) queryTotal.getSingleResult();
        logger.info("Total Entities to be updated : " + total);

        if (total > 0) {
            Integer numberOfElements = 50;
            Integer numberOfElementsBeforeGC = 1000;
            org.hibernate.Query query = session
                    .createQuery("FROM " + entityClass + "  where lastUpdate>=:lastUpdate");
            query.setParameter("lastUpdate", fromDate);

            Transaction tx = fullTextSession.beginTransaction();
            query.setReadOnly(true);
            query.setLockMode("a", LockMode.NONE);
            results = query.scroll(ScrollMode.FORWARD_ONLY);
            Integer resultNumber = 0;
            while (results.next()) {
                Object entityClass = (Object) results.get(0);
                fullTextSession.delete(entityClass);
                fullTextSession.index(entityClass);
                resultNumber++;

                if (resultNumber % numberOfElements == 0) {
                    flushingFullTextSession(total, resultNumber, fullTextSession);
                }

                if (resultNumber % numberOfElementsBeforeGC == 0) {
                    System.gc();
                    logger.info("Invoked Garbage collector to prevent OutOfMemory Errors");
                }
            }

            flushingFullTextSession(total, resultNumber, fullTextSession);
            tx.commit();

            logger.info("Initiating Lucene Index Optimze...");
            SearchFactory searchFactory = fullTextSession.getSearchFactory();
            searchFactory.optimize(entityClass);
            logger.info("Finished Lucene Index Optimze");
        } else {
            logger.info("No Entities found to be indexed.");
        }
        logger.info("Indexing documents terminated without errors.");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        if (fullTextSession.isOpen()) {
            fullTextSession.close();
        }
        if (session.isOpen()) {
            session.close();
        }
    }
}

From source file:org.n52.sos.ds.hibernate.dao.i18n.AbstractHibernateI18NDAO.java

License:Open Source License

protected void deleteOldValues(String id, Session session) {
    Criteria criteria = session.createCriteria(getHibernateEntityClass());
    criteria.createCriteria(AbstractHibernateI18NMetadata.OBJECT_ID)
            .add(Restrictions.eq(AbstractIdentifierNameDescriptionEntity.IDENTIFIER, id));
    ScrollableResults scroll = null;
    try {//from w  w  w .  j  av a2s  .co  m
        scroll = criteria.scroll();
        while (scroll.next()) {
            @SuppressWarnings("unchecked")
            H h18n = (H) scroll.get()[0];
            session.delete(h18n);
        }
    } finally {
        if (scroll != null) {
            scroll.close();
        }
    }
    session.flush();
}