Spring, JBPM持久化系列--摆脱LazyInitializationException
关键字: spring 持久化Spring, Springmodules, JBPM持久化集成理解系列三
【本系列如需转载,请注明作者及出处】
不管Jbpm的持久化策略如何,要重用Spring的持久化集成服务,我们要怎么做呢?Jbpm可以是B/S架构中的一部分,也可以是C/S架构的一部分。对于B/S架构,我们都知道有OpenSessionInViewFilter,然而C/S没有“Filter”,该从何入手呢?
我们先来看看OpenSessionInViewFilter是怎样做到的,本篇不讨论它的优缺点。OpenSessionInViewFilter也是一个javax.servlet.Filter,只不过它还是BeanNameAware,ServletContextAware,InitializingBean和DisposableBean的实现。
像所有的Filter一样,Filter的入口都是
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException;
到了OpenSessionInViewFilter这里,执行的方法就是
protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException;
doFilterInternal做的事情说简单也不简单,我们来看该方法做了什么,请留意注释:
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
//1, 先把SessionFactory取出来。
SessionFactory sessionFactory = lookupSessionFactory(request);
boolean participate = false;
//2.1 如果是设置了isSingleSession为true,则整个request请求中都会只使用一个session。
if (isSingleSession()) {
// single session mode
//2.2 再看看当前线程是不是已经存在一个可用的session
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
participate = true;
}
else {
logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
//2.3 没有就创建一个
Session session = getSession(sessionFactory);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
}
else {
// deferred close mode
//3.1 如果已经延迟关闭策略已经被激活,则什么事情也不做
if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
participate = true;
}
else {
//3.2 如果为false,则激活延迟关闭session策略
SessionFactoryUtils.initDeferredClose(sessionFactory);
}
}
try {
filterChain.doFilter(request, response);
}
finally {
//4.1, filter返回后,如果在进入该filter之前延迟关闭已经被激活,该filter不负责关闭session,
if (!participate) {
if (isSingleSession()) {
// single session mode
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
closeSession(sessionHolder.getSession(), sessionFactory);
}
else {
//4.2 如延迟关闭是该filter激活的,就在此关闭session
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
}
}
isSingleSession为false时,也就是延迟关闭策略,应该有一部分会感到很奇怪了,并没有看到session的打开,这时就有必要要探讨一下了HibernateTemplate。HibernateTemplate也是经典的回调模型,它的核心是execute方法。看到这段代码之后,是不是豁然开朗呢? 原来execute里一开始就将session取出来了,执行了callback之后就开始做session的管理了。注意HibernateTemplate里默认是allowCreate = true; alwaysUseNewSession = false;
public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Session session = getSession();
/*
*省略一段代码
*/
Object result = action.doInHibernate(sessionToExpose);
/*
*省略一段代码
*/
SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
}
SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());就像它字面要表达的,如果延迟关闭策略处于激活状态,则只是将session注册到一个容器里,到某个时刻再统一关闭,例如在OpenSessionInViewFilter返回时。
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 1883 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
链接
最新评论
-
把握现在,把握未来—2008 ...
鉴于本人水平有限,翻译过程中存在错漏之处请大家指出,以免造成误导,谢谢。
-- by ginge -
Have you known enough ab ...
It does't release database connection af ...
-- by dboylx






评论排行榜