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:com.byteslounge.spring.tx.MyOwnTxManager.java

License:Apache License

@Override
protected void doCleanupAfterCompletion(Object transaction) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

    // Remove the session holder from the thread.
    if (txObject.isNewSessionHolder()) {
        TransactionSynchronizationManager.unbindResource(getSessionFactory());
    }/* w w w. j  av a2  s.  com*/

    // Remove the JDBC connection holder from the thread, if exposed.
    if (getDataSource() != null) {
        TransactionSynchronizationManager.unbindResource(getDataSource());
    }

    Session session = txObject.getSessionHolder().getSession();
    if (this.prepareConnection && session.isConnected() && isSameConnectionForEntireSession(session)) {
        // We're running with connection release mode "on_close": We're able to reset
        // the isolation level and/or read-only flag of the JDBC Connection here.
        // Else, we need to rely on the connection pool to perform proper cleanup.
        try {
            Connection con = ((SessionImplementor) session).connection();
            DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
        } catch (HibernateException ex) {
            logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
        }
    }

    if (txObject.isNewSession()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Closing Hibernate Session [" + session + "] after transaction");
        }
        SessionFactoryUtils.closeSession(session);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
        }
        if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
            session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
        }
        if (!this.hibernateManagedSession) {
            session.disconnect();
        }
    }
    txObject.getSessionHolder().clear();
}

From source file:com.celements.payment.service.PayPalService.java

License:Open Source License

public PayPal loadPayPalObject(final String txnId) throws XWikiException {
    boolean bTransaction = true;

    PayPal payPalObj = new PayPal();
    payPalObj.setTxn_id(txnId);/*from ww w.  j a va  2  s .  c  o  m*/

    getStore().checkHibernate(getContext());

    SessionFactory sfactory = getStore().injectCustomMappingsInSessionFactory(getContext());
    bTransaction = bTransaction && getStore().beginTransaction(sfactory, false, getContext());
    Session session = getStore().getSession(getContext());
    session.setFlushMode(FlushMode.MANUAL);

    try {
        session.load(payPalObj, payPalObj.getTxn_id());
    } catch (ObjectNotFoundException exp) {
        LOGGER.debug("no paypal object for txn_id [" + payPalObj.getTxn_id() + "] in" + " database ["
                + getContext().getDatabase() + "] found.");
        // No paypall object in store
    }
    LOGGER.trace("successfully loaded paypal object for txn_id [" + payPalObj.getTxn_id() + "] in database ["
            + getContext().getDatabase() + "] :" + payPalObj.getOrigMessage());
    return payPalObj;
}

From source file:com.celements.payment.service.PostFinanceService.java

License:Open Source License

public PostFinance loadPostFinanceObject(final String txnId) throws XWikiException {
    boolean bTransaction = true;

    PostFinance postFinanceObj = new PostFinance();
    postFinanceObj.setTxn_id(txnId);//from   w w  w .j a  va 2  s.c o  m

    getStore().checkHibernate(getContext());

    SessionFactory sfactory = getStore().injectCustomMappingsInSessionFactory(getContext());
    bTransaction = bTransaction && getStore().beginTransaction(sfactory, false, getContext());
    Session session = getStore().getSession(getContext());
    session.setFlushMode(FlushMode.MANUAL);

    try {
        session.load(postFinanceObj, postFinanceObj.getTxn_id());
    } catch (ObjectNotFoundException exp) {
        LOGGER.debug("no PostFinance object for txn_id [" + postFinanceObj.getTxn_id() + "] in" + " database ["
                + getContext().getDatabase() + "] found.");
        // No PostFinancel object in store
    }
    LOGGER.trace("successfully loaded PostFinance object for txn_id [" + postFinanceObj.getTxn_id()
            + "] in database [" + getContext().getDatabase() + "] :" + postFinanceObj.getOrigMessage());
    return postFinanceObj;
}

From source file:com.creative.dao.repository.GenericBatchDaoImpl.java

License:Apache License

@Override
public int executeUpdate(Query query) {
    Session session = sessionFactory.getCurrentSession();
    session.setCacheMode(CacheMode.IGNORE);
    session.setFlushMode(FlushMode.MANUAL);
    int rows = query.executeUpdate();
    session.flush();/*from w w w  . j  av a  2s  .  c  o m*/

    logger.info("Updated rows  " + rows + " for " + query.getQueryString());
    return rows;
}

