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:grails.plugins.quartz.listeners.SessionBinderJobListener.java

License:Apache License

public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    try {/*from   w ww. j av  a 2  s  . c om*/
        if (!FlushMode.MANUAL.equals(sessionHolder.getSession().getFlushMode())) {
            sessionHolder.getSession().flush();
        }
    } catch (Exception e) {
        if (LOG.isErrorEnabled())
            LOG.error("Cannot flush Hibernate Sesssion, error will be ignored", e);
    } finally {
        TransactionSynchronizationManager.unbindResource(sessionFactory);
        SessionFactoryUtils.closeSession(sessionHolder.getSession());
        if (LOG.isDebugEnabled())
            LOG.debug("Hibernate Session is unbounded from Job thread and closed");
    }
}

From source file:it.jugpadova.blo.EventBo.java

License:Apache License

public void regenerateLuceneIndexes() {
    Session session = this.eventDao.getHibernateTemplate().getSessionFactory().getCurrentSession();
    FullTextSession fullTextSession = Search.createFullTextSession(session);
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);
    ScrollableResults results = fullTextSession.createCriteria(Event.class).scroll(ScrollMode.FORWARD_ONLY);

    int index = 0;
    while (results.next()) {
        index++;/* ww  w  .j  a v  a  2s  .c o  m*/
        fullTextSession.index(results.get(0)); //index each element

        if (index % 50 == 0) {
            fullTextSession.clear(); //clear every batchSize since the queue is processed

        }
    }
}

From source file:itensil.io.HibernateUtil.java

License:Open Source License

/**
 * Set this session to read-only mode/* ww  w  .  j av a2s. co m*/
 *
 */
public static void readOnlySession() {
    log.debug("Setting Session to read-only.");
    getSession().setFlushMode(FlushMode.MANUAL);
}

From source file:kr.debop4j.data.ogm.dao.HibernateOgmDao.java

License:Apache License

@Override
public void indexAll(Class<?> clazz, int batchSize) {
    if (isDebugEnabled)
        log.debug("[{}]?  ?  ??? ...", clazz);

    clearIndex(clazz);/*from  w w  w  .j a v a2  s .c  o m*/

    if (batchSize < DEFAUALT_BATCH_SIZE)
        batchSize = DEFAUALT_BATCH_SIZE;

    FullTextSession fts = getFullTextSession();

    FlushMode currentFlushMode = fts.getFlushMode();
    CacheMode currentCacheMode = fts.getCacheMode();
    fts.setFlushMode(FlushMode.MANUAL);
    fts.setCacheMode(CacheMode.IGNORE);

    try {
        Transaction tx = fts.beginTransaction();
        ScrollableResults results = fts.createCriteria(clazz).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            fts.index(results.get(0));
            if (++index % batchSize == 0) {
                fts.flushToIndexes();
                fts.clear();
                if (isTraceEnabled)
                    log.trace("?? . index=[{}]", index);
            }
        }
        fts.flushToIndexes();
        tx.commit();

        log.info("[{}]?   [{}]   ??? !!!", clazz,
                index);
    } finally {
        fts.setFlushMode(currentFlushMode);
        fts.setCacheMode(currentCacheMode);
    }
}

From source file:kr.debop4j.search.dao.HibernateSearchDao.java

License:Apache License

@Override
public void indexAll(Class<?> clazz, int batchSize) {
    if (log.isDebugEnabled())
        log.debug("[{}]?  ?  ??? ...", clazz);

    clearIndex(clazz);//from  w ww. j  ava  2  s  .  c  o  m

    if (batchSize < BATCH_SIZE)
        batchSize = BATCH_SIZE;

    final FullTextSession fts = getFullTextSession();

    FlushMode currentFlushMode = fts.getFlushMode();
    CacheMode currentCacheMode = fts.getCacheMode();
    fts.setFlushMode(FlushMode.MANUAL);
    fts.setCacheMode(CacheMode.IGNORE);

    try {
        Transaction tx = fts.beginTransaction();
        ScrollableResults results = fts.createCriteria(clazz).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            fts.index(results.get(0));
            if (index % batchSize == 0) {
                fts.flushToIndexes();
                fts.clear();
            }
        }
        fts.flushToIndexes();
        tx.commit();

        if (log.isDebugEnabled())
            log.debug("[{}]?   [{}]   ??? !!!",
                    clazz, index);
    } finally {
        fts.setFlushMode(currentFlushMode);
        fts.setCacheMode(currentCacheMode);
    }
}

From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java

License:Open Source License

void createSession(SessionFactory factory, DatasourceConnection dc) {
    Session session;/*  w ww.j  av  a 2  s .  c  o  m*/
    _sessions.put(KeyImpl.init(dc.getDatasource().getName()),
            session = factory.openSession(dc.getConnection()));
    session.setFlushMode(FlushMode.MANUAL);
}

From source file:net.databinder.hib.conv.DataConversationRequestCycle.java

License:Open Source License

