Example usage for org.hibernate FlushMode AUTO

List of usage examples for org.hibernate FlushMode AUTO

Introduction

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

Prototype

FlushMode AUTO

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

Click Source Link

Document

The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

Usage

From source file:org.encuestame.persistence.filter.SessionFilter.java

License:Apache License

protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    Session session = super.getSession(sessionFactory);
    session.setFlushMode(FlushMode.AUTO);
    return session;
}

From source file:org.eurocarbdb.interceptor.PersistenceLifecycleInterceptor.java

License:Open Source License

/**
*   Called before Action or View have been executed. Current 
*   implementation sets the Hibernate Session to be 
*   {@link FlushMode.AUTO} (which it probably is already..).
*///from   w ww. j a v a  2 s. co m
protected void handlePreExecutePhase() {
    //  reset Hibernate FlushMode to AUTO (just in case it 
    //  wasn't already). AUTO means DB operations will be performed
    //  asynchronously, batched together near the end of the transaction
    //  to improve performance & concurrency.
    if (em instanceof HibernateEntityManager) {
        HibernateEntityManager hem = (HibernateEntityManager) em;
        log.trace("resetting Hibernate flush_mode to AUTO");
        hem.getHibernateSession().setFlushMode(FlushMode.AUTO);
    }

    log.info("time elapsed since init: " + elapsed() + "millsecs");
}

From source file:org.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

/**
 * Sets the target object to read-write, allowing Hibernate to dirty check it and auto-flush changes.
 *
 * @see #setObjectToReadyOnly(Object, org.hibernate.SessionFactory)
 *
 * @param target The target object//www  . ja va  2s. c o  m
 * @param sessionFactory The SessionFactory instance
 */
public static void setObjectToReadWrite(final Object target, SessionFactory sessionFactory) {
    Session session = sessionFactory.getCurrentSession();
    if (!canModifyReadWriteState(session, target)) {
        return;
    }

    SessionImplementor sessionImpl = (SessionImplementor) session;
    EntityEntry ee = sessionImpl.getPersistenceContext().getEntry(target);

    if (ee == null || ee.getStatus() != Status.READ_ONLY) {
        return;
    }

    Object actualTarget = target;
    if (target instanceof HibernateProxy) {
        actualTarget = ((HibernateProxy) target).getHibernateLazyInitializer().getImplementation();
    }

    session.setReadOnly(actualTarget, false);
    session.setFlushMode(FlushMode.AUTO);
    incrementVersion(target);
}

From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java

License:Apache License

/**
 * Apply the flush mode that's been specified for this accessor to the given Session.
 *
 * @param session             the current Hibernate Session
 * @param existingTransaction if executing within an existing transaction
 * @return the previous flush mode to restore after the operation, or <code>null</code> if none
 * @see #setFlushMode/* w ww .j ava  2s. c om*/
 * @see org.hibernate.Session#setFlushMode
 */
protected FlushMode applyFlushMode(Session session, boolean existingTransaction) {
    if (isApplyFlushModeOnlyToNonExistingTransactions() && existingTransaction) {
        return null;
    }

    if (getFlushMode() == FLUSH_NEVER) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (!previousFlushMode.lessThan(FlushMode.COMMIT)) {
                session.setFlushMode(FlushMode.MANUAL);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.MANUAL);
        }
    } else if (getFlushMode() == FLUSH_EAGER) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (!previousFlushMode.equals(FlushMode.AUTO)) {
                session.setFlushMode(FlushMode.AUTO);
                return previousFlushMode;
            }
        } else {
            // rely on default FlushMode.AUTO
        }
    } else if (getFlushMode() == FLUSH_COMMIT) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) {
                session.setFlushMode(FlushMode.COMMIT);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.COMMIT);
        }
    } else if (getFlushMode() == FLUSH_ALWAYS) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (!previousFlushMode.equals(FlushMode.ALWAYS)) {
                session.setFlushMode(FlushMode.ALWAYS);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.ALWAYS);
        }
    }
    return null;
}

