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.openmrs.module.auditlog.AuditLogHelper.java

License:Open Source License

/**
 * Checks if the specified type is implicitly audit
 * /* www .  j  a va 2  s . c om*/
 * @should return true if a class is implicitly audited
 * @should return false if a class is not implicitly marked as audited
 * @should return false if a class is already explicitly marked already as audited
 * @should return true if a class is implicitly audited and strategy is all except
 * @should return false if a class is not implicitly audited and strategy is all except
 * @should return false if a class is already explicitly audited and strategy is all except
 */
public boolean isImplicitlyAudited(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 (implicitlyAuditedTypeCache == null) {
        SessionFactory sf = DAOUtils.getSessionFactory();
        FlushMode originalFlushMode = sf.getCurrentSession().getFlushMode();
        sf.getCurrentSession().setFlushMode(FlushMode.MANUAL);
        try {
            return isImplicitlyAuditedInternal(clazz);
        } finally {
            //reset
            sf.getCurrentSession().setFlushMode(originalFlushMode);
        }
    }

    return isImplicitlyAuditedInternal(clazz);
}

From source file:org.openmrs.module.conceptpubsub.api.db.hibernate.interceptor.LocalMappingHibernateInterceptor.java

License:Open Source License

/**
 * @see org.hibernate.EmptyInterceptor#onDelete(java.lang.Object, java.io.Serializable,
 *      java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
 *///from w w w  . j  a  v a 2s. c o m
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if (!(entity instanceof Concept)) {
        return;
    }

    Concept concept = (Concept) entity;

    ConceptPubSubService service = Context.getService(ConceptPubSubService.class);

    FlushMode flushMode = setFlushMode(FlushMode.MANUAL);
    if (service.isLocalSourceConfigured()) {
        service.markLocalMappingRetiredInConcept(concept);
    }
    setFlushMode(flushMode);

}

From source file:org.openmrs.module.conceptpubsub.api.db.hibernate.interceptor.LocalMappingHibernateInterceptor.java

License:Open Source License

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
        String[] propertyNames, Type[] types) {
    if (!(entity instanceof Concept)) {
        return false;
    }//from   w w  w .  ja v  a 2  s  .co m

    Concept concept = (Concept) entity;

    ConceptPubSubService service = Context.getService(ConceptPubSubService.class);

    FlushMode flushMode = setFlushMode(FlushMode.MANUAL);
    if (service.isLocalSourceConfigured()) {
        if (concept.isRetired()) {
            service.markLocalMappingRetiredInConcept(concept);
        } else {
            service.markLocalMappingUnretiredInConcept(concept);
        }
    }
    setFlushMode(flushMode);

    return false;
}

From source file:org.openmrs.module.distrotools.metadata.handler.impl.RoleDeployHandlerTest.java

License:Open Source License

/**
 * We previously encountered a problem where the session couldn't be flushed at certain stages during installation
 * and re-installation of various roles and privileges. It seems like these objects can be cached via the UUID, and
 * once we stopped needlessly overwriting UUIDs the problem was fixed.
 *///  ww w .  j  a  v a 2s .  co m
@Test
public void integration_shouldWorkWithoutFlushes() {
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);

    distroToolsService.installObject(privilege("Privilege1", "Testing"));
    distroToolsService.installObject(role("Role1", "Testing", null, idSet("Privilege1")));
    distroToolsService.installObject(role("Role2", "Testing", idSet("Role1"), null));

    distroToolsService.installObject(privilege("Privilege1", "Testing"));

    distroToolsService.installObject(role("Role1", "Testing", null, idSet("Privilege1")));

    Context.flushSession();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.AUTO);
}

From source file:org.openmrs.module.metadatadeploy.handler.impl.RoleDeployHandlerTest.java

License:Open Source License

/**
 * We previously encountered a problem where the session couldn't be flushed at certain stages during installation
 * and re-installation of various roles and privileges. It seems like these objects can be cached via the UUID, and
 * once we stopped needlessly overwriting UUIDs the problem was fixed.
 *///from  w w w  .j a v a 2s.  c o  m
@Test
public void integration_shouldWorkWithoutFlushes() {
    getCurrentSession().setFlushMode(FlushMode.MANUAL);

    deployService.installObject(privilege("Privilege1", "Testing"));
    deployService.installObject(role("Role1", "Testing", null, idSet("Privilege1")));
    deployService.installObject(role("Role2", "Testing", idSet("Role1"), null));

    deployService.installObject(privilege("Privilege1", "Testing"));

    deployService.installObject(role("Role1", "Testing", null, idSet("Privilege1")));

    Context.flushSession();
    getCurrentSession().setFlushMode(FlushMode.AUTO);
}