/**
 * Called by DataStaticService when a session is needed and does not already exist. 
 * Determines current page and retrieves its associated conversation session if 
 * appropriate. Does nothing if current page is not yet available.
 * @param key factory key object, or null for the default factory
 *//*  w  ww.  j a v a 2 s  .c  o  m*/
public void dataSessionRequested(Object key) {
    Page page = getResponsePage();
    if (page == null)
        page = getRequest().getPage();

    if (page == null) {
        Class pageClass = getResponsePageClass();
        if (pageClass != null) {
            openHibernateSession(key);
            // set to manual if we are going to a conv. page
            if (IConversationPage.class.isAssignableFrom(pageClass))
                Databinder.getHibernateSession(key).setFlushMode(FlushMode.MANUAL);
        }
        return;
    }

    // if continuing a conversation page
    if (page instanceof IConversationPage) {
        // look for existing session
        IConversationPage convPage = (IConversationPage) page;
        org.hibernate.classic.Session sess = convPage.getConversationSession(key);

        // if usable session exists, try to open txn, bind, and return
        if (sess != null && sess.isOpen()) {
            try {
                sess.beginTransaction();
                ManagedSessionContext.bind(sess);
                keys.add(key);
                return;
            } catch (HibernateException e) {
                log.warn("Existing session exception on beginTransation, opening new", e);
            }
        }
        // else start new one and set in page
        sess = openHibernateSession(key);
        sess.setFlushMode(FlushMode.MANUAL);
        ((IConversationPage) page).setConversationSession(key, sess);
        return;
    }
    // start new standard session
    openHibernateSession(key);
}

From source file:net.jforum.actions.LuceneAdminActions.java

License:Open Source License

public void rebuildIndex() {

    Runnable indexingJob = new Runnable() {
        public void run() {
            Session session = null;/*w  ww .  j  ava  2s. co m*/

            try {
                session = sessionFactory.openSession();

                FullTextSession fullTextSession = Search.createFullTextSession(session);
                fullTextSession.setFlushMode(FlushMode.MANUAL);
                fullTextSession.setCacheMode(CacheMode.IGNORE);

                session.beginTransaction();

                int index = 0;
                int batchSize = config.getInt(ConfigKeys.LUCENE_BATCH_SIZE);

                ScrollableResults results = fullTextSession.createCriteria(Post.class).createAlias("topic", "t")
                        .scroll(ScrollMode.FORWARD_ONLY);

                while (results.next() && "1".equals(config.getValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING))) {
                    index++;

                    fullTextSession.index(results.get(0));

                    if (index % batchSize == 0) {
                        session.clear();
                    }
                }

                session.getTransaction().commit();
            } catch (Exception e) {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
            } finally {
                if (session.isOpen() && session.isConnected()) {
                    session.close();
                }
            }
        }
    };

    this.config.addProperty(ConfigKeys.LUCENE_CURRENTLY_INDEXING, "1");

    Thread thread = new Thread(indexingJob);
    thread.start();

    this.viewService.redirectToAction(Actions.LIST);
}

From source file:net.jforum.controllers.LuceneAdminController.java

License:Open Source License

public void rebuildIndex() {

    Runnable indexingJob = new Runnable() {
        public void run() {
            Session session = null;/*from   ww w . j  a v a2  s.c o m*/

            try {
                session = sessionFactory.openSession();

                FullTextSession fullTextSession = Search.createFullTextSession(session);
                fullTextSession.setFlushMode(FlushMode.MANUAL);
                fullTextSession.setCacheMode(CacheMode.IGNORE);

                session.beginTransaction();

                int index = 0;
                int batchSize = config.getInt(ConfigKeys.LUCENE_BATCH_SIZE);

                ScrollableResults results = fullTextSession.createCriteria(Post.class).createAlias("topic", "t")
                        .scroll(ScrollMode.FORWARD_ONLY);

                while (results.next() && "1".equals(config.getValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING))) {
                    index++;

                    fullTextSession.index(results.get(0));

                    if (index % batchSize == 0) {
                        session.clear();
                    }
                }

                session.getTransaction().commit();
            } catch (Exception e) {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
            } finally {
                if (session.isOpen() && session.isConnected()) {
                    session.close();
                }
            }
        }
    };

    this.config.addProperty(ConfigKeys.LUCENE_CURRENTLY_INDEXING, "1");

    Thread thread = new Thread(indexingJob);
    thread.start();

    this.result.redirectTo(this).list();
}

From source file:nl.strohalm.cyclos.struts.CyclosRequestProcessor.java

License:Open Source License

private void openReadOnlyConnection(final HttpServletRequest request) {
    if (noTransaction(request)) {
        return;/*from   w  w w  .  j  ava2 s. c  om*/
    }
    logDebug(request, "Opening read-only transaction for include");

    final Connection connection = (Connection) TransactionSynchronizationManager
            .getResource(connectionProvider);

    final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    final Session session = holder.getSession();
    session.setFlushMode(FlushMode.MANUAL);
    session.setDefaultReadOnly(true);
    session.reconnect(connection);

    TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);
}