Example usage for org.springframework.orm.hibernate5.support AsyncRequestInterceptor AsyncRequestInterceptor

List of usage examples for org.springframework.orm.hibernate5.support AsyncRequestInterceptor AsyncRequestInterceptor

Introduction

In this page you can find the example usage for org.springframework.orm.hibernate5.support AsyncRequestInterceptor AsyncRequestInterceptor.

Prototype

public AsyncRequestInterceptor(SessionFactory sessionFactory, SessionHolder sessionHolder) 

Source Link

Usage

From source file:org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor.java

/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 *//* w w w.  j  a  va 2s. c  o  m*/
@Override
public void preHandle(WebRequest request) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    if (asyncManager.hasConcurrentResult()) {
        if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
            return;
        }
    }

    if (TransactionSynchronizationManager.hasResource(obtainSessionFactory())) {
        // Do not modify the Session: just mark the request accordingly.
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {
        logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
        Session session = openSession();
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.bindResource(obtainSessionFactory(), sessionHolder);

        AsyncRequestInterceptor asyncRequestInterceptor = new AsyncRequestInterceptor(obtainSessionFactory(),
                sessionHolder);
        asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
        asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
    }
}