From source file:com.creative.dao.repository.GenericBatchDaoImpl.java

License:Apache License

private <T> int executeBatch(BatchType batchType, List<T> list) {
    Session session = sessionFactory.getCurrentSession();
    session.setCacheMode(CacheMode.IGNORE);
    session.setFlushMode(FlushMode.MANUAL);
    logger.info("Executing  Batch of size :" + list.size() + " given batch size is:" + batchSize);

    for (int i = 0; i < list.size(); i++) {
        switch (batchType) {
        case BATCH_INSERT:
            session.save(list.get(i));/*from   w  ww .j  av a 2 s .  c om*/
            break;
        case BATCH_DELETE:
            session.delete(list.get(i));
            break;
        case BATCH_INSERT_OR_UPDATE:
            session.saveOrUpdate(list.get(i));
        default:
            // nothing;
        }
        if (i > 0 && i % batchSize == 0) {
            logger.info("Flushing and clearing the cache" + " after row number :" + i);
            session.flush();
            session.clear();
        }

    }
    session.flush();
    return list.size();
}

From source file:com.denksoft.springstarter.util.HibernateFilter.java

License:Open Source License

@Override
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    //        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
    Session session = super.getSession(sessionFactory);
    //set the FlushMode to auto in order to save objects.
    session.setFlushMode(FlushMode.AUTO);
    return session;
}

From source file:com.evolveum.midpoint.repo.sql.helpers.BaseHelper.java

License:Apache License

public Session beginTransaction(boolean readOnly) {
    Session session = getSessionFactory().openSession();
    session.beginTransaction();// w  ww  . jav  a  2  s  .  c o m

    if (getConfiguration().getTransactionIsolation() == TransactionIsolation.SNAPSHOT) {
        LOGGER.trace("Setting transaction isolation level SNAPSHOT.");
        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                connection.createStatement().execute("SET TRANSACTION ISOLATION LEVEL SNAPSHOT");
            }
        });
    }

    if (readOnly) {
        // we don't want to flush changes during readonly transactions (they should never occur,
        // but if they occur transaction commit would still fail)
        session.setFlushMode(FlushMode.MANUAL);

        LOGGER.trace("Marking transaction as read only.");
        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                connection.createStatement().execute("SET TRANSACTION READ ONLY");
            }
        });
    }
    return session;
}

From source file:com.evolveum.midpoint.repo.sql.SqlBaseService.java

License:Apache License

protected Session beginTransaction(boolean readOnly) {
    Session session = getSessionFactory().openSession();
    session.beginTransaction();/*w w w  .  j a  va 2 s . com*/

    if (getConfiguration().getTransactionIsolation() == TransactionIsolation.SNAPSHOT) {
        LOGGER.trace("Setting transaction isolation level SNAPSHOT.");
        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                connection.createStatement().execute("SET TRANSACTION ISOLATION LEVEL SNAPSHOT");
            }
        });
    }

    if (readOnly) {
        // we don't want to flush changes during readonly transactions (they should never occur,
        // but if they occur transaction commit would still fail)
        session.setFlushMode(FlushMode.MANUAL);

        LOGGER.trace("Marking transaction as read only.");
        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                connection.createStatement().execute("SET TRANSACTION READ ONLY");
            }
        });
    }
    return session;
}

From source file:com.ferrishibernatesupport.saveferris.dao.impl.DefaultRepository.java

License:Open Source License

@Override
public Session getSession() {
    Session currentSession = this.sessionFactory.getCurrentSession();
    currentSession.setFlushMode(FlushMode.COMMIT);
    return currentSession;
}

From source file:com.fiveamsolutions.nci.commons.util.HibernateHelper.java

License:Open Source 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)
 * @param sf the session factory.//ww w.j ava 2s. c o  m
 */
public void openAndBindSession(SessionFactory sf) {
    SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sf;
    org.hibernate.classic.Session currentSession = sessionFactoryImplementor.openSession(null, true, false,
            ConnectionReleaseMode.AFTER_STATEMENT);
    currentSession.setFlushMode(FlushMode.COMMIT);
    ManagedSessionContext.bind(currentSession);
}