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:edu.nps.moves.mmowgli.hibernate.HSess.java

License:Open Source License

public static void init() {
    if (get() != null) {
        dumpPreviousCallerTrace();//  ww  w  .ja  v a 2s . c o m
        repair(); // closes after dumping stack in sys out
    }

    Session s = VHib.openSession();
    s.setFlushMode(FlushMode.COMMIT);
    s.beginTransaction();
    s.getTransaction().setTimeout(HIBERNATE_TRANSACTION_TIMEOUT_IN_SECONDS);

    MSysOut.println(HIBERNATE_LOGS, "HSess.open() of sess " + s.hashCode());
    set(s);
}

From source file:edu.wustl.common.hibernate.HibernateUtil.java

License:BSD License

public static Session newSession() {
    Session session = m_sessionFactory.openSession();
    session.setFlushMode(FlushMode.COMMIT);
    try {/* w  w w.j a va 2  s .  c  om*/
        session.connection().setAutoCommit(false);
    } catch (SQLException ex) {
        throw new HibernateException(ex.getMessage(), ex);
    }
    return session;
}

From source file:edu.wustl.dao.connectionmanager.ConnectionManager.java

License:BSD License

/**
 * This method will be called to create new session.
 * @return session object.//from   w  w w  .  j  av  a 2 s.c  o  m
 *@throws DAOException :Generic DAOException.
 */
public Session newSession() throws DAOException {
    try {
        Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.COMMIT);
        session.connection().setAutoCommit(false);
        return session;
    } catch (Exception excp) {
        ErrorKey errorKey = ErrorKey.getErrorKey("db.operation.error");
        throw new DAOException(errorKey, excp, "ConnectionManager.java :" + DAOConstants.NEW_SESSION_ERROR);
    }
}

From source file:example.cds.HibernateSessionFactoryFinder.java

License:Open Source License

public Session getSession(String name) {
    // run code found in Jun's example if no name specified
    if (name == null || name.length() == 0) {
        return getDefaultSessionFactory().getCurrentSession();
    }// w  ww .  j a  va 2  s .c  om
    Object sessionManager = applicationContext.getBean(name);
    if (sessionManager == null) {
        throw new IllegalArgumentException("no spring bean found with the name '" + name + "'");
    }
    if (sessionManager instanceof SessionFactory) {
        Session session = ((SessionFactory) sessionManager).openSession();
        session.setFlushMode(FlushMode.NEVER);
        return session;
    } else if (sessionManager instanceof HibernateDaoSupport) {
        HibernateDaoSupport daoSupport = (HibernateDaoSupport) sessionManager;
        Session session = daoSupport.getSessionFactory().openSession();
        session.setFlushMode(FlushMode.NEVER);
        return session;
    } else {
        throw new IllegalArgumentException("spring bean with name '" + name + "' has bogus type '"
                + sessionManager.getClass().getName() + "'");
    }
}

From source file:gov.nih.nci.caarray.dao.HibernateIntegrationTestCleanUpUtility.java

License:BSD License

private static boolean doCleanUp(String deleteSql) {
    Transaction tx = null;/*  w  w  w .j  av  a2s  . c  o m*/
    boolean removed = false;
    Session s = null;
    try {
        s = getSession();
        s.setFlushMode(FlushMode.MANUAL);
        tx = s.beginTransaction();
        disableForeignKeyChecks(s);
        int deletedObjs = s.createQuery(deleteSql).executeUpdate();
        if (deletedObjs > 0) {
            removed = true;
        }
        s.flush();
        tx.commit();
    } catch (DAOException deleteException) {
        hibernateHelper.rollbackTransaction(tx);
        LOG.warn("Error cleaning up test objects.", deleteException);
    } catch (HibernateException he) {
        hibernateHelper.rollbackTransaction(tx);
        LOG.warn("Error cleaning up test objects.", he);
    } finally {
        s.close();
    }
    return removed;
}

From source file:gov.nih.nci.caarray.validation.UniqueConstraintValidator.java

License:BSD License

