论坛首页 Java企业应用论坛

Spring, Springmodules, JBPM持久化集成--优雅的背后(Part2)

浏览 3253 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-03-04  

Spring, Springmodules, JBPM持久化集成理解系列二

【本系列如需转载,请注明作者及出处】 

 

 

本系列文章假设阅者具备以下知识:
1,ThreadLocal相关知识
2,Spring持久化,事务管理相关知识
3,Hibernate 持久化相关知识
4,Jbpm 持久化相关知识
5,Springmodules项目相关知识
6,相关J2EE知识


且具备以下材料:
1,Spring源代码
2,Hibernate源代码
3,Jbpm源代码
4,Springmodules源代码

 

 

第二部分,Session的关闭: 到了这里,我们就到了验证的最后一部分了,JbpmContext的关闭了。


【位置JbpmContext】

public void close() {
    log.debug("closing JbpmContext");
    try {
      if (services!=null) {
        try {
          autoSave();
        } finally {
          services.close();
        }
      }
    } finally {
      if (jbpmConfiguration!=null) {
        jbpmConfiguration.jbpmContextClosed(this);
      }
    }
}
 

也就是说,JbpmContext关闭的时候会产生自动保存,最后关闭services,现在摘取DbPersistenceService的close里面我们关心的逻辑:



【位置DbPersistenceService】

if (mustSessionBeClosed) {
      try {
        if(session.isOpen()) {
          log.debug("closing hibernate session");
          session.close();
        } else {
          log.warn("hibernate session was already closed");
        }
      } catch (Exception e) {
        throw new JbpmPersistenceException("couldn't close hibernate session", e);
      }
    }

    if (mustConnectionBeClosed) {
      try {
        if ( (connection!=null)
            && (! connection.isClosed())
           ) {
          log.debug("closing jdbc connection");
          connection.close();
        } else {
          log.warn("jdbc connection was already closed");
        }
      } catch (Exception e) {
        throw new JbpmPersistenceException("couldn't close jdbc connection", e);
      }
    }



总结:



对于情况一,如果提供了session,mustConnectionBeClosed是false,mustSessionBeClosed也是false,所以这种情况Jbpm不会关闭Connection。


对于情况二mustConnectionBeClosed是true,mustSessionBeClosed也是false,所以Jbpm会调用Session.close(),但是关闭一个使用已有Connection打开的Session,在close时的时候是不会关闭实际的Jdbc 连接的。有兴趣的朋友可用自己继续查看相应的Hibernate代码。



SpringModules集成Jbpm,它向JbpmContext提供了Session,那么它就应该负责Session的创建和关闭了,这些事情Springmodules的核心类JbpmTemplate使用了HibernateTemplate,重用了Spring对Hibernate数据库事务的封装。也就能进一步享受Spring的事务管理和延迟加载,也就是OpenSessionInView的便利。


我后来写了一个Junit测试类来验证,附Junit测试代码:

 

package com.cabernet;

import java.util.List;

import junit.framework.Assert;

import org.jbpm.taskmgmt.exe.TaskInstance;
import org.springmodules.workflow.jbpm31.JbpmTemplate;

public class TestSetUp extends BaseTestCase
{
	protected JbpmTemplate	jbpmTemplate;

	public void test()
	{
		Assert.assertNotNull(this.jbpmTemplate);

		boolean isDebugEnabled = log.isDebugEnabled();

		if (isDebugEnabled)
		{
			log.debug("findTaskInstances");
		}
		
		List<TaskInstance> tasks = this.jbpmTemplate.findTaskInstances("bert");

		if (tasks != null)
		{
			if (isDebugEnabled)
			{
				log.debug("Tasks found, Swimlane: [" + tasks.get(0).getTask()
						.getSwimlane().getName() + "].");
			}

			return;
		}

		if (isDebugEnabled)
		{
			log.debug("No tasks found.");
		}
	}

	public JbpmTemplate getJbpmTemplate()
	{
		return jbpmTemplate;
	}

	public void setJbpmTemplate(JbpmTemplate jbpmTemplate)
	{
		this.jbpmTemplate = jbpmTemplate;
	}

}

 

上述Junit 类继承的父类BaseTestCase是AbstractDependencyInjectionSpringContextTests的子类,

 启用了与OpenSessionInView一样的数据LazyLoading和JDBC Connection延迟关闭策略。

 

附件是测试这个这个流程所发生的Log,首先是SessionFactoryUtils打开了Hibernate Session,最后是SessionFactoryUtils关闭了Hibernate Session,符合Jbpm声称的Context哲学。

