Example usage for org.springframework.orm.hibernate5 SessionFactoryUtils getFlushMode

List of usage examples for org.springframework.orm.hibernate5 SessionFactoryUtils getFlushMode

Introduction

In this page you can find the example usage for org.springframework.orm.hibernate5 SessionFactoryUtils getFlushMode.

Prototype

Method getFlushMode

To view the source code for org.springframework.orm.hibernate5 SessionFactoryUtils getFlushMode.

Click Source Link

Usage

From source file:org.springframework.orm.hibernate5.HibernateTemplate.java

/**
 * Check whether write operations are allowed on the given Session.
 * <p>Default implementation throws an InvalidDataAccessApiUsageException in
 * case of {@code FlushMode.MANUAL}. Can be overridden in subclasses.
 * @param session current Hibernate Session
 * @throws InvalidDataAccessApiUsageException if write operations are not allowed
 * @see #setCheckWriteOperations/*from   w w  w . ja v a  2s.c  om*/
 * @see Session#getFlushMode()
 * @see FlushMode#MANUAL
 */
protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException {
    if (isCheckWriteOperations() && SessionFactoryUtils.getFlushMode(session).lessThan(FlushMode.COMMIT)) {
        throw new InvalidDataAccessApiUsageException(
                "Write operations are not allowed in read-only mode (FlushMode.MANUAL): "
                        + "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.");
    }
}

From source file:org.springframework.orm.hibernate5.SpringSessionContext.java

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *//* www  .j a v  a 2 s  .c  o m*/
@Override
@SuppressWarnings("deprecation")
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
    if (value instanceof Session) {
        return (Session) value;
    } else if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        Session session = sessionHolder.getSession();
        if (!sessionHolder.isSynchronizedWithTransaction()
                && TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(
                    new SpringSessionSynchronization(sessionHolder, this.sessionFactory, false));
            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 = SessionFactoryUtils.getFlushMode(session);
            if (flushMode.equals(FlushMode.MANUAL)
                    && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }
        return session;
    }

    if (this.transactionManager != null && this.jtaSessionContext != null) {
        try {
            if (this.transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                Session session = this.jtaSessionContext.currentSession();
                if (TransactionSynchronizationManager.isSynchronizationActive()) {
                    TransactionSynchronizationManager
                            .registerSynchronization(new SpringFlushSynchronization(session));
                }
                return session;
            }
        } catch (SystemException ex) {
            throw new HibernateException("JTA TransactionManager found but status check failed", ex);
        }
    }

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        Session session = this.sessionFactory.openSession();
        if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
            session.setFlushMode(FlushMode.MANUAL);
        }
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.registerSynchronization(
                new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));
        TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);
        sessionHolder.setSynchronizedWithTransaction(true);
        return session;
    } else {
        throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
    }
}