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.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

protected void applyFlushMode(Session session) {
    FlushMode hibernateFlushMode = FlushMode.AUTO;
    switch (flushMode) {
    case AUTO://from  w ww.j ava2  s. c  om
        hibernateFlushMode = FlushMode.AUTO;
        break;
    case MANUAL:
        hibernateFlushMode = FlushMode.MANUAL;
        break;
    case COMMIT:
        hibernateFlushMode = FlushMode.COMMIT;
        break;
    case ALWAYS:
        hibernateFlushMode = FlushMode.ALWAYS;
        break;
    }
    session.setFlushMode(hibernateFlushMode);
}

From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

@Override
public void postHandle(WebRequest request, ModelMap model) throws DataAccessException {
    final boolean isFlowRequest = request.getAttribute(IS_FLOW_REQUEST_ATTRIBUTE,
            WebRequest.SCOPE_REQUEST) != null;
    if (isFlowRequest) {
        return;/*ww  w.  j a v  a2s .co m*/
    }

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(getSessionFactory());
    Session session = sessionHolder != null ? sessionHolder.getSession() : null;
    try {
        super.postHandle(request, model);
        if (session != null && flushMode != AbstractHibernateDatastore.FlushMode.MANUAL
                && !FlushMode.isManualFlushMode(session.getFlushMode())) {
            logger.debug("Eagerly flushing Hibernate session");
            session.flush();
        }
    } finally {
        if (session != null) {
            session.setFlushMode(FlushMode.MANUAL);
        }
    }
}

From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

public void setFlushMode(int flushMode) {
    if (AbstractHibernateDatastore.FlushMode.AUTO.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.AUTO;
    } else if (AbstractHibernateDatastore.FlushMode.MANUAL.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.MANUAL;
    } else if (AbstractHibernateDatastore.FlushMode.COMMIT.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.COMMIT;
    } else if (AbstractHibernateDatastore.FlushMode.ALWAYS.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.ALWAYS;
    }//from   ww  w.  j  ava  2  s . co m
}

From source file:org.grails.webflow.persistence.SessionAwareHibernateFlowExecutionListener.java

License:Apache License

private Session createSession(RequestContext context) {
    Session session;/*from   w  w w.  java 2s  .  c  o m*/
    if (entityInterceptor != null) {
        if (hibernate3Present) {
            try {
                session = (Session) openSessionWithInterceptorMethod.invoke(localSessionFactory,
                        entityInterceptor);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException("Unable to open Hibernate 3 session", ex);
            } catch (InvocationTargetException ex) {
                throw new IllegalStateException("Unable to open Hibernate 3 session", ex);
            }
        } else {
            session = localSessionFactory.withOptions().interceptor(entityInterceptor).openSession();
        }
    } else {
        if (hibernate3Present) {
            try {
                session = (Session) openSessionMethod.invoke(localSessionFactory);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException("Unable to open Hibernate 3 session", ex);
            } catch (InvocationTargetException ex) {
                throw new IllegalStateException("Unable to open Hibernate 3 session", ex);
            }
        } else {
            session = localSessionFactory.openSession();
        }
    }
    session.setFlushMode(FlushMode.MANUAL);
    return session;
}

From source file:org.grouter.common.hibernatesearch.FullIndexHandler.java

License:Apache License

/**
 * Creates index/*from  w w  w  .  j  ava2  s  . c  om*/
 *
 * @param batchSize batch index parameter
 * @param session the Hibernate session
 * @param theIndexClass classes to index
 */
public void doFullIndex(int batchSize, Session session, Class... theIndexClass) {
    FullTextSession fullTextSession = Search.createFullTextSession(session);
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);
    //Scrollable results will avoid loading too many objects in memory
    for (Class theClass : theIndexClass) {
        ScrollableResults results = fullTextSession.createCriteria(theClass).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            fullTextSession.index(results.get(0)); //index each element
            if (index % batchSize == 0) {
                session.clear(); //clear every batchSize since the queue is processed
            }
        }
    }
}

From source file:org.grouter.domain.dao.spring.SystemDAOImpl.java

License:Apache License

public void doFullIndex(int batchSize, Class theIndexClass, Session session)// Class... clazzes)
{
    FullTextSession fullTextSession = Search.createFullTextSession(session);
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);
    //Transaction transaction = fullTextSession.beginTransaction();
    //Scrollable results will avoid loading too many objects in memory
    ScrollableResults results = fullTextSession.createCriteria(theIndexClass).scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
        index++;//w  w w  . j a  v  a2 s.c  om
        fullTextSession.index(results.get(0)); //index each element
        if (index % batchSize == 0) {
            session.clear(); //clear every batchSize since the queue is processed
        }
    }
    //transaction.commit();
}

