Example usage for org.hibernate FlushMode MANUAL

List of usage examples for org.hibernate FlushMode MANUAL

Introduction

In this page you can find the example usage for org.hibernate FlushMode MANUAL.

Prototype

FlushMode MANUAL

To view the source code for org.hibernate FlushMode MANUAL.

Click Source Link

Document

The Session is only ever flushed when Session#flush is explicitly called by the application.

Usage

From source file:org.unitime.timetable.model.ExactTimeMins.java

License:Open Source License

public static ExactTimeMins findByMinPerMtg(int minPerMtg) {
    return (ExactTimeMins) (new ExactTimeMinsDAO()).getSession().createQuery(
            "select m from ExactTimeMins m where m.minsPerMtgMin<=:minPerMtg and :minPerMtg<=m.minsPerMtgMax")
            .setInteger("minPerMtg", minPerMtg).setCacheable(true).setFlushMode(FlushMode.MANUAL)
            .uniqueResult();/*from  w ww  .  ja v  a 2 s  .  c o m*/
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueBatchSolverValidator.java

License:Apache License

@Override
public void save() throws Exception {
    iProgress.setStatus("Validating solution ...");
    List<Protocol> protocols = new ArrayList<Protocol>();
    protocols.add(Protocol.HTTP);/*  w w w. j  ava2s .  c  o m*/
    protocols.add(Protocol.HTTPS);
    iClient = new Client(protocols);
    iCSV.setHeader(new CSVField[] { new CSVField("PUID"), new CSVField("Name"), new CSVField("Course"),
            new CSVField("CRN"), new CSVField("Code"), new CSVField("Message") });
    org.hibernate.Session hibSession = null;
    Transaction tx = null;
    try {
        hibSession = SessionDAO.getInstance().getSession();
        hibSession.setCacheMode(CacheMode.IGNORE);
        hibSession.setFlushMode(FlushMode.MANUAL);

        tx = hibSession.beginTransaction();

        Session session = Session.getSessionUsingInitiativeYearTerm(iInitiative, iYear, iTerm);
        if (session == null)
            throw new Exception("Session " + iInitiative + " " + iTerm + iYear + " not found!");
        ApplicationProperties.setSessionId(session.getUniqueId());
        iSession = new AcademicSessionInfo(session);

        validate(session, hibSession);

        hibSession.flush();

        tx.commit();
        tx = null;
    } catch (Exception e) {
        iProgress.fatal("Unable to validate, reason: " + e.getMessage(), e);
        sLog.error(e.getMessage(), e);
        if (tx != null)
            tx.rollback();
    } finally {
        if (hibSession != null && hibSession.isOpen())
            hibSession.close();
        try {
            iClient.stop();
        } catch (Exception e) {
            sLog.error(e.getMessage(), e);
        }
    }
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.XEBatchSolverSaver.java

License:Apache License

@Override
public void save() throws Exception {
    iProgress.setStatus("Saving solution ...");
    List<Protocol> protocols = new ArrayList<Protocol>();
    protocols.add(Protocol.HTTP);/*  w  ww .  ja va2s  .c om*/
    protocols.add(Protocol.HTTPS);
    iClient = new Client(protocols);
    iCSV.setHeader(new CSVField[] { new CSVField("PUID"), new CSVField("Name"), new CSVField("Course"),
            new CSVField("CRN"), new CSVField("Request"), new CSVField("Status"), new CSVField("Message"),
            new CSVField("Used Override") });
    org.hibernate.Session hibSession = null;
    Transaction tx = null;
    try {
        hibSession = SessionDAO.getInstance().getSession();
        hibSession.setCacheMode(CacheMode.IGNORE);
        hibSession.setFlushMode(FlushMode.MANUAL);

        tx = hibSession.beginTransaction();

        Session session = Session.getSessionUsingInitiativeYearTerm(iInitiative, iYear, iTerm);
        if (session == null)
            throw new Exception("Session " + iInitiative + " " + iTerm + iYear + " not found!");
        ApplicationProperties.setSessionId(session.getUniqueId());
        iSession = new AcademicSessionInfo(session);

        save(session, hibSession);

        if (!iUpdatedStudents.isEmpty() && CustomStudentEnrollmentHolder.isCanRequestUpdates()) {
            CustomStudentEnrollmentHolder.getProvider().requestUpdate((OnlineSectioningServer) getSolver(),
                    new OnlineSectioningHelper(hibSession, getUser()), iUpdatedStudents);
        }

        hibSession.flush();

        tx.commit();
        tx = null;
    } catch (Exception e) {
        iProgress.fatal("Unable to save , reason: " + e.getMessage(), e);
        sLog.error(e.getMessage(), e);
        if (tx != null)
            tx.rollback();
    } finally {
        if (hibSession != null && hibSession.isOpen())
            hibSession.close();
        try {
            iClient.stop();
        } catch (Exception e) {
            sLog.error(e.getMessage(), e);
        }
    }
}

From source file:org.unitime.timetable.solver.studentsct.StudentSectioningDatabaseLoader.java

License:Open Source License

public void load() {
    iProgress.setStatus("Loading input data ...");
    org.hibernate.Session hibSession = null;
    Transaction tx = null;/*from   w ww .  j  a va 2  s.c o  m*/
    try {
        hibSession = SessionDAO.getInstance().getSession();
        hibSession.setCacheMode(CacheMode.IGNORE);
        hibSession.setFlushMode(FlushMode.MANUAL);

        tx = hibSession.beginTransaction();

        Session session = null;
        if (iSessionId != null) {
            session = SessionDAO.getInstance().get(iSessionId);
            if (session != null) {
                iYear = session.getAcademicYear();
                iTerm = session.getAcademicTerm();
                iInitiative = session.getAcademicInitiative();
                getModel().getProperties().setProperty("Data.Year", iYear);
                getModel().getProperties().setProperty("Data.Term", iTerm);
                getModel().getProperties().setProperty("Data.Initiative", iInitiative);
            }
        } else {
            session = Session.getSessionUsingInitiativeYearTerm(iInitiative, iYear, iTerm);
            if (session != null) {
                iSessionId = session.getUniqueId();
                getModel().getProperties().setProperty("General.SessionId", String.valueOf(iSessionId));
            }
        }

        if (session == null)
            throw new Exception("Session " + iInitiative + " " + iTerm + iYear + " not found!");

        iProgress.info("Loading data for " + iInitiative + " " + iTerm + iYear + "...");

        if (getModel().getDistanceConflict() != null)
            TravelTime.populateTravelTimes(getModel().getDistanceConflict().getDistanceMetric(), iSessionId,
                    hibSession);

        load(session, hibSession);

        tx.commit();
    } catch (Exception e) {
        iProgress.fatal("Unable to load sectioning problem, reason: " + e.getMessage(), e);
        sLog.error(e.getMessage(), e);
        tx.rollback();
    } finally {
        // here we need to close the session since this code may run in a separate thread
        if (hibSession != null && hibSession.isOpen())
            hibSession.close();
    }
}

From source file:org.unitime.timetable.solver.studentsct.StudentSectioningDatabaseSaver.java

License:Open Source License

public void save() {
    iProgress.setStatus("Saving solution ...");
    iTimeStamp = new Date();
    org.hibernate.Session hibSession = null;
    Transaction tx = null;//from   ww  w .ja  v a2s .c  om
    try {
        hibSession = SessionDAO.getInstance().getSession();
        hibSession.setCacheMode(CacheMode.IGNORE);
        hibSession.setFlushMode(FlushMode.MANUAL);

        tx = hibSession.beginTransaction();

        Session session = Session.getSessionUsingInitiativeYearTerm(iInitiative, iYear, iTerm);

        if (session == null)
            throw new Exception("Session " + iInitiative + " " + iTerm + iYear + " not found!");

        save(session, hibSession);

        StudentSectioningQueue.sessionStatusChanged(hibSession, null, session.getUniqueId(), true);

        hibSession.flush();

        tx.commit();
        tx = null;

    } catch (Exception e) {
        iProgress.fatal("Unable to save student schedule, reason: " + e.getMessage(), e);
        sLog.error(e.getMessage(), e);
        if (tx != null)
            tx.rollback();
    } finally {
        // here we need to close the session since this code may run in a separate thread
        if (hibSession != null && hibSession.isOpen())
            hibSession.close();
    }
}

From source file:org.wallride.service.SystemService.java

License:Apache License

@Async
@Transactional(propagation = Propagation.SUPPORTS)
public void reIndex() throws Exception {
    logger.info("Re-Index started");

    FullTextSession fullTextSession = Search.getFullTextSession((entityManager.unwrap(Session.class)));

    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);

    for (Class persistentClass : fullTextSession.getSearchFactory().getIndexedTypes()) {
        Transaction transaction = fullTextSession.beginTransaction();

        // Scrollable results will avoid loading too many objects in memory
        ScrollableResults results = fullTextSession.createCriteria(persistentClass).setFetchSize(BATCH_SIZE)
                .scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;//from w  ww  .  j  a  va 2  s.  co m
            fullTextSession.index(results.get(0)); //index each element
            if (index % BATCH_SIZE == 0) {
                fullTextSession.flushToIndexes(); //apply changes to indexes
                fullTextSession.clear(); //free memory since the queue is processed
            }
        }
        transaction.commit();
    }
    logger.info("Re-Index finished");
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> List<E> findByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setCacheable(query.isCached());

    //Issue #21/*  w ww  .j a v a  2 s  .co m*/
    criteria.setFlushMode(FlushMode.MANUAL);

    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }
    return criteria.list();
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E findFirstByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setMaxResults(1).setCacheable(query.isCached());
    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }//from ww  w  .j av  a2  s.  com

    //Issue #21
    criteria.setFlushMode(FlushMode.MANUAL);

    final List<E> list = criteria.list();
    if (list.size() > 0) {
        return list.get(0);
    }
    return null;
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E findUniqueByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setMaxResults(2).setCacheable(query.isCached());
    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }// w ww .  ja v  a 2  s. c o  m

    //Issue #21
    criteria.setFlushMode(FlushMode.MANUAL);

    final List<E> list = criteria.list();
    if (list.size() == 0) {
        return null;
    } else if (list.size() == 1) {
        return list.get(0);
    } else {
        throw new DataIntegrityViolationException("expecting unique result but got many");
    }
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> List<E> getAll(Class<E> entityType) {
    return DetachedCriteria.forClass(entityType).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setFlushMode(FlushMode.MANUAL).list(); //Issue #21
}