Example usage for org.hibernate FlushMode MANUAL

List of usage examples for org.hibernate FlushMode MANUAL

Introduction

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

Prototype

FlushMode MANUAL

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

Click Source Link

Document

The Session is only ever flushed when Session#flush is explicitly called by the application.

Usage

From source file:org.jboss.seam.persistence.hibernate.test.ManagedHibernateSessionFlushModeTestBase.java

License:Open Source License

@Test
public void testChangedTouchedSessionFlushMode() {
    try {/*w  w  w. jav a2  s.co m*/
        session.setFlushMode(FlushMode.AUTO);
        pc.changeFlushMode(FlushModeType.MANUAL);
        Assert.assertEquals(FlushMode.MANUAL, session.getFlushMode());
    } finally {
        session.setFlushMode(FlushMode.AUTO);
    }
}

From source file:org.jboss.seam.persistence.test.ManagedPersistenceContextFlushModeTestBase.java

License:Open Source License

@Test
public void testPersistenceContextDefaultFlushMode()
        throws NotSupportedException, SystemException, SecurityException, IllegalStateException,
        RollbackException, HeuristicMixedException, HeuristicRollbackException {
    manager.setFlushModeType(FlushModeType.MANUAL);
    Assert.assertEquals(FlushMode.MANUAL, ((Session) em.getDelegate()).getFlushMode());
}

From source file:org.jboss.seam.persistence.test.ManagedPersistenceContextFlushModeTestBase.java

License:Open Source License

@Test
public void testChangedTouchedPersistenceContextFlushMode() {
    try {//from www  .  ja  v a 2  s .c  o  m
        em.setFlushMode(javax.persistence.FlushModeType.AUTO);
        pc.changeFlushMode(FlushModeType.MANUAL);
        Assert.assertEquals(FlushMode.MANUAL, ((Session) em.getDelegate()).getFlushMode());
    } finally {
        em.setFlushMode(javax.persistence.FlushModeType.AUTO);
    }
}

From source file:org.jooby.internal.hbm.RootUnitOfWork.java

License:Apache License

public RootUnitOfWork setReadOnly() {
    if (rollbackOnly) {
        return this;
    }/*from  w  ww . ja  va2 s  .  com*/

    log.debug("read-only session: {}", oid(session));
    setConnectionReadOnly(true);
    readOnly = true;
    session.setHibernateFlushMode(FlushMode.MANUAL);
    session.setDefaultReadOnly(true);
    return this;
}

From source file:org.jooby.internal.hbm.TrxResponse.java

License:Apache License

private void trxSend(final Object result) throws Throwable {

    Consumer<Boolean> setReadOnly = (readOnly) -> {
        try {/* ww  w .ja  va  2s.  co m*/
            Connection connection = ((SessionImplementor) session).connection();
            connection.setReadOnly(readOnly);
        } catch (Exception ex) {
            log.trace("  [" + sessionId + "] unable to setReadOnly " + readOnly, ex);
        }
        session.setDefaultReadOnly(readOnly);
    };

    try {
        log.debug("  [{}] flushing", sessionId);
        session.flush();

        if (trx.isActive()) {
            log.debug("  [{}] commiting transaction: {}", sessionId, trx);
            trx.commit();
        }

        EntityTransaction readOnlyTrx = null;
        try {
            // start new transaction
            log.debug("  [{}] setting connection to read only", sessionId);
            setReadOnly.accept(true);
            session.setFlushMode(FlushMode.MANUAL);
            readOnlyTrx = em.getTransaction();
            log.debug("  [{}] starting readonly transaction: {}", sessionId, readOnlyTrx);
            readOnlyTrx.begin();

            // send it!
            rsp.send(result);

            log.debug("  [{}] commiting readonly transaction: {}", sessionId, readOnlyTrx);
            readOnlyTrx.commit();
        } catch (Throwable ex) {
            if (readOnlyTrx != null && readOnlyTrx.isActive()) {
                log.debug("  [{}] rolling back readonly transaction: {}", sessionId, readOnlyTrx);
                readOnlyTrx.rollback();
            }
            throw ex;
        } finally {
            log.debug("  [{}] removing readonly mode from connection", sessionId);
            setReadOnly.accept(false);
        }
    } catch (Throwable ex) {
        rollbackOnly = true;
        throw ex;
    } finally {
        done();
    }
}