From source file:org.openmrs.module.metadatamapping.api.db.hibernate.interceptor.LocalMappingHibernateInterceptor.java

License:Open Source License

/**
 * @see org.hibernate.EmptyInterceptor#onDelete(java.lang.Object, java.io.Serializable,
 *      java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
 *///  w ww .  ja  v  a  2 s  . com
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if (!(entity instanceof Concept)) {
        return;
    }

    Concept concept = (Concept) entity;

    MetadataMappingService service = Context.getService(MetadataMappingService.class);

    FlushMode flushMode = setFlushMode(FlushMode.MANUAL);
    if (service.isLocalConceptSourceConfigured()) {
        service.markLocalMappingRetiredInConcept(concept);
    }
    setFlushMode(flushMode);

}

From source file:org.openmrs.module.metadatamapping.api.db.hibernate.interceptor.LocalMappingHibernateInterceptor.java

License:Open Source License

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
        String[] propertyNames, Type[] types) {
    if (!(entity instanceof Concept)) {
        return false;
    }/*from ww  w . j av  a 2  s.c  o  m*/

    Concept concept = (Concept) entity;

    MetadataMappingService service = Context.getService(MetadataMappingService.class);

    FlushMode flushMode = setFlushMode(FlushMode.MANUAL);
    if (service.isLocalConceptSourceConfigured()) {
        if (concept.isRetired()) {
            service.markLocalMappingRetiredInConcept(concept);
        } else {
            service.markLocalMappingUnretiredInConcept(concept);
        }
    }
    setFlushMode(flushMode);

    return false;
}

From source file:org.openmrs.module.metadatasharing.api.db.hibernate.CustomSessionFlushTask.java

License:Open Source License

/**
 * Executes the task method preventing flushes.
 * //from  w ww .  j  ava2  s .  c o  m
 * @return the result or <code>null</code>
 */
public final T executeInManualFlushMode() {
    Session currentSession = HibernateSessionFactory.getSessionFactory().getCurrentSession();

    FlushMode previousFlushMode = currentSession.getFlushMode();
    currentSession.setFlushMode(FlushMode.MANUAL);

    try {
        return task();
    } finally {
        currentSession.setFlushMode(previousFlushMode);
    }
}

From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncDAO.java

License:Open Source License

/**
 * Sets hibernate flush mode to org.hibernate.FlushMode.MANUAL.
 * //from w w  w.  j  a  v  a  2  s .c om
 * @return true if the flush mode was set already manual
 * @see #isFlushModeManual()
 * @see org.hibernate.FlushMode
 * @see org.openmrs.module.sync.api.db.SyncDAO#setFlushModeManual()
 */
public boolean setFlushModeManual() throws DAOException {
    boolean isManualAlready = isFlushModeManual();

    // don't need to set it if its already manual
    if (!isManualAlready)
        sessionFactory.getCurrentSession().setFlushMode(org.hibernate.FlushMode.MANUAL);

    return isManualAlready;
}

From source file:org.openspaces.persistency.hibernate.iterator.DefaultScrollableDataIterator.java

License:Open Source License

protected ScrollableResults createCursor() {
    session = sessionFactory.openSession();
    session.setFlushMode(FlushMode.MANUAL);
    transaction = session.beginTransaction();
    if (entityName != null) {
        Criteria criteria = session.createCriteria(entityName);
        criteria.setCacheable(false);//from  ww w .  ja va2  s. c o  m
        criteria.setFlushMode(FlushMode.MANUAL);
        criteria.setFetchSize(fetchSize);
        if (perfromOrderById) {
            ClassMetadata metadata = sessionFactory.getClassMetadata(entityName);
            String idPropName = metadata.getIdentifierPropertyName();
            if (idPropName != null) {
                criteria.addOrder(Order.asc(idPropName));
            }
        }
        if (from >= 0) {
            if (from > 0)
                criteria.setFirstResult(from);
            criteria.setMaxResults(size);
        }
        return criteria.scroll(ScrollMode.FORWARD_ONLY);
    } else if (sqlQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromSQLQuery(sqlQuery, session);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else if (hQuery != null) {
        Query query = session.createQuery(hQuery);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        query.setCacheMode(CacheMode.IGNORE);
        query.setCacheable(false);
        query.setReadOnly(true);
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else {
        throw new IllegalStateException("No SQLQuery, entity, or Hibernate Query provided");
    }
}