Search in sources :

Example 1 with SessionHolder

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

the class OpenSessionInViewInterceptor method preHandle.

/**
	 * Open a new Hibernate {@code Session} according and bind it to the thread via the
	 * {@link TransactionSynchronizationManager}.
	 */
@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(getSessionFactory())) {
        // 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(getSessionFactory(), sessionHolder);
        AsyncRequestInterceptor asyncRequestInterceptor = new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
        asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
        asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 2 with SessionHolder

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

the class OpenSessionInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    SessionFactory sf = getSessionFactory();
    if (!TransactionSynchronizationManager.hasResource(sf)) {
        // New Session to be bound for the current method's scope...
        Session session = openSession();
        try {
            TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
            return invocation.proceed();
        } finally {
            SessionFactoryUtils.closeSession(session);
            TransactionSynchronizationManager.unbindResource(sf);
        }
    } else {
        // Pre-bound Session found -> simply proceed.
        return invocation.proceed();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 3 with SessionHolder

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

the class DhisTest method bindSession.

/**
     * Binds a Hibernate Session to the current thread.
     */
private void bindSession() {
    SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
    Session session = sessionFactory.openSession();
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
Also used : SessionFactory(org.hibernate.SessionFactory) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 4 with SessionHolder

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

the class DbmsUtils method bindSessionToThread.

public static void bindSessionToThread(SessionFactory sessionFactory) {
    Session session = sessionFactory.openSession();
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
Also used : SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 5 with SessionHolder

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

the class DbmsUtils method unbindSessionFromThread.

public static void unbindSessionFromThread(SessionFactory sessionFactory) {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
    SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
Also used : 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