From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController.java

License:Open Source License

private Session getNoTxSession() {
    if (noTxSession == null) {
        if (noTxDataSource != null) {
            try {
                noTxSession = getHibernateSessionFactory().withOptions()
                        .connection(noTxDataSource.getConnection()).openSession();
            } catch (SQLException ex) {
                LOG.error("Couldn't get connection from non transactional data source {}", noTxDataSource);
                throw new BackendException(ex, "Couldn't get connection from non transactional data source");
            }/*from  ww w. j  a v  a  2 s . c  om*/
        } else {
            noTxSession = getHibernateSessionFactory().openSession();
        }
        noTxSession.setFlushMode(FlushMode.MANUAL);
    }
    return noTxSession;
}

From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController.java

License:Open Source License

private void initializeProperty(IComponent componentOrEntity, String propertyName) {
    Session hibernateSession = getHibernateSession();
    FlushMode oldFlushMode = hibernateSession.getFlushMode();
    try {//from w w  w .j  a  v  a 2 s.c om
        // Temporary switch to a read-only session.
        hibernateSession.setFlushMode(FlushMode.MANUAL);
        try {
            currentInitializationSession = hibernateSession;
            performPropertyInitializationUsingSession(componentOrEntity, propertyName, hibernateSession);
        } finally {
            currentInitializationSession = null;
        }
    } finally {
        hibernateSession.setFlushMode(oldFlushMode);
    }
}

From source file:org.jtalks.poulpe.web.osod.OpenSessions.java

License:Open Source License

/**
 * Creates a new session for the specified desktop id if doesn't exist. If it's already inside {@link OpenSessions},
 * then it will be returned. It also sets the flush mode to {@link FlushMode#MANUAL} when session is opened. This
 * method doesn't bind sessions to the thread.
 *
 * @param desktopId an id of the desktop to search the session for
 * @return a session that is bound to the desktop or new session that is not bound to anything if no desktop with
 *         such id was registered//  ww w . jav a2  s  .co  m
 */
@VisibleForTesting
Session getOrCreateSession(String desktopId) {
    Session session = sessions.get(desktopId);
    if (session == null) {
        session = createSession();
        session.setFlushMode(FlushMode.MANUAL);
    }
    return session;
}

From source file:org.kuali.rice.krad.dao.impl.BusinessObjectDaoJpa.java

License:Educational Community License

/**
 * @see org.kuali.rice.krad.dao.BusinessObjectDao#manageReadOnly(org.kuali.rice.krad.bo.PersistableBusinessObject)
 *//*from ww  w  . j a v  a2s .co m*/
public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
    Session session = ((HibernateEntityManager) entityManager).getSession();
    FlushMode currentFlushMode = session.getFlushMode();
    session.setFlushMode(FlushMode.MANUAL); // make sure the merge doesn't flush what we're trying to make read only
    PersistableBusinessObject managedBO = entityManager.merge(bo);
    session.setReadOnly(managedBO, true);
    session.setFlushMode(currentFlushMode);
    return managedBO;
}

From source file:org.mifos.application.questionnaire.migration.QuestionnaireMigration.java

License:Open Source License

private Integer migratePPISurvey(PPISurvey survey) {
    Integer questionGroupId = null;
    getSession().beginTransaction();/*from   w ww  . j a  v a 2s.com*/
    getSession().setFlushMode(FlushMode.MANUAL);
    try {
        QuestionGroupDto questionGroupDto = mapPPISurveyToQuestionGroupDto(survey);
        questionGroupId = createQuestionGroup(questionGroupDto, survey);
        Integer eventSourceId = getEventSourceId(questionGroupDto);
        if (migratePPISurveyResponses(survey, questionGroupId, eventSourceId)) {
            /* todo - we don't want to remove surveys */
            getSession().getTransaction().commit();
            logger.info(format("Completed migration for survey '%s' with ID %s", survey.getName(),
                    survey.getSurveyId()));
        }
    } catch (Exception e) {
        logger.error(format("Unable to remove survey '%s' with ID %s", survey.getName(), survey.getSurveyId()),
                e);
    }

    return questionGroupId;
}