Example usage for org.hibernate FlushMode COMMIT

List of usage examples for org.hibernate FlushMode COMMIT

Introduction

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

Prototype

FlushMode COMMIT

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

Click Source Link

Document

The Session is flushed when Transaction#commit is called.

Usage

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 {//from  ww w  .  j a va  2s  . co  m
        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 ww .j  a  v  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:es.tekniker.framework.ktek.commons.KTEKPersistentManager.java

License:Open Source License

private KTEKPersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);/* w  w w . java  2s.  c o m*/
    setFlushMode(FlushMode.COMMIT);
}

From source file:fr.mcc.ginco.dao.hibernate.ThesaurusTermDAO.java

License:CeCILL license

@Override
public ThesaurusTerm update(ThesaurusTerm termToUpdate) throws BusinessException {
    getCurrentSession().setFlushMode(FlushMode.COMMIT);
    // Verifying if there is no a similar term (lexicalValue + lang)
    Long numberOfExistingTerms = countSimilarTermsByLexicalValueAndLanguage(termToUpdate);
    if (numberOfExistingTerms > 0) {
        throw new BusinessException("Already existing term : " + termToUpdate.getLexicalValue(),
                "already-existing-term", new Object[] { termToUpdate.getLexicalValue() });
    }/*from   w  w  w.j  a va2s.c  om*/

    if (termToUpdate.getHidden() == null) {
        //By default, hidden is false if not set
        termToUpdate.setHidden(false);
    } else if (termToUpdate.getHidden()) {
        if (termToUpdate.getPrefered()) {
            throw new BusinessException("Only non prefered terms can be hidden",
                    "only-non-prefered-term-can-be-hidden");
        }
    }

    // Update an existing term
    getCurrentSession().saveOrUpdate(termToUpdate);
    getCurrentSession().flush();
    return termToUpdate;
}

From source file:gcs.GCSPersistentManager.java

private GCSPersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);//from  www . j av a 2 s.c om
    setFlushMode(FlushMode.COMMIT);
}

From source file:gov.nih.nci.firebird.test.data.TestDataLoader.java

License:Open Source License

void openSession() {
    session = hibernateHelper.getSessionFactory().openSession();
    session.setFlushMode(FlushMode.COMMIT);
    transaction = session.beginTransaction();
}

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)
 *//*from  ww w.  java2  s.c o  m*/
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:ome.server.utests.handlers.SessionHandlerMockHibernateTest.java

License:Open Source License

@Test
public void testStatefulInvocationWithSessionThenClosed() throws Throwable {
    newStatefulDestroyInvocation();/*from w w w  .  j a  v  a  2 s . c om*/
    opensSession();
    setsFlushMode(FlushMode.COMMIT);
    beginsTransaction(1);
    disconnectsSession();
    closesSession();
    handler.invoke(invocation);
    for (RegisterServiceCleanupMessage r : cleanups) {
        r.close();
    }
    super.verify();
}

From source file:ome.server.utests.handlers.SessionHandlerMockHibernateTest.java

License:Open Source License

private void setsFlushMode(FlushMode... modes) {
    // done by handler see ticket:557
    if (modes.length == 0) {
        mockSession.expects(atLeastOnce()).method("setFlushMode").with(eq(FlushMode.COMMIT));
        mockSession.expects(atLeastOnce()).method("setFlushMode").with(eq(FlushMode.MANUAL));
    } else {//from w  ww  . j  a  v a 2 s.  c om
        for (FlushMode mode : modes) {
            mockSession.expects(atLeastOnce()).method("setFlushMode").with(eq(mode));
        }
    }
}

From source file:ome.tools.hibernate.SessionStatus.java

License:Open Source License

private Object doStateful(final MethodInvocation invocation) throws Throwable {
    Object result = null;/*w w  w. j  av  a2  s .co  m*/
    SessionStatus status = null;
    try {
        // Need to open even if "closing" because the service may need
        // to perform cleanup in its close() method.
        status = newOrRestoredSession(invocation);
        status.session.setFlushMode(FlushMode.COMMIT);
        // changing MANUAL to COMMIT for ticket:557. the appserver
        // won't allow us to commit here anyway, and setting to COMMIT
        // prevents Spring from automatically re-writing the flushMode
        // as AUTO
        result = invocation.proceed();
        return result;
    } finally {
        // TODO do we need to check for disconnected or closed session here?
        // The newOrRestoredSession method does not attempt to close the
        // session before throwing the dirty session exception. We must do
        // it here.
        try {
            if (isCloseSession(invocation)) {
                ctx.publishMessage(new RegisterServiceCleanupMessage(this, invocation.getThis()) {
                    @Override
                    public void close() {
                        SessionStatus status = removeStatus(invocation);
                        status.session.disconnect();
                        status.session.close();
                    }

                });
            } else {
                if (status != null) {
                    // Guarantee that no one has changed the FlushMode
                    status.session.setFlushMode(FlushMode.MANUAL);
                    status.session.disconnect();
                    status.calls--;
                }
            }
        } catch (Exception e) {

            log.error("Error while closing/disconnecting session.", e);

        } finally {

            try {
                resetThreadSession();
            } catch (Exception e) {
                log.error("Could not cleanup thread session.", e);
                throw e;
            }

        }

    }
}