From source file:org.grails.orm.hibernate.GrailsSessionContext.java

License:Apache License

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *///from  w ww . java  2  s  . co m
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager.getResource(sessionFactory);
    if (value instanceof Session) {
        return (Session) value;
    }

    if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        Session session = sessionHolder.getSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()
                && !sessionHolder.isSynchronizedWithTransaction()) {
            TransactionSynchronizationManager
                    .registerSynchronization(createSpringSessionSynchronization(sessionHolder));
            sessionHolder.setSynchronizedWithTransaction(true);
            // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
            // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
            FlushMode flushMode = HibernateVersionSupport.getFlushMode(session);
            if (flushMode.equals(FlushMode.MANUAL)
                    && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                HibernateVersionSupport.setFlushMode(session, FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }
        return session;
    }

    if (jtaSessionContext != null) {
        Session session = jtaSessionContext.currentSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager
                    .registerSynchronization(createSpringFlushSynchronization(session));
        }
        return session;
    }

    if (allowCreate) {
        // be consistent with older HibernateTemplate behavior
        return createSession(value);
    }

    throw new HibernateException("No Session found for current thread");
}

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

License:Apache License

protected void applyFlushMode(Session session) {
    FlushMode hibernateFlushMode = FlushMode.AUTO;
    switch (getFlushMode()) {
    case GrailsHibernateTemplate.FLUSH_EAGER:
    case GrailsHibernateTemplate.FLUSH_AUTO:
        hibernateFlushMode = FlushMode.AUTO;
        break;/*from ww  w.j  a  va  2 s .c  o m*/
    case GrailsHibernateTemplate.FLUSH_NEVER:
        hibernateFlushMode = FlushMode.MANUAL;
        break;
    case GrailsHibernateTemplate.FLUSH_COMMIT:
        hibernateFlushMode = FlushMode.COMMIT;
        break;
    case GrailsHibernateTemplate.FLUSH_ALWAYS:
        hibernateFlushMode = FlushMode.ALWAYS;
        break;
    }
    session.setFlushMode(hibernateFlushMode);
}

From source file:org.grails.orm.hibernate.support.HibernatePersistenceContextInterceptor.java

License:Apache License

public void setReadWrite() {
    if (getSessionFactory() == null)
        return;
    getSession().setFlushMode(FlushMode.AUTO);
}

From source file:org.grails.orm.hibernate.support.HibernatePersistenceContextInterceptor.java

License:Apache License

public void init() {
    if (incNestingCount() > 1) {
        return;/*w  w  w .jav  a 2  s  .  co m*/
    }
    SessionFactory sf = getSessionFactory();
    if (sf == null) {
        return;
    }
    if (TransactionSynchronizationManager.hasResource(sf)) {
        // Do not modify the Session: just set the participate flag.
        setParticipate(true);
    } else {
        setParticipate(false);
        LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor");
        Session session = getSession();
        GrailsHibernateUtil.enableDynamicFilterEnablerIfPresent(sf, session);
        session.setFlushMode(FlushMode.AUTO);
        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    }
}

From source file:org.grails.orm.hibernate3.support.HibernatePersistenceContextInterceptor.java

License:Apache License

public void init() {
    if (incNestingCount() > 1) {
        return;// w  w w  .  j av a2s .  co  m
    }
    SessionFactory sf = getSessionFactory();
    if (sf == null) {
        return;
    }
    if (TransactionSynchronizationManager.hasResource(sf)) {
        // Do not modify the Session: just set the participate flag.
        setParticipate(true);
    } else {
        setParticipate(false);
        LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor");
        Session session = getSession();
        HibernateRuntimeUtils.enableDynamicFilterEnablerIfPresent(sf, session);
        session.setFlushMode(FlushMode.AUTO);
        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    }
}

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 a  va 2 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);
}