List of usage examples for org.springframework.orm.hibernate5 SessionFactoryUtils closeSession
public static void closeSession(@Nullable Session session)
From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java
/** * 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 *//*from w w w .j a v a2s . com*/ 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.springframework.orm.hibernate5.HibernateTemplate.java
/** * 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// w w w .j a v a 2 s .c o m * @return a result object returned by the action, or {@code null} * @throws DataAccessException in case of Hibernate errors */ @SuppressWarnings("deprecation") @Nullable protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSession) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Session session = null; boolean isNew = false; try { session = obtainSessionFactory().getCurrentSession(); } catch (HibernateException ex) { logger.debug("Could not retrieve pre-bound Hibernate session", ex); } if (session == null) { session = obtainSessionFactory().openSession(); session.setFlushMode(FlushMode.MANUAL); isNew = true; } try { enableFilters(session); Session sessionToExpose = (enforceNativeSession || isExposeNativeSession() ? session : createSessionProxy(session)); return action.doInHibernate(sessionToExpose); } catch (HibernateException ex) { throw SessionFactoryUtils.convertHibernateAccessException(ex); } catch (PersistenceException ex) { if (ex.getCause() instanceof HibernateException) { throw SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause()); } throw ex; } catch (RuntimeException ex) { // Callback code threw application exception... throw ex; } finally { if (isNew) { SessionFactoryUtils.closeSession(session); } else { disableFilters(session); } } }
From source file:org.springframework.orm.hibernate5.support.AsyncRequestInterceptor.java
private void closeSession() { if (this.timeoutInProgress || this.errorInProgress) { logger.debug("Closing Hibernate Session after async request timeout/error"); SessionFactoryUtils.closeSession(this.sessionHolder.getSession()); }// w w w .j a va 2s . c om }
From source file:org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor.java
/** * Unbind the Hibernate {@code Session} from the thread and close it). * @see TransactionSynchronizationManager *//*from ww w . j a v a 2 s .com*/ @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()); } }