From source file:org.headsupdev.agile.app.search.Reindex.java

License:Open Source License

public void run() {
    Task reindex = new ReindexTask();
    Manager.getInstance().addTask(reindex);

    try {/* w w  w  .  jav a 2 s.  c o m*/
        for (String className : HibernateUtil.getEntityClassNames()) {
            Session session = HibernateUtil.getCurrentSession();
            FullTextSession fullTextSession = org.hibernate.search.Search
                    .createFullTextSession(((SessionProxy) session).getRealSession());
            Transaction tx = fullTextSession.beginTransaction();

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

            Manager.getLogger(getClass().getName()).debug("  object type " + className);

            //Scrollable results will avoid loading too many objects in memory
            ScrollableResults results = fullTextSession.createCriteria(className).setFetchSize(BATCH_SIZE)
                    .scroll(ScrollMode.FORWARD_ONLY);

            int index = 0;
            while (results.next()) {
                Object o = results.get(0);

                index++;
                if (o.getClass().isAnnotationPresent(Indexed.class)) {
                    if (HeadsUpConfiguration.isDebug()) {
                        System.out.print(".");
                    }
                    fullTextSession.index(o); //index each element
                }
                if (index % BATCH_SIZE == 0) {
                    fullTextSession.flushToIndexes(); //apply changes to indexes
                    fullTextSession.clear(); //clear since the queue is processed
                }
            }
            tx.commit();

            if (HeadsUpConfiguration.isDebug()) {
                System.out.println();
            }
        }
    } catch (Exception e) {
        Manager.getLogger(getClass().getName()).error("Failed to reindex search data", e);
    }

    Manager.getInstance().removeTask(reindex);
}

From source file:org.hyperic.hq.authz.server.session.ResourceDAO.java

License:Open Source License

/**
 * Find a Resource by type Id and instance Id, allowing for the query to
 * return a stale copy of the resource (for efficiency reasons).
 * /*from w  ww . j a v  a  2  s.com*/
 * @param typeId The type Id.
 * @param id The instance Id.
 * @param allowStale <code>true</code> to allow stale copies of an alert
 *        definition in the query results; <code>false</code> to never allow
 *        stale copies, potentially always forcing a sync with the database.
 * @return The Resource.
 */
public Resource findByInstanceId(Integer typeId, Integer id, boolean allowStale) {
    FlushMode oldFlushMode = this.getSession().getFlushMode();

    try {
        if (allowStale) {
            this.getSession().setFlushMode(FlushMode.MANUAL);
        }

        return findByInstanceId(typeId, id);
    } finally {
        this.getSession().setFlushMode(oldFlushMode);
    }
}

From source file:org.hyperic.hq.events.server.session.AlertDefinitionDAO.java

License:Open Source License

/**
 * Find the alert def for a given appdef entity that is the child of the
 * parent alert def passed in, allowing for the query to return a stale copy
 * of the alert definition (for efficiency reasons).
 * /*from w  w w .j  a v  a 2s .  c om*/
 * @param ent
 * @param parentId
 * @param allowStale <code>true</code> to allow stale copies of an alert
 *        definition in the query results; <code>false</code> to never allow
 *        stale copies, potentially always forcing a sync with the database.
 * @return The alert definition or <code>null</code>.
 */
public AlertDefinition findChildAlertDef(Resource res, Integer parentId, boolean allowStale) {
    Session session = this.getSession();
    FlushMode oldFlushMode = session.getFlushMode();

    try {
        if (allowStale) {
            session.setFlushMode(FlushMode.MANUAL);
        }

        return findChildAlertDef(res, parentId);
    } finally {
        session.setFlushMode(oldFlushMode);
    }
}

From source file:org.hyperic.hq.events.server.session.EventLogDAO.java

License:Open Source License

/**
 * Insert the event logs in batch, with batch size specified by the
 * <code>hibernate.jdbc.batch_size</code> configuration property.
 * //  www . j  ava2s  . c o m
 * @param eventLogs The event logs to insert.
 */
void insertLogs(EventLog[] eventLogs) {
    Session session = getSession();

    FlushMode flushMode = session.getFlushMode();
    CacheMode cacheMode = session.getCacheMode();

    try {
        session.setFlushMode(FlushMode.MANUAL);

        // We do not want to update the 2nd level cache with these event
        // logs
        session.setCacheMode(CacheMode.IGNORE);

        for (int i = 0; i < eventLogs.length; i++) {
            create(eventLogs[i]);
        }

        session.flush();
        session.clear();
    } finally {
        session.setFlushMode(flushMode);
        session.setCacheMode(cacheMode);
    }
}