private void closeSession() { if (this.timeoutInProgress || this.errorInProgress) { logger.debug("Closing Hibernate Session after async request timeout/error"); SessionFactoryUtils.closeSession(this.sessionHolder.getSession()); } }
@Override public void afterPropertiesSet() { if (getSessionFactory() == null) { throw new IllegalArgumentException("Property 'sessionFactory' is required"); } if (this.entityInterceptor instanceof String && this.beanFactory == null) { throw new IllegalArgumentException("Property 'beanFactory' is required for 'entityInterceptorBeanName'"); } // Check for SessionFactory's DataSource. if (this.autodetectDataSource && getDataSource() == null) { DataSource sfds = SessionFactoryUtils.getDataSource(getSessionFactory()); if (sfds != null) { // Use the SessionFactory's DataSource for exposing transactions to JDBC code. if (logger.isDebugEnabled()) { logger.debug("Using DataSource [" + sfds + "] of Hibernate SessionFactory for HibernateTransactionManager"); } setDataSource(sfds); } } }
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * <p>Will automatically apply a specified SQLExceptionTranslator to a * Hibernate JDBCException, else rely on Hibernate's default translation. * @param ex the HibernateException that occurred * @return a corresponding DataAccessException * @see SessionFactoryUtils#convertHibernateAccessException */ protected DataAccessException convertHibernateAccessException(HibernateException ex) { return SessionFactoryUtils.convertHibernateAccessException(ex); }
@Override public void beforeCommit(boolean readOnly) throws DataAccessException { if (!readOnly) { Session session = getCurrentSession(); // Read-write transaction -> flush the Hibernate Session. // Further check: only flush when not FlushMode.MANUAL. if (!FlushMode.MANUAL.equals(SessionFactoryUtils.getFlushMode(session))) { SessionFactoryUtils.flush(getCurrentSession(), true); } } }
throw SessionFactoryUtils.convertHibernateAccessException(ex); throw SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause()); SessionFactoryUtils.closeSession(session);
FlushMode flushMode = SessionFactoryUtils.getFlushMode(session); if (FlushMode.MANUAL.equals(flushMode)) { session.setFlushMode(FlushMode.AUTO); SessionFactoryUtils.closeSession(session); txObject.setSessionHolder(null);
/** * Check whether write operations are allowed on the given Session. * <p>Default implementation throws an InvalidDataAccessApiUsageException in * case of {@code FlushMode.MANUAL}. Can be overridden in subclasses. * @param session current Hibernate Session * @throws InvalidDataAccessApiUsageException if write operations are not allowed * @see #setCheckWriteOperations * @see Session#getFlushMode() * @see FlushMode#MANUAL */ protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException { if (isCheckWriteOperations() && SessionFactoryUtils.getFlushMode(session).lessThan(FlushMode.COMMIT)) { throw new InvalidDataAccessApiUsageException( "Write operations are not allowed in read-only mode (FlushMode.MANUAL): "+ "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition."); } }
@Override public void flush() { SessionFactoryUtils.flush(this.session, false); }
throw SessionFactoryUtils.convertHibernateAccessException(ex); throw SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause()); SessionFactoryUtils.closeSession(session);
FlushMode flushMode = SessionFactoryUtils.getFlushMode(session); if (FlushMode.MANUAL.equals(flushMode)) { session.setFlushMode(FlushMode.AUTO); SessionFactoryUtils.closeSession(session); txObject.setSessionHolder(null);
@Override public void beforeCommit(boolean readOnly) throws DataAccessException { if (!readOnly) { Session session = getCurrentSession(); // Read-write transaction -> flush the Hibernate Session. // Further check: only flush when not FlushMode.MANUAL. if (!FlushMode.MANUAL.equals(SessionFactoryUtils.getFlushMode(session))) { SessionFactoryUtils.flush(getCurrentSession(), true); } } }
/** * Check whether write operations are allowed on the given Session. * <p>Default implementation throws an InvalidDataAccessApiUsageException in * case of {@code FlushMode.MANUAL}. Can be overridden in subclasses. * @param session current Hibernate Session * @throws InvalidDataAccessApiUsageException if write operations are not allowed * @see #setCheckWriteOperations * @see Session#getFlushMode() * @see FlushMode#MANUAL */ protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException { if (isCheckWriteOperations() && SessionFactoryUtils.getFlushMode(session).lessThan(FlushMode.COMMIT)) { throw new InvalidDataAccessApiUsageException( "Write operations are not allowed in read-only mode (FlushMode.MANUAL): "+ "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition."); } }
@Override public void flush() { SessionFactoryUtils.flush(this.session, false); }
private void closeSession() { if (this.timeoutInProgress || this.errorInProgress) { logger.debug("Closing Hibernate Session after async request timeout/error"); SessionFactoryUtils.closeSession(this.sessionHolder.getSession()); } }
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * <p>Will automatically apply a specified SQLExceptionTranslator to a * Hibernate JDBCException, else rely on Hibernate's default translation. * @param ex the HibernateException that occurred * @return a corresponding DataAccessException * @see SessionFactoryUtils#convertHibernateAccessException */ protected DataAccessException convertHibernateAccessException(HibernateException ex) { return SessionFactoryUtils.convertHibernateAccessException(ex); }
@Override public void afterPropertiesSet() { if (getSessionFactory() == null) { throw new IllegalArgumentException("Property 'sessionFactory' is required"); } if (this.entityInterceptor instanceof String && this.beanFactory == null) { throw new IllegalArgumentException("Property 'beanFactory' is required for 'entityInterceptorBeanName'"); } // Check for SessionFactory's DataSource. if (this.autodetectDataSource && getDataSource() == null) { DataSource sfds = SessionFactoryUtils.getDataSource(getSessionFactory()); if (sfds != null) { // Use the SessionFactory's DataSource for exposing transactions to JDBC code. if (logger.isDebugEnabled()) { logger.debug("Using DataSource [" + sfds + "] of Hibernate SessionFactory for HibernateTransactionManager"); } setDataSource(sfds); } } }
@Override public void beforeCommit(boolean readOnly) throws DataAccessException { if (!readOnly) { Session session = getCurrentSession(); // Read-write transaction -> flush the Hibernate Session. // Further check: only flush when not FlushMode.MANUAL. if (!FlushMode.MANUAL.equals(SessionFactoryUtils.getFlushMode(session))) { SessionFactoryUtils.flush(getCurrentSession(), true); } } }
FlushMode flushMode = SessionFactoryUtils.getFlushMode(session); if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
@Override public void flush() { SessionFactoryUtils.flush(getCurrentSession(), false); }
/** * Unbind the Hibernate {@code Session} from the thread and close it). * @see TransactionSynchronizationManager */ @Override public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException { if (!decrementParticipateCount(request)) { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(obtainSessionFactory()); logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor"); SessionFactoryUtils.closeSession(sessionHolder.getSession()); } }