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:in.pleasecome.tohich_hunter.checkin.DAO.TownDAOImpl.java

@Override
@Transactional(readOnly = false)/*from ww  w .  ja  v  a  2 s. c o  m*/
public void edit(Town town) {
    getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
    getHibernateTemplate().update(town);
}

From source file:in.pleasecome.tohich_hunter.checkin.DAO.TownDAOImpl.java

@Override
@Transactional(readOnly = false)//w  ww .j  a  v a2s . co  m
public void delete(Town town) {
    getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
    getHibernateTemplate().delete(town);
}

From source file:io.dropwizard.sharding.utils.TransactionHandler.java

License:Apache License

private void configureSession() {
    session.setDefaultReadOnly(readOnly);
    session.setCacheMode(CacheMode.NORMAL);
    session.setFlushMode(FlushMode.AUTO);
}

From source file:model.PDSTallerPersistentManager.java

private PDSTallerPersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);/*w  ww. ja  v a 2s.co  m*/
    setFlushMode(FlushMode.AUTO);
}

From source file:modelo.orm.ORMSISREHMEDPersistentManager.java

private ORMSISREHMEDPersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);//from ww  w  . ja va2  s  . c  o m
    setFlushMode(FlushMode.AUTO);
}

From source file:models.AutoinsurancePersistentManager.java

private AutoinsurancePersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);//from   w ww .  j  a  v a2 s.c  o  m
    setFlushMode(FlushMode.AUTO);
}

From source file:ngs.persistentmodel.NextGenSport2PersistentManager.java

private NextGenSport2PersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);/*from   w  ww. j  a  v  a2 s  .  c  om*/
    setFlushMode(FlushMode.AUTO);
}

From source file:org.babyfish.springframework.orm.hibernate.SpringXSessionContext.java

License:Open Source License

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *///from w ww  . j a  v a2  s  .c  om
@SuppressWarnings("deprecation")
public XSession currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
    if (value instanceof XSession) {
        return (XSession) value;
    } else if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        XSession session = (XSession) sessionHolder.getSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()
                && !sessionHolder.isSynchronizedWithTransaction()) {
            TransactionSynchronizationManager.registerSynchronization(
                    new SpringSessionSynchronization(sessionHolder, this.sessionFactory));
            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 = session.getFlushMode();
            if (FlushMode.isManualFlushMode(flushMode)
                    && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }
        return session;
    } else if (this.jtaSessionContext != null) {
        XSession session = this.jtaSessionContext.currentSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new SpringFlushSynchronization(session));
        }
        return session;
    } else {
        throw new HibernateException("No Session found for current thread");
    }
}

From source file:org.beangle.commons.orm.hibernate.HibernateTransactionManager.java