2008-03-04 00:14:12 DEBUG (SessionFactoryUtils.java:690)     - Initializing deferred close of Hibernate Sessions
2008-03-04 00:14:12 DEBUG (SessionFactoryUtils.java:316)     - Opening Hibernate Session
2008-03-04 00:14:12 DEBUG (TestSetUp.java:22)     - findTaskInstances
2008-03-04 00:14:12 DEBUG (JbpmContextInfo.java:142)     - creating jbpm context with service factories '[message, scheduler, logging, persistence, authentication]'
2008-03-04 00:14:12 DEBUG (JbpmContext.java:124)     - creating JbpmContext
2008-03-04 00:14:12 DEBUG (HibernateTemplate.java:364)     - Found thread-bound Session for HibernateTemplate
2008-03-04 00:14:12 DEBUG (DbPersistenceServiceFactory.java:55)     - creating persistence service
2008-03-04 00:14:12 DEBUG (DbPersistenceService.java:303)     - injecting a session disables transaction
Hibernate: select taskinstan0_.ID_ as ID1_27_, taskinstan0_.NAME_ as NAME3_27_, taskinstan0_.DESCRIPTION_ as DESCRIPT4_27_, taskinstan0_.ACTORID_ as ACTORID5_27_, taskinstan0_.CREATE_ as CREATE6_27_, taskinstan0_.START_ as START7_27_, taskinstan0_.END_ as END8_27_, taskinstan0_.DUEDATE_ as DUEDATE9_27_, taskinstan0_.PRIORITY_ as PRIORITY10_27_, taskinstan0_.ISCANCELLED_ as ISCANCE11_27_, taskinstan0_.ISSUSPENDED_ as ISSUSPE12_27_, taskinstan0_.ISOPEN_ as ISOPEN13_27_, taskinstan0_.ISSIGNALLING_ as ISSIGNA14_27_, taskinstan0_.ISBLOCKING_ as ISBLOCKING15_27_, taskinstan0_.TASK_ as TASK16_27_, taskinstan0_.TOKEN_ as TOKEN17_27_, taskinstan0_.SWIMLANINSTANCE_ as SWIMLAN18_27_, taskinstan0_.TASKMGMTINSTANCE_ as TASKMGM19_27_ from JBPM_TASKINSTANCE taskinstan0_ where taskinstan0_.ACTORID_=? and taskinstan0_.ISSUSPENDED_<>1 and taskinstan0_.ISOPEN_=1
2008-03-04 00:14:13 DEBUG (HibernateTemplate.java:388)     - Not closing pre-bound Hibernate Session after HibernateTemplate
2008-03-04 00:14:13 DEBUG (JbpmContext.java:133)     - closing JbpmContext
2008-03-04 00:14:13 DEBUG (Services.java:210)     - closing service 'persistence': org.jbpm.persistence.db.DbPersistenceService@1ce663c
Hibernate: select task0_.ID_ as ID1_14_0_, task0_.NAME_ as NAME2_14_0_, task0_.PROCESSDEFINITION_ as PROCESSD3_14_0_, task0_.DESCRIPTION_ as DESCRIPT4_14_0_, task0_.ISBLOCKING_ as ISBLOCKING5_14_0_, task0_.ISSIGNALLING_ as ISSIGNAL6_14_0_, task0_.DUEDATE_ as DUEDATE7_14_0_, task0_.ACTORIDEXPRESSION_ as ACTORIDE8_14_0_, task0_.POOLEDACTORSEXPRESSION_ as POOLEDAC9_14_0_, task0_.TASKMGMTDEFINITION_ as TASKMGM10_14_0_, task0_.TASKNODE_ as TASKNODE11_14_0_, task0_.STARTSTATE_ as STARTSTATE12_14_0_, task0_.ASSIGNMENTDELEGATION_ as ASSIGNM13_14_0_, task0_.SWIMLANE_ as SWIMLANE14_14_0_, task0_.TASKCONTROLLER_ as TASKCON15_14_0_ from JBPM_TASK task0_ where task0_.ID_=?
Hibernate: select swimlane0_.ID_ as ID1_13_0_, swimlane0_.NAME_ as NAME2_13_0_, swimlane0_.ACTORIDEXPRESSION_ as ACTORIDE3_13_0_, swimlane0_.POOLEDACTORSEXPRESSION_ as POOLEDAC4_13_0_, swimlane0_.ASSIGNMENTDELEGATION_ as ASSIGNME5_13_0_, swimlane0_.TASKMGMTDEFINITION_ as TASKMGMT6_13_0_ from JBPM_SWIMLANE swimlane0_ where swimlane0_.ID_=?
2008-03-04 00:14:13 DEBUG (TestSetUp.java:31)     - Tasks found, Swimlane: [buyer].
2008-03-04 00:14:13 DEBUG (SessionFactoryUtils.java:714)     - Processing deferred close of Hibernate Sessions

 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics