Example usage for org.hibernate Session setFlushMode

List of usage examples for org.hibernate Session setFlushMode

Introduction

In this page you can find the example usage for org.hibernate Session setFlushMode.

Prototype

@Deprecated
void setFlushMode(FlushMode flushMode);

Source Link

Document

Set the flush mode for this session.

Usage

From source file:org.castafiore.persistence.DaoImpl.java

License:Open Source License

public Session getSession() {
    try {//from  w  w w .  j  av  a 2  s.c om
        Session session = SESSION_THREAD.get();
        if (session == null || !session.isOpen()) {
            session = getHibernateTemplate().getSessionFactory().openSession();
            session.setFlushMode(FlushMode.MANUAL);
            //session.setCacheMode(CacheMode.IGNORE);
            SESSION_THREAD.set(session);
        }

        Transaction t = TRANSACTION_THREAD.get();
        if (t == null) {
            t = session.beginTransaction();
            t.setTimeout(9000);
            TRANSACTION_THREAD.set(t);

        } else {
            if (!t.isActive()) {
                t.begin();
            }
        }
        return session;

    } catch (Exception e) {
        //return HibernateUtil.getSession(getHibernateTemplate().getSessionFactory());
        throw new RuntimeException(e);
    }
}

From source file:org.castafiore.persistence.LoadHibernateSessionFilter.java

License:Open Source License

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    SessionFactory factory = SpringUtil.getBeanOfType(SessionFactory.class);
    Map castaSession = SpringUtil.getBean("casta_session");
    Session s = null;
    if (!castaSession.containsKey("hibernate.session")) {
        s = factory.openSession();//from ww  w. ja va2 s .c  om
        s.setFlushMode(FlushMode.COMMIT);
        s.setCacheMode(CacheMode.IGNORE);
        castaSession.put("hibernate.session", s);

    } else {
        s = (Session) castaSession.get("hibernate.session");
    }

    //t = null;
    try {
        if (t == null) {
            t = s.beginTransaction();
            creatorHash = request.hashCode();
        }

        if (!t.isActive()) {
            t.begin();
            creatorHash = request.hashCode();
        }

        chain.doFilter(request, response);

        //s.flush();
        if (t.isActive()) {
            if (creatorHash == request.hashCode()) {
                s.flush();
                t.commit();
                s.clear();

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        if (t != null && t.isActive()) {
            if (creatorHash == request.hashCode()) {
                t.rollback();
                s.clear();
            }
        }

        if (s != null) {

        }
    }

}

From source file:org.celllife.idart.database.hibernate.util.HibernateUtil.java

License:Open Source License

public Session getSession() {
    Session sess = sessionFactory.openSession();
    sess.setFlushMode(FlushMode.COMMIT);
    return sess;
}

From source file:org.cgiar.ccafs.ap.data.dao.mysqlhiberate.StandardDAO.java

License:Open Source License

/**
 * This method opens a session to the database.
 * /*ww w. j  a  v a2s  .c  o  m*/
 * @return a Session object.
 */
private Session openSession() {
    Session session = null;
    session = sessionFactory.openSession();
    // Calling flush when committing change.
    session.setFlushMode(FlushMode.COMMIT);
    return session;

}

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

License:Apache License

/**
 * Sets the target object to read-only using the given SessionFactory instance. This
 * avoids Hibernate performing any dirty checking on the object
 *
 * @see #setObjectToReadWrite(Object, org.hibernate.SessionFactory)
 *
 * @param target The target object/*from  w w  w.j  ava 2s. c om*/
 * @param sessionFactory The SessionFactory instance
 */
public static void setObjectToReadyOnly(Object target, SessionFactory sessionFactory) {
    Session session = sessionFactory.getCurrentSession();
    if (canModifyReadWriteState(session, target)) {
        if (target instanceof HibernateProxy) {
            target = ((HibernateProxy) target).getHibernateLazyInitializer().getImplementation();
        }
        session.setReadOnly(target, true);
        session.setFlushMode(FlushMode.MANUAL);
    }
}

From source file:org.codehaus.groovy.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//from  w  ww . ja v  a 2s .c  o  m
 * @param sessionFactory The SessionFactory instance
 */
public static void setObjectToReadWrite(final Object target, SessionFactory sessionFactory) {
    HibernateTemplate template = new HibernateTemplate(sessionFactory);
    template.setExposeNativeSession(true);
    template.execute(new HibernateCallback<Void>() {
        public Void doInHibernate(Session session) throws HibernateException, SQLException {
            if (canModifyReadWriteState(session, target)) {
                SessionImplementor sessionImpl = (SessionImplementor) session;
                EntityEntry ee = sessionImpl.getPersistenceContext().getEntry(target);

                if (ee != null && ee.getStatus() == Status.READ_ONLY) {
                    Object actualTarget = target;
                    if (target instanceof HibernateProxy) {
                        actualTarget = ((HibernateProxy) target).getHibernateLazyInitializer()
                                .getImplementation();
                    }

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

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

License:Apache License

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *///from w w  w . j  av a2  s .  c o 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 = session.getFlushMode();
            if (FlushMode.isManualFlushMode(flushMode)
                    && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(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.codehaus.groovy.grails.orm.hibernate.GrailsSessionContext.java

License:Apache License

private Session createSession(Object resource) {
    LOG.debug("Opening Hibernate Session");

    SessionHolder sessionHolder = (SessionHolder) resource;

    Session session = sessionFactory.openSession();

    // Use same Session for further Hibernate actions within the transaction.
    // Thread object will get removed by synchronization at transaction completion.
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        // We're within a Spring-managed transaction, possibly from JtaTransactionManager.
        LOG.debug("Registering Spring transaction synchronization for new Hibernate Session");
        SessionHolder holderToUse = sessionHolder;
        if (holderToUse == null) {
            holderToUse = new SessionHolder(session);
        } else {/*  w ww . ja  v a 2s  .com*/
            // it's up to the caller to manage concurrent sessions
            // holderToUse.addSession(session);
        }
        if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
            session.setFlushMode(FlushMode.MANUAL);
        }
        TransactionSynchronizationManager
                .registerSynchronization(createSpringSessionSynchronization(holderToUse));
        holderToUse.setSynchronizedWithTransaction(true);
        if (holderToUse != sessionHolder) {
            TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
        }
    } else {
        // No Spring transaction management active -> try JTA transaction synchronization.
        registerJtaSynchronization(session, sessionHolder);
    }

    /*        // Check whether we are allowed to return the Session.
            if (!allowCreate && !isSessionTransactional(session, sessionFactory)) {
               closeSession(session);
               throw new IllegalStateException("No Hibernate Session bound to thread, " +
      "and configuration does not allow creation of non-transactional one here");
            }
    */
    return session;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.InstanceApiHelper.java

License:Apache License

public void setFlushModeManual() {
    hibernateTemplate.execute(new HibernateCallback<Void>() {
        public Void doInHibernate(Session session) {
            session.setFlushMode(FlushMode.MANUAL);
            return null;
        }//from  ww  w .  j  a va  2  s  .  c  o m
    });
}

From source file:org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventListener.java

License:Apache License

private <T> T doWithManualSession(AbstractEvent event, Closure<T> callable) {
    Session session = event.getSession();
    FlushMode current = session.getFlushMode();
    try {//from   ww  w  .j  a  v  a  2  s  .c  om
        session.setFlushMode(FlushMode.MANUAL);
        return callable.call();
    } finally {
        session.setFlushMode(current);
    }
}