License:Open Source License

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

    if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
        throw new IllegalTransactionStateException(
                "Pre-bound JDBC Connection found! HibernateTransactionManager does not support "
                        + "running within DataSourceTransactionManager if told to manage the DataSource itself. "
                        + "It is recommended to use a single HibernateTransactionManager for all transactions "
                        + "on a single DataSource, no matter whether Hibernate or JDBC access.");
    }/*  w  ww . j ava  2s .c  o  m*/

    Session session = null;

    try {
        if (txObject.getSessionHolder() == null
                || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
            Interceptor entityInterceptor = getEntityInterceptor();
            Session newSession = (entityInterceptor != null ? getSessionFactory().openSession(entityInterceptor)
                    : getSessionFactory().openSession());
            if (logger.isDebugEnabled()) {
                logger.debug("Opened new Session [" + SessionUtils.toString(newSession)
                        + "] for Hibernate transaction");
            }
            txObject.setSession(newSession);
        }

        session = txObject.getSessionHolder().getSession();

        if (this.prepareConnection && isSameConnectionForEntireSession(session)) {
            // We're allowed to change the transaction settings of the JDBC Connection.
            if (logger.isDebugEnabled()) {
                logger.debug("Preparing JDBC Connection of Hibernate Session [" + SessionUtils.toString(session)
                        + "]");
            }
            @SuppressWarnings("deprecation")
            Connection con = session.connection();
            Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
            txObject.setPreviousIsolationLevel(previousIsolationLevel);
        } else {
            // Not allowed to change the transaction settings of the JDBC Connection.
            if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
                // We should set a specific isolation level but are not allowed to...
                throw new InvalidIsolationLevelException(
                        "HibernateTransactionManager is not allowed to support custom isolation levels: "
                                + "make sure that its 'prepareConnection' flag is on (the default) and that the "
                                + "Hibernate connection release mode is set to 'on_close' (BeangleTransactionFactory's default). "
                                + "Make sure that your SessionFactoryBean actually uses BeangleTransactionFactory: Your "
                                + "Hibernate properties should *not* include a 'hibernate.transaction.factory_class' property!");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Not preparing JDBC Connection of Hibernate Session ["
                        + SessionUtils.toString(session) + "]");
            }
        }

        if (definition.isReadOnly() && txObject.isNewSession()) {
            // Just set to NEVER in case of a new Session for this transaction.
            session.setFlushMode(FlushMode.MANUAL);
        }

        if (!definition.isReadOnly() && !txObject.isNewSession()) {
            // We need AUTO or COMMIT for a non-read-only transaction.
            FlushMode flushMode = session.getFlushMode();
            if (flushMode.lessThan(FlushMode.COMMIT)) {
                session.setFlushMode(FlushMode.AUTO);
                txObject.getSessionHolder().setPreviousFlushMode(flushMode);
            }
        }

        Transaction hibTx;

        // Register transaction timeout.
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            // Use Hibernate's own transaction timeout mechanism on Hibernate 3.1+
            // Applies to all statements, also to inserts, updates and deletes!
            hibTx = session.getTransaction();
            hibTx.setTimeout(timeout);
            hibTx.begin();
        } else {
            // Open a plain Hibernate transaction without specified timeout.
            hibTx = session.beginTransaction();
        }

        // Add the Hibernate transaction to the session holder.
        txObject.getSessionHolder().setTransaction(hibTx);

        // Register the Hibernate Session's JDBC Connection for the DataSource, if set.
        if (getDataSource() != null) {
            @SuppressWarnings("deprecation")
            Connection con = session.connection();
            ConnectionHolder conHolder = new ConnectionHolder(con);
            if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
                conHolder.setTimeoutInSeconds(timeout);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");
            }
            TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
            txObject.setConnectionHolder(conHolder);
        }

        // Bind the session holder to the thread.
        if (txObject.isNewSessionHolder()) {
            TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());
        }
        txObject.getSessionHolder().setSynchronizedWithTransaction(true);
    }

    catch (Exception ex) {
        if (txObject.isNewSession()) {
            try {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
            } catch (Throwable ex2) {
                logger.debug("Could not rollback Session after failed transaction begin", ex);
            } finally {
                SessionUtils.closeSession(session);
            }
        }
        throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
    }
}

From source file:org.beangle.orm.hibernate.BeangleSessionContext.java

License:Open Source License

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *//*from w ww. j  ava 2 s. c  o  m*/
public Session currentSession() throws HibernateException {
    SessionHolder sessionHolder = SessionUtils.currentSession(this.sessionFactory);
    Session session = sessionHolder.getSession();
    // TODO what time enter into the code?
    if (TransactionSynchronizationManager.isSynchronizationActive()
            && !sessionHolder.isSynchronizedWithTransaction()) {
        TransactionSynchronizationManager
                .registerSynchronization(new SessionSynchronization(sessionHolder, this.sessionFactory));
        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 = session.getFlushMode();
        if (FlushMode.isManualFlushMode(flushMode)
                && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
            session.setFlushMode(FlushMode.AUTO);
            sessionHolder.setPreviousFlushMode(flushMode);
        }
    }
    return session;
}