List of usage examples for org.hibernate FlushMode MANUAL
FlushMode MANUAL
To view the source code for org.hibernate FlushMode MANUAL.
Click Source Link
From source file:org.generationcp.middleware.operation.saver.WorkbookSaver.java
License:Open Source License
public void createStocksIfNecessary(final int datasetId, final Workbook workbook, final VariableTypeList effectVariables, final List<String> trialHeaders) { final Map<String, Integer> stockMap = this.getStockModelBuilder().getStockMapForDataset(datasetId); List<Integer> variableIndexesList = new ArrayList<>(); // we get the indexes so that in the next rows we dont need to compare // anymore per row if (workbook.getObservations() != null && !workbook.getObservations().isEmpty()) { final MeasurementRow row = workbook.getObservations().get(0); variableIndexesList = this.getVariableListTransformer().transformStockIndexes(row, effectVariables, trialHeaders);//from w ww .ja v a 2s . c o m } if (workbook.getObservations() != null) { final Session activeSession = this.getActiveSession(); final FlushMode existingFlushMode = activeSession.getFlushMode(); activeSession.setFlushMode(FlushMode.MANUAL); try { for (final MeasurementRow row : workbook.getObservations()) { final VariableList stock = this.getVariableListTransformer() .transformStockOptimize(variableIndexesList, row, effectVariables, trialHeaders); final String stockFactor = this.getStockFactor(stock); Integer stockId = stockMap.get(stockFactor); if (stockId == null) { stockId = this.getStockSaver().saveStock(stock); stockMap.put(stockFactor, stockId); } else { this.getStockSaver().saveOrUpdateStock(stock, stockId); } row.setStockId(stockId); } activeSession.flush(); } finally { if (existingFlushMode != null) { activeSession.setFlushMode(existingFlushMode); } } } }
From source file:org.generationcp.middleware.operation.saver.WorkbookSaver.java
License:Open Source License
private void createMeasurementEffectExperiments(final CropType crop, final int datasetId, final VariableTypeList effectVariables, final List<MeasurementRow> observations, final List<String> trialHeaders) { final TimerWatch watch = new TimerWatch("saving stocks and measurement effect data (total)"); final TimerWatch rowWatch = new TimerWatch("for each row"); // observation values start at row 2 int i = 2;//from ww w . j a va 2s . c om final ExperimentValuesTransformer experimentValuesTransformer = this.getExperimentValuesTransformer(); final ExperimentModelSaver experimentModelSaver = this.getExperimentModelSaver(); Map<Integer, PhenotypeExceptionDto> exceptions = null; final Session activeSession = this.getActiveSession(); final FlushMode existingFlushMode = activeSession.getFlushMode(); try { activeSession.setFlushMode(FlushMode.MANUAL); if (observations != null) { for (final MeasurementRow row : observations) { rowWatch.restart("saving row " + i++); final ExperimentValues experimentValues = experimentValuesTransformer.transform(row, effectVariables, trialHeaders); try { experimentModelSaver.addExperiment(crop, datasetId, ExperimentType.PLOT, experimentValues); } catch (final PhenotypeException e) { WorkbookSaver.LOG.error(e.getMessage(), e); if (exceptions == null) { exceptions = e.getExceptions(); } else { for (final Integer standardVariableId : e.getExceptions().keySet()) { final PhenotypeExceptionDto exception = e.getExceptions().get(standardVariableId); if (exceptions.get(standardVariableId) == null) { // add exception exceptions.put(standardVariableId, exception); } else { // add invalid values to the existing map of // exceptions for each phenotype for (final String invalidValue : exception.getInvalidValues()) { exceptions.get(standardVariableId).getInvalidValues().add(invalidValue); } } } } } } } activeSession.flush(); } finally { if (existingFlushMode != null) { activeSession.setFlushMode(existingFlushMode); } } rowWatch.stop(); watch.stop(); if (exceptions != null) { throw new PhenotypeException(exceptions); } }
From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java
License:Apache License
/** * Apply the flush mode that's been specified for this accessor to the given Session. * * @param session the current Hibernate Session * @param existingTransaction if executing within an existing transaction * @return the previous flush mode to restore after the operation, or <code>null</code> if none * @see #setFlushMode/*from ww w. ja v a2 s. c o m*/ * @see org.hibernate.Session#setFlushMode */ protected FlushMode applyFlushMode(Session session, boolean existingTransaction) { if (isApplyFlushModeOnlyToNonExistingTransactions() && existingTransaction) { return null; } if (getFlushMode() == FLUSH_NEVER) { if (existingTransaction) { FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session); if (!previousFlushMode.lessThan(FlushMode.COMMIT)) { session.setFlushMode(FlushMode.MANUAL); return previousFlushMode; } } else { session.setFlushMode(FlushMode.MANUAL); } } else if (getFlushMode() == FLUSH_EAGER) { if (existingTransaction) { FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session); if (!previousFlushMode.equals(FlushMode.AUTO)) { session.setFlushMode(FlushMode.AUTO); return previousFlushMode; } } else { // rely on default FlushMode.AUTO } } else if (getFlushMode() == FLUSH_COMMIT) { if (existingTransaction) { FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session); if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) { session.setFlushMode(FlushMode.COMMIT); return previousFlushMode; } } else { session.setFlushMode(FlushMode.COMMIT); } } else if (getFlushMode() == FLUSH_ALWAYS) { if (existingTransaction) { FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session); if (!previousFlushMode.equals(FlushMode.ALWAYS)) { session.setFlushMode(FlushMode.ALWAYS); return previousFlushMode; } } else { session.setFlushMode(FlushMode.ALWAYS); } } return null; }
From source file:org.grails.orm.hibernate.GrailsSessionContext.java
License:Apache License
/** * Retrieve the Spring-managed Session for the current thread, if any. *//* w w w. j a v a2 s . com*/ public Session currentSession() throws HibernateException { Object value = TransactionSynchronizationManager.getResource(sessionFactory); if (value instanceof Session) { return (Session) value; } if (value instanceof SessionHolder) { SessionHolder sessionHolder = (SessionHolder) value; Session session = sessionHolder.getSession(); if (TransactionSynchronizationManager.isSynchronizationActive() && !sessionHolder.isSynchronizedWithTransaction()) { TransactionSynchronizationManager .registerSynchronization(createSpringSessionSynchronization(sessionHolder)); sessionHolder.setSynchronizedWithTransaction(true); // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session // with FlushMode.MANUAL, which needs to allow flushing within the transaction. FlushMode flushMode = HibernateVersionSupport.getFlushMode(session); if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { HibernateVersionSupport.setFlushMode(session, FlushMode.AUTO); sessionHolder.setPreviousFlushMode(flushMode); } } return session; } if (jtaSessionContext != null) { Session session = jtaSessionContext.currentSession(); if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager .registerSynchronization(createSpringFlushSynchronization(session)); } return session; } if (allowCreate) { // be consistent with older HibernateTemplate behavior return createSession(value); } throw new HibernateException("No Session found for current thread"); }
From source file:org.grails.orm.hibernate.support.GrailsOpenSessionInViewInterceptor.java
License:Apache License
protected void applyFlushMode(Session session) { FlushMode hibernateFlushMode = FlushMode.AUTO; switch (getFlushMode()) { case GrailsHibernateTemplate.FLUSH_EAGER: case GrailsHibernateTemplate.FLUSH_AUTO: hibernateFlushMode = FlushMode.AUTO; break;/* ww w . jav a 2s. c o m*/ case GrailsHibernateTemplate.FLUSH_NEVER: hibernateFlushMode = FlushMode.MANUAL; break; case GrailsHibernateTemplate.FLUSH_COMMIT: hibernateFlushMode = FlushMode.COMMIT; break; case GrailsHibernateTemplate.FLUSH_ALWAYS: hibernateFlushMode = FlushMode.ALWAYS; break; } session.setFlushMode(hibernateFlushMode); }
From source file:org.grails.orm.hibernate.support.GrailsOpenSessionInViewInterceptor.java
License:Apache License
@Override public void postHandle(WebRequest request, ModelMap model) throws DataAccessException { final boolean isFlowRequest = request.getAttribute(IS_FLOW_REQUEST_ATTRIBUTE, WebRequest.SCOPE_REQUEST) != null; if (isFlowRequest) { return;/* ww w . j a va2 s .co m*/ } SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(getSessionFactory()); Session session = sessionHolder != null ? sessionHolder.getSession() : null; try { super.postHandle(request, model); if (session != null && getFlushMode() != GrailsHibernateTemplate.FLUSH_NEVER && !FlushMode.isManualFlushMode(session.getFlushMode())) { logger.debug("Eagerly flushing Hibernate session"); session.flush(); } } finally { if (session != null) { session.setFlushMode(FlushMode.MANUAL); } } }
From source file:org.grails.orm.hibernate.support.HibernatePersistenceContextInterceptor.java
License:Apache License
public void setReadOnly() { if (getSessionFactory() == null) return; getSession().setFlushMode(FlushMode.MANUAL); }
From source file:org.grails.orm.hibernate.support.HibernateVersionSupport.java
License:Apache License
/** * Get the native Hibernate FlushMode, adapting between Hibernate 5.0/5.1 and 5.2+. * @param session the Hibernate Session to get the flush mode from * @return the FlushMode (never {@code null}) * @since 4.3//from w w w .j a v a 2 s . c o m */ public static FlushMode getFlushMode(Session session) { if (session != null) { return (FlushMode) ReflectionUtils.invokeMethod(getFlushMode, session); } return FlushMode.MANUAL; }
From source file:org.grails.orm.hibernate.validation.UniqueConstraint.java
License:Apache License
@Override public IHibernateTemplate getHibernateTemplate(Object target) { AbstractHibernateGormInstanceApi instanceApi = (AbstractHibernateGormInstanceApi) GormEnhancer .findInstanceApi(target.getClass()); sessionFactory = instanceApi.getSessionFactory(); AbstractHibernateDatastore app = (AbstractHibernateDatastore) instanceApi.getDatastore(); return app.getHibernateTemplate(AbstractHibernateDatastore.FlushMode.MANUAL.getLevel()); }
From source file:org.grails.orm.hibernate3.support.GrailsOpenSessionInViewInterceptor.java
License:Apache License
@Override public void postHandle(WebRequest request, ModelMap model) throws DataAccessException { final boolean isFlowRequest = request.getAttribute(IS_FLOW_REQUEST_ATTRIBUTE, WebRequest.SCOPE_REQUEST) != null; if (isFlowRequest) { return;//from w w w .j ava 2 s . co m } try { super.postHandle(request, model); } finally { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(getSessionFactory()); if (sessionHolder != null) { Session session = sessionHolder.getSession(); session.setFlushMode(FlushMode.MANUAL); } } }