org.hibernate.Session#setHibernateFlushMode ( )源码实例Demo

下面列出了org.hibernate.Session#setHibernateFlushMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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);
        }
    }
}
 
源代码2 项目: gorm-hibernate5   文件: ClosureEventListener.java
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);
    }
}
 
源代码3 项目: dhis2-core   文件: IntegrationTestBase.java
private void bindSession()
{
    SessionFactory sessionFactory = (SessionFactory) webApplicationContext.getBean( "sessionFactory" );
    Session session = sessionFactory.openSession();
    session.setHibernateFlushMode(FlushMode.ALWAYS);
    TransactionSynchronizationManager.bindResource( sessionFactory, new SessionHolder( session ) );
}
 
protected void applyFlushMode(Session session) {
    session.setHibernateFlushMode(hibernateFlushMode);
}
 
源代码5 项目: dropwizard-jaxws   文件: UnitOfWorkInvoker.java
/**
 * @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());
}
 
源代码6 项目: aw-reporting   文件: SqlReportEntitiesPersister.java
/**
 * 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);
  }
}