Java Code Examples for org.hibernate.Session#setHibernateFlushMode()

The following examples show how to use org.hibernate.Session#setHibernateFlushMode() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public void postHandle(WebRequest request, ModelMap model) throws DataAccessException {
    SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.getResource(getSessionFactory());
    Session session = sessionHolder != null ? sessionHolder.getSession() : null;
    try {
        super.postHandle(request, model);
        FlushMode flushMode = session != null ? session.getHibernateFlushMode() : null;
        boolean isNotManual = flushMode != FlushMode.MANUAL && flushMode != FlushMode.COMMIT;
        if (session != null && isNotManual) {
            if(logger.isDebugEnabled()) {
                logger.debug("Eagerly flushing Hibernate session");
            }
            session.flush();
        }
    }
    finally {
        if (session != null) {
            session.setHibernateFlushMode(FlushMode.MANUAL);
        }
    }
}
 
Example 2
private <T> T doWithManualSession(AbstractEvent event, Closure<T> callable) {
    Session session = event.getSession();
    FlushMode current = session.getHibernateFlushMode();
    try {
        session.setHibernateFlushMode(FlushMode.MANUAL);
        return callable.call();
    } finally {
        session.setHibernateFlushMode(current);
    }
}
 
Example 3
private void bindSession()
{
    SessionFactory sessionFactory = (SessionFactory) webApplicationContext.getBean( "sessionFactory" );
    Session session = sessionFactory.openSession();
    session.setHibernateFlushMode(FlushMode.ALWAYS);
    TransactionSynchronizationManager.bindResource( sessionFactory, new SessionHolder( session ) );
}
 
Example 4
protected void applyFlushMode(Session session) {
    session.setHibernateFlushMode(hibernateFlushMode);
}
 
Example 5
/**
 * @see io.dropwizard.hibernate.UnitOfWorkAspect#configureSession()
 */
private void configureSession(Session session, UnitOfWork unitOfWork) {
    session.setDefaultReadOnly(unitOfWork.readOnly());
    session.setCacheMode(unitOfWork.cacheMode());
    session.setHibernateFlushMode(unitOfWork.flushMode());
}
 
Example 6
/**
 * Persists all the given entities into the DB configured in the {@code SessionFactory}. Specify
 * the following system properties backoff.delay
 */
@Override
@Transactional
@Retryable(
    value = {LockAcquisitionException.class},
    maxAttemptsExpression = "#{ @systemProperties['retryBackoff'] ?: 20}",
    backoff =
        @Backoff(
            delayExpression = "#{ @systemProperties['retryDelay'] ?: 100}",
            maxDelayExpression = "#{ @systemProperties['retryMaxDelay'] ?: 50000 }",
            multiplierExpression = "#{ @systemProperties['retryMultiplier'] ?: 1.5}"))
public void persistReportEntities(List<? extends Report> reportEntities) {
  int batchFlush = 0;
  Session session = sessionFactory.getCurrentSession();
  FlushMode previousFlushMode = session.getHibernateFlushMode();
  session.setHibernateFlushMode(FlushMode.MANUAL);

  try {
    for (Report report : reportEntities) {
      report.setRowId();

      session.saveOrUpdate(report);
      batchFlush++;

      if (batchFlush == config.getBatchSize()) {
        session.flush();
        session.clear();
        batchFlush = 0;
      }
    }

    if (batchFlush > 0) {
      session.flush();
      session.clear();
    }
  } catch (NonUniqueObjectException ex) {
    // Github issue 268 & 280
    //   https://github.com/googleads/aw-reporting/issues/268
    //   https://github.com/googleads/aw-reporting/issues/280
    //
    // Currently we allow specifying report definitions which do not include all primary key
    // fields. This leads to cryptic hibernate errors without providing a reasonable
    // resolution strategy.
    //
    // This fix explains where to find the list of primary key fields, but does not address
    // the underlying issue of allowing non-unique rows to be downloaded in the first place.
    //
    // Ideally we would guarantee uniqueness of rows without the user having to specify the
    // PK fields.
    // However, this would be a substantial migration for the AWReporting user base.
    // Instead, we just log a (hopefully) useful error message.
    // Also note that the error message assumes that reportEntities was not empty, because
    // otherwise the exception would not have been thrown.
    logger.error(
        "Duplicate row detected. This is most likely because your report definition does not "
            + "include the primary key fields defined in {}.setRowId(). "
            + "Please add the missing fields and try again.",
        reportEntities.get(0).getClass().getName());
    throw ex;
  } finally {
    session.setHibernateFlushMode(previousFlushMode);
  }
}