OpenSessionInViewFilter作为一个filter,会在request到达servlet之前拦截request。
在OpenSessionInViewFilter中,首先会得到当前的sessionfactory,部分源码如下:
protected SessionFactory lookupSessionFactory() {
if (logger.isDebugEnabled()) {
logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
}
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
}
其中getSessionFactoryBeanName会得到默认的"sessionFactory"。当然这个可以在OpenSessionInViewFilter中配置。
然后,根据得到的sessionFactory,去判断当前线程是否已绑定跟sessionFactory对应的session。有则取,无则生成新的session并绑定。部分源码如下:
if (isSingleSession()) {
// single session mode
if ([color=red]TransactionSynchronizationManager.hasResource(sessionFactory)) [/color]{
// Do not modify the Session: just set the participate flag.
participate = true;
}
else {
logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
Session session = getSession(sessionFactory);
[color=red]TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));[/color]
}
}
其中TransactionSynchronizationManager.bindResource,getResource是通过ThreadLocal方式绑定资源到当前线程。源码略。
这样,就会在request到达servlet之前为当前线程绑定一个session。
之后,通过servlet转发到controler--service--dao--service--jsp--controler,直到jsp渲染完毕,发回response时,再走filter的以下代码;
finally {
if (!participate) {
if (isSingleSession()) {
// single session mode
SessionHolder sessionHolder =
(SessionHolder) [color=red]TransactionSynchronizationManager.unbindResource(sessionFactory);[/color]
logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
closeSession(sessionHolder.getSession(), sessionFactory);
}
else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
}
这中间有个问题就是在hibernatetemplate中,执行完操作后,是要关闭session的。但实际上,它关闭session是有判断的,关键的部分是,如果OpenSessionInViewFilter之前向当前thread放置过session则暂时不关闭,在OpenSessionInViewFilter中再关闭,hibernatetemplate中的判断部分源码如下:
public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory) {
if (sessionFactory == null) {
return false;
}
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
return (sessionHolder != null && sessionHolder.containsSession(session));
}
在OpenSessionInViewFilter中,首先会得到当前的sessionfactory,部分源码如下:
protected SessionFactory lookupSessionFactory() {
if (logger.isDebugEnabled()) {
logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
}
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
}
其中getSessionFactoryBeanName会得到默认的"sessionFactory"。当然这个可以在OpenSessionInViewFilter中配置。
然后,根据得到的sessionFactory,去判断当前线程是否已绑定跟sessionFactory对应的session。有则取,无则生成新的session并绑定。部分源码如下:
if (isSingleSession()) {
// single session mode
if ([color=red]TransactionSynchronizationManager.hasResource(sessionFactory)) [/color]{
// Do not modify the Session: just set the participate flag.
participate = true;
}
else {
logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
Session session = getSession(sessionFactory);
[color=red]TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));[/color]
}
}
其中TransactionSynchronizationManager.bindResource,getResource是通过ThreadLocal方式绑定资源到当前线程。源码略。
这样,就会在request到达servlet之前为当前线程绑定一个session。
之后,通过servlet转发到controler--service--dao--service--jsp--controler,直到jsp渲染完毕,发回response时,再走filter的以下代码;
finally {
if (!participate) {
if (isSingleSession()) {
// single session mode
SessionHolder sessionHolder =
(SessionHolder) [color=red]TransactionSynchronizationManager.unbindResource(sessionFactory);[/color]
logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
closeSession(sessionHolder.getSession(), sessionFactory);
}
else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
}
这中间有个问题就是在hibernatetemplate中,执行完操作后,是要关闭session的。但实际上,它关闭session是有判断的,关键的部分是,如果OpenSessionInViewFilter之前向当前thread放置过session则暂时不关闭,在OpenSessionInViewFilter中再关闭,hibernatetemplate中的判断部分源码如下:
public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory) {
if (sessionFactory == null) {
return false;
}
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
return (sessionHolder != null && sessionHolder.containsSession(session));
}