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.mifos.framework.persistence.LegacyGenericDao.java

License:Open Source License

public Object execUniqueResultNamedQueryWithoutFlush(final String queryName,
        final Map<String, ?> queryParameters) throws PersistenceException {
    try {/*from  w  w w  . j  a v a  2 s .com*/
        Session sess = getSession();
        sess.setFlushMode(FlushMode.MANUAL);
        Query query = getSession().getNamedQuery(queryName);
        logger.debug("The query object for the query with the name  " + queryName + " has been obtained");
        query.setProperties(queryParameters);
        Object returnObject = query.uniqueResult();
        sess.setFlushMode(FlushMode.AUTO);
        return returnObject;
    } catch (GenericJDBCException gje) {
        throw new ConnectionNotFoundException(gje);
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
}

From source file:org.obiba.onyx.engine.variable.export.OnyxDataExport.java

License:Open Source License

@Transactional(rollbackFor = Exception.class)
public void exportInterviews() throws Exception {

    FlushMode originalExportMode = sessionFactory.getCurrentSession().getFlushMode();
    // Change the flushMode. We'll flush the session manually: see ExportListener below.
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    try {//from   ww  w  .j  a v a 2s.  co  m
        log.info("Starting export to configured destinations.");
        internalExport();
        log.info("Export successfully completed.");
    } finally {
        // Reset the flushMode
        sessionFactory.getCurrentSession().setFlushMode(originalExportMode);
    }

}

From source file:org.openeos.services.ui.vaadin.internal.OpenSessionInViewListener.java

License:Apache License

@Override
public void onTransactionStart(IUnoVaadinApplication application) {
    if (sessionFactory != null) {
        if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
            LOG.debug("Participating in existing open session.");
        } else {//w  w w.j a v  a2s.  co  m
            LOG.debug("Opening session in View...");
            Session session = SessionFactoryUtils.openSession(sessionFactory);
            session.setFlushMode(FlushMode.MANUAL);
            TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
            tlSession.set(session);
        }
    }
}

From source file:org.openmrs.api.db.hibernate.HibernateAdministrationDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.AdministrationDAO#validate(java.lang.Object, Errors)
 */// w ww.j a  va2 s .c o m
@Override
public void validate(Object object, Errors errors) throws DAOException {
    FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    try {
        for (Validator validator : getValidators(object)) {
            validator.validate(object, errors);
        }
    } finally {
        sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
    }
}

From source file:org.openmrs.api.db.hibernate.HibernateConceptDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.ConceptDAO#getDefaultConceptMapType()
 */// w  w  w  . j  a  v  a2 s.c  o  m
@Override
public ConceptMapType getDefaultConceptMapType() throws DAOException {
    FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    try {
        //Defaults to same-as if the gp is not set.
        String defaultConceptMapType = Context.getAdministrationService()
                .getGlobalProperty(OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE);
        if (defaultConceptMapType == null) {
            throw new DAOException("The default concept map type is not set. You need to set the '"
                    + OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE + "' global property.");
        }

        ConceptMapType conceptMapType = getConceptMapTypeByName(defaultConceptMapType);
        if (conceptMapType == null) {
            throw new DAOException("The default concept map type (name: " + defaultConceptMapType
                    + ") does not exist! You need to set the '" + OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE
                    + "' global property.");
        }
        return conceptMapType;
    } finally {
        sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
    }
}

From source file:org.openmrs.api.db.hibernate.HibernateContextDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.ContextDAO#getUserByUuid(java.lang.String)
 *///from  w  ww  .ja v  a 2s .c o  m
public User getUserByUuid(String uuid) {

    // don't flush here in case we're in the AuditableInterceptor.  Will cause a StackOverflowEx otherwise
    FlushMode flushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);

    User u = (User) sessionFactory.getCurrentSession().createQuery("from User u where u.uuid = :uuid")
            .setString("uuid", uuid).uniqueResult();

    // reset the flush mode to whatever it was before
    sessionFactory.getCurrentSession().setFlushMode(flushMode);

    return u;
}

From source file:org.openmrs.api.db.hibernate.HibernateContextDAO.java

License:Mozilla Public License

public void openSession() {
    log.debug("HibernateContext: Opening Hibernate Session");
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        if (log.isDebugEnabled()) {
            log.debug("Participating in existing session (" + sessionFactory.hashCode() + ")");
        }//from  w ww  .j a  v  a 2  s.  com
        participate = true;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Registering session with synchronization manager (" + sessionFactory.hashCode() + ")");
        }
        Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.MANUAL);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }
}

From source file:org.openmrs.api.db.hibernate.HibernateContextDAO.java

License:Mozilla Public License

@Override
public void updateSearchIndexForType(Class<?> type) {
    //From http://docs.jboss.org/hibernate/search/3.3/reference/en-US/html/manual-index-changes.html#search-batchindex-flushtoindexes
    FullTextSession session = Search.getFullTextSession(sessionFactory.getCurrentSession());
    session.purgeAll(type);//w  ww.  ja v a  2  s .  co  m

    //Prepare session for batch work
    session.flush();
    session.clear();

    FlushMode flushMode = session.getFlushMode();
    CacheMode cacheMode = session.getCacheMode();
    try {
        session.setFlushMode(FlushMode.MANUAL);
        session.setCacheMode(CacheMode.IGNORE);

        //Scrollable results will avoid loading too many objects in memory
        ScrollableResults results = session.createCriteria(type).setFetchSize(1000)
                .scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            session.index(results.get(0)); //index each element
            if (index % 1000 == 0) {
                session.flushToIndexes(); //apply changes to indexes
                session.clear(); //free memory since the queue is processed
            }
        }
        session.flushToIndexes();
        session.clear();
    } finally {
        session.setFlushMode(flushMode);
        session.setCacheMode(cacheMode);
    }
}

From source file:org.openmrs.api.db.hibernate.HibernateOrderDAO.java

License:Mozilla Public License

@Override
public List<Object[]> getOrderFromDatabase(Order order, boolean isOrderADrugOrder) throws APIException {
    String sql = "SELECT patient_id, care_setting, concept_id FROM orders WHERE order_id = :orderId";

    if (isOrderADrugOrder) {
        sql = " SELECT o.patient_id, o.care_setting, o.concept_id, d.drug_inventory_id "
                + " FROM orders o, drug_order d WHERE o.order_id = d.order_id AND o.order_id = :orderId";
    }// w  w w  .ja  v  a  2 s .  com
    Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);
    query.setParameter("orderId", order.getOrderId());

    //prevent hibernate from flushing before fetching the list
    query.setFlushMode(FlushMode.MANUAL);

    return query.list();
}

From source file:org.openmrs.module.auditlog.AuditLogHelper.java

License:Open Source License

public boolean isAudited(Class<?> clazz) {
    //We need to stop hibernate auto flushing which might happen as we fetch
    //the GP values, Otherwise if a flush happens, then the interceptor
    //logic will be called again which will result in an infinite loop/stack overflow
    if (exceptionsTypeCache == null || auditingStrategyCache == null) {
        SessionFactory sf = DAOUtils.getSessionFactory();
        FlushMode originalFlushMode = sf.getCurrentSession().getFlushMode();
        sf.getCurrentSession().setFlushMode(FlushMode.MANUAL);
        try {/*w ww  . j  a v  a2  s.com*/
            return isAuditedInternal(clazz);
        } finally {
            //reset
            sf.getCurrentSession().setFlushMode(originalFlushMode);
        }
    }

    return isAuditedInternal(clazz);
}