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);
}
}
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();
}
}
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));
}
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));
}
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());
}
Aggregations