/**
 * {@inheritDoc}//  w  ww . j  a v  a2 s.  com
 */
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.ExcessiveMethodLength" })
public boolean isValid(final Object o) {
    UnfilteredCallback unfilteredCallback = new UnfilteredCallback() {
        public Object doUnfiltered(Session s) {
            FlushMode fm = s.getFlushMode();
            try {
                s.setFlushMode(FlushMode.MANUAL);
                Class<?> classWithConstraint = findClassDeclaringConstraint(
                        hibernateHelper.unwrapProxy(o).getClass());
                Criteria crit = s.createCriteria(classWithConstraint);
                ClassMetadata metadata = hibernateHelper.getSessionFactory()
                        .getClassMetadata(classWithConstraint);
                for (UniqueConstraintField field : uniqueConstraint.fields()) {
                    Object fieldVal = metadata.getPropertyValue(o, field.name(), EntityMode.POJO);
                    if (fieldVal == null) {
                        if (field.nullsEqual()) {
                            // nulls are equal, so add it to to criteria
                            crit.add(Restrictions.isNull(field.name()));
                        } else {
                            // nulls are never equal, so uniqueness is automatically satisfied
                            return true;
                        }
                    } else {
                        // special casing for entity-type properties - only include them in the criteria if they are
                        // already
                        // persistent
                        // otherwise, short-circuit the process and return true immediately since if the
                        // entity-type property
                        // is not persistent then it will be a new value and thus different from any currently in 
                        // the db, thus satisfying uniqueness
                        ClassMetadata fieldMetadata = hibernateHelper.getSessionFactory()
                                .getClassMetadata(hibernateHelper.unwrapProxy(fieldVal).getClass());
                        if (fieldMetadata == null
                                || fieldMetadata.getIdentifier(fieldVal, EntityMode.POJO) != null) {
                            crit.add(Restrictions.eq(field.name(),
                                    ReflectHelper.getGetter(o.getClass(), field.name()).get(o)));
                        } else {
                            return true;
                        }
                    }
                }
                // if object is already persistent, then add condition to exclude it matching itself
                Object id = metadata.getIdentifier(o, EntityMode.POJO);
                if (id != null) {
                    crit.add(Restrictions.ne(metadata.getIdentifierPropertyName(), id));
                }

                int numMatches = crit.list().size();
                return numMatches == 0;
            } finally {
                s.setFlushMode(fm);
            }
        }
    };
    return (Boolean) hibernateHelper.doUnfiltered(unfilteredCallback);

}

From source file:gov.nih.nci.firebird.commons.test.TestDataRemover.java

License:Open Source License

public void cleanUp(List<String> dataRemovalSqlStatements) {
    Transaction tx = null;/*from w w  w.  j  a  v  a2  s . c o m*/
    Session session = null;
    try {
        session = openSession();
        session.setFlushMode(FlushMode.MANUAL);
        tx = session.beginTransaction();
        doCleanUp(session, dataRemovalSqlStatements);
        session.flush();
        tx.commit();
    } catch (HibernateException he) {
        handleException(tx, he, "Error deleting test data");
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:gov.nih.nci.firebird.test.TestDataHandler.java

License:Open Source License

public void cleanUp() {
    Transaction tx = null;/*from   w w w . j  a v  a  2 s .c o m*/
    Session session = null;
    try {
        session = openSession();
        session.setFlushMode(FlushMode.MANUAL);
        tx = session.beginTransaction();
        doCleanUp(session);
        session.flush();
        tx.commit();
    } catch (HibernateException he) {
        handleException(tx, he, "Error deleting test data");
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:gov.nih.nci.lv.util.HibernateHelper.java

License:BSD License

/**
 * Open a hibernate session and bind it as the current session via
 * {@link ManagedSessionContext#bind(org.hibernate.classic.Session)}. The hibernate property
 * "hibernate.current_session_context_class" must be set to "managed" for this to have effect This method should be
 * called from within an Interceptor or Filter type class that is setting up the scope of the Session. This method
 * should then call {@link HibernateUtil#unbindAndCleanupSession()} when the scope of the Session is expired.
 *
 * @see ManagedSessionContext#bind(org.hibernate.classic.Session)
 *///  w  ww  .j a  v a 2s .c om
public void openAndBindSession() {
    SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) getSessionFactory();
    org.hibernate.classic.Session currentSession = sessionFactoryImplementor.openSession(null, true, false,
            ConnectionReleaseMode.AFTER_STATEMENT);
    currentSession.setFlushMode(FlushMode.COMMIT);
    ManagedSessionContext.bind(currentSession);
}

From source file:grails.plugins.quartz.listeners.SessionBinderJobListener.java

License:Apache License

public void jobToBeExecuted(JobExecutionContext context) {
    Session session = SessionFactoryUtils.getSession(sessionFactory, true);
    session.setFlushMode(FlushMode.AUTO);
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    if (LOG.isDebugEnabled())
        LOG.debug("Hibernate Session is bounded to Job thread");
}