List of usage examples for org.hibernate Session setFlushMode
@Deprecated
void setFlushMode(FlushMode flushMode);
From source file:org.grails.orm.hibernate.cfg.GrailsHibernateUtil.java
License:Apache License
/** * Sets the target object to read-write, allowing Hibernate to dirty check it and auto-flush changes. * * @see #setObjectToReadyOnly(Object, org.hibernate.SessionFactory) * * @param target The target object// w ww .ja v a 2 s . c om * @param sessionFactory The SessionFactory instance */ public static void setObjectToReadWrite(final Object target, SessionFactory sessionFactory) { Session session = sessionFactory.getCurrentSession(); if (!canModifyReadWriteState(session, target)) { return; } SessionImplementor sessionImpl = (SessionImplementor) session; EntityEntry ee = sessionImpl.getPersistenceContext().getEntry(target); if (ee == null || ee.getStatus() != Status.READ_ONLY) { return; } Object actualTarget = target; if (target instanceof HibernateProxy) { actualTarget = ((HibernateProxy) target).getHibernateLazyInitializer().getImplementation(); } session.setReadOnly(actualTarget, false); session.setFlushMode(FlushMode.AUTO); incrementVersion(target); }
From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java
License:Apache License
/** * Execute the action specified by the given action object within a Session. * * @param action callback object that specifies the Hibernate action * @param enforceNativeSession whether to enforce exposure of the native Hibernate Session to callback code * @return a result object returned by the action, or <code>null</code> * @throws org.springframework.dao.DataAccessException in case of Hibernate errors */// w w w . j a va 2s .c o m protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSession) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Session session = getSession(); boolean existingTransaction = isSessionTransactional(session); if (existingTransaction) { LOG.debug("Found thread-bound Session for HibernateTemplate"); } FlushMode previousFlushMode = null; try { previousFlushMode = applyFlushMode(session, existingTransaction); if (shouldPassReadOnlyToHibernate()) { session.setDefaultReadOnly(true); } Session sessionToExpose = (enforceNativeSession || exposeNativeSession ? session : createSessionProxy(session)); T result = action.doInHibernate(sessionToExpose); flushIfNecessary(session, existingTransaction); return result; } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } catch (SQLException ex) { throw jdbcExceptionTranslator.translate("Hibernate-related JDBC operation", null, ex); } catch (RuntimeException ex) { // Callback code threw application exception... throw ex; } finally { if (existingTransaction) { LOG.debug("Not closing pre-bound Hibernate Session after HibernateTemplate"); if (previousFlushMode != null) { session.setFlushMode(previousFlushMode); } } else { SessionFactoryUtils.closeSession(session); } } }
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 w w w. ja v a 2s . com*/ * @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.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;//from w w w. j a va2s.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;//from w ww. j a v a 2 s. com } 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 init() { if (incNestingCount() > 1) { return;/*from w ww . j a v a 2 s . co m*/ } SessionFactory sf = getSessionFactory(); if (sf == null) { return; } if (TransactionSynchronizationManager.hasResource(sf)) { // Do not modify the Session: just set the participate flag. setParticipate(true); } else { setParticipate(false); LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor"); Session session = getSession(); GrailsHibernateUtil.enableDynamicFilterEnablerIfPresent(sf, session); session.setFlushMode(FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); } }
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 ww w . j av a2s .c om } try { super.postHandle(request, model); } finally { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(getSessionFactory()); if (sessionHolder != null) { Session session = sessionHolder.getSession(); session.setFlushMode(FlushMode.MANUAL); } } }
From source file:org.grails.orm.hibernate3.support.HibernatePersistenceContextInterceptor.java
License:Apache License
public void init() { if (incNestingCount() > 1) { return;//from w ww .j a v a2s .c om } SessionFactory sf = getSessionFactory(); if (sf == null) { return; } if (TransactionSynchronizationManager.hasResource(sf)) { // Do not modify the Session: just set the participate flag. setParticipate(true); } else { setParticipate(false); LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor"); Session session = getSession(); HibernateRuntimeUtils.enableDynamicFilterEnablerIfPresent(sf, session); session.setFlushMode(FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); } }
From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java
License:Apache License
protected void applyFlushMode(Session session) { FlushMode hibernateFlushMode = FlushMode.AUTO; switch (flushMode) { case AUTO://from ww w .j a v a 2 s . c om hibernateFlushMode = FlushMode.AUTO; break; case MANUAL: hibernateFlushMode = FlushMode.MANUAL; break; case COMMIT: hibernateFlushMode = FlushMode.COMMIT; break; case ALWAYS: hibernateFlushMode = FlushMode.ALWAYS; break; } session.setFlushMode(hibernateFlushMode); }
From source file:org.grails.orm.hibernate4.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;/*www . j a v a2s . c o m*/ } SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(getSessionFactory()); Session session = sessionHolder != null ? sessionHolder.getSession() : null; try { super.postHandle(request, model); if (session != null && flushMode != AbstractHibernateDatastore.FlushMode.MANUAL && !FlushMode.isManualFlushMode(session.getFlushMode())) { logger.debug("Eagerly flushing Hibernate session"); session.flush(); } } finally { if (session != null) { session.setFlushMode(FlushMode.MANUAL); } } }