Search in sources :

Example 6 with SessionHolder

use of org.springframework.orm.hibernate5.SessionHolder in project spring-framework by spring-projects.

the class OpenSessionInViewFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    SessionFactory sessionFactory = lookupSessionFactory(request);
    boolean participate = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    String key = getAlreadyFilteredAttributeName();
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        // Do not modify the Session: just set the participate flag.
        participate = true;
    } else {
        boolean isFirstRequest = !isAsyncDispatch(request);
        if (isFirstRequest || !applySessionBindingInterceptor(asyncManager, key)) {
            logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
            Session session = openSession(sessionFactory);
            SessionHolder sessionHolder = new SessionHolder(session);
            TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
            AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(sessionFactory, sessionHolder);
            asyncManager.registerCallableInterceptor(key, interceptor);
            asyncManager.registerDeferredResultInterceptor(key, interceptor);
        }
    }
    try {
        filterChain.doFilter(request, response);
    } finally {
        if (!participate) {
            SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
            if (!isAsyncStarted(request)) {
                logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
                SessionFactoryUtils.closeSession(sessionHolder.getSession());
            }
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 7 with SessionHolder

use of org.springframework.orm.hibernate5.SessionHolder in project spring-framework by spring-projects.

the class OpenSessionInViewInterceptor method afterCompletion.

/**
	 * Unbind the Hibernate {@code Session} from the thread and close it).
	 * @see TransactionSynchronizationManager
	 */
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    if (!decrementParticipateCount(request)) {
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
        logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
        SessionFactoryUtils.closeSession(sessionHolder.getSession());
    }
}
Also used : SessionHolder(org.springframework.orm.hibernate5.SessionHolder)

Example 8 with SessionHolder

use of org.springframework.orm.hibernate5.SessionHolder in project dhis2-core by dhis2.

the class DhisTest method unbindSession.

/**
     * Unbinds and closes the bound Hibernate Session from the current thread.
     */
private void unbindSession() {
    SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
    SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
Also used : SessionFactory(org.hibernate.SessionFactory) SessionHolder(org.springframework.orm.hibernate5.SessionHolder)

Aggregations

SessionHolder (org.springframework.orm.hibernate5.SessionHolder)8 Session (org.hibernate.Session)5 SessionFactory (org.hibernate.SessionFactory)4 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)2