`
ginge
  • 浏览: 211879 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

    博客分类:
  • JBPM
阅读更多

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

 

 

分享到:
评论

相关推荐

    spring-modules-jbpm31-0.6.jar

    spring-modules-jbpm31-0.6.jar,spring整合jbpm的jar包

    jbpm-starters-kit-3.1.2.part2.rar

    jbpm-starters-kit-3.1.2插件,jbpm-starters-kit-3.1.2插件,jbpm-starters-kit-3.1.2插件,jbpm-starters-kit-3.1.2插件,jbpm-starters-kit-3.1.2插件,jbpm-starters-kit-3.1.2插件,共两卷

    Spring-Jbpm-JSF-Example.pdf

    标题与描述提到的“Spring-Jbpm-JSF-Example.pdf”是一个示例项目,展示了如何将Spring框架、Jbpm工作流引擎和JSF(JavaServer Faces)技术集成在一起。此项目通过一个简单的应用实例,演示了如何运行基本的工作流程...

    jbpm-starters-kit-3.1.2.part1

    jbpm-starters-kit-3.1.2.part1 和 jbpm-starters-kit-3.1.2.part2 是两个分卷压缩文件,它们共同组成一个完整的jbpm开发入门套件。jbpm(Java Business Process Management)是一个开源的工作流管理系统,用于实现...

    jbpm-starters-kit-3.1.2.part1_part2

    jbpm-starters-kit-3.1.2.part1_part2 是一个分卷压缩包,它包含了jbpm-starters-kit的3.1.2版本。jbpm(Business Process Management)是开源的工作流管理系统,用于实现业务流程自动化。这个压缩包很可能包含源...

    jbpm下载 jbpm-jpdl-suite-3.2GA -(5)分开压缩(共5个文件)

    jbpm-jpdl-suite-3.2GA -(5)分开压缩(共5个文件)jbpm下载 jbpm-jpdl-suite-3.2GA -(5)分开压缩(共5个文件)jbpm下载

    jbpm-starters-kit-3.1.2.part1.rar

    jbpm-starters-kit-3.1.2.插件,jbpm-starters-kit-3.1.2.插件,jbpm-starters-kit-3.1.2.插件,jbpm-starters-kit-3.1.2.插件,jbpm-starters-kit-3.1.2.插件,jbpm-starters-kit-3.1.2.插件,jbpm-starters-kit-...

    Spring与JBPM4的集成.docx

    ProcessEngineContext是指JBPM4的流程引擎上下文,Hibbernate Session Factory是指JBPM4的持久化机制,Transaction Context是指JBPM4的事务管理机制,Spring Context是指JBPM4中的Spring框架上下文。这些机制的集成...

    jbpm-jpdl-suite-3.2.3安装配置.doc

    2. **jbpm-jpdl-suite-3.2.3下载与解压**: - 从官方网址http://www.jboss.org/jbossjbpm/获取jbpm-jpdl-suite-3.2.3.zip,解压后得到jbpm-jpdl-3.2.3目录。 3. **MySQL数据库配置**: - 启动MySQL服务器,并以...

    JBPM-----PPT

    - **持久化机制**:介绍JBPM如何利用数据库存储流程实例和变量,确保数据安全和流程恢复。 4. **平台特性:JBPM_(4)_Platform.ppt** - **jbpm-gwt-console**:一个基于GWT的Web管理控制台,用于监控和管理流程...

    jbpm-jpdl-suite-3.2.3的安装配置

    2. **jbpm-jpdl-designer-3.1.2.zip**:这是一个基于Eclipse的图形化流程设计工具,支持流程的可视化定制,避免直接编辑XML文件。同时,它还具有流程定义上传功能,方便将新定义的流程发布到jPDL系统。 3. **jbpm-...

    jbpm-bpmn2-5.1.0.Final.jar

    jbpm-bpmn2-5.1.0.Final.jar jbpm5

    spring与jbpm的集成包(符集成方法)

    Spring 和 JBPM 集成是一项关键的技术任务,它允许开发者在基于 Spring 的应用程序中无缝地集成业务流程管理(BPM)功能。JBPM 是一个开源的 BPM 解决方案,提供工作流服务、流程建模、执行和监控等功能。下面我们将...

    jbpm-jpdl-designer-site-3.1.7.zip

    jbpm-jpdl-designer-site-3.1.7.zip jbpm-jpdl-designer-site-3.1.7.zip jbpm-jpdl-designer-site-3.1.7.zip

    jbpm-jpdl-suite-3.2.3.zip

    jbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3.zipjbpm-jpdl-suite-3.2.3....

    jbpm的入门1--简单实例

    总的来说,jbpm是一个强大的工具,它使得业务流程的管理和自动化变得简单。通过这个简单的入门实例,你将了解到如何设计、部署和运行一个基本的jbpm流程。随着深入学习,你可以利用jbpm构建复杂的业务流程系统,适应...

    ssh-jbpm.rar_jbpm stru_jbpm-web-test_ssh_struts2_struts2 jbpm

    SSH是一个常见的Java企业级开发组合,Spring提供了依赖注入(DI)和面向切面编程(AOP),Hibernate则负责数据库持久化,而Struts2作为前端控制器,处理用户请求。在"ssh-jbpm.rar"中,很可能有这些技术的整合示例,...

    jbpm-4.3-src.rar_bdf-jbpm4 src _jboss 4.3_jboss jbpm4.3_jbpm-4.3

    jbpm-4.3-src.rar_bdf-jbpm4 src _jboss 4.3_jboss jbpm4.3_jbpm-4.3 这个压缩包文件包含的是JBOSS jBPM4.3的源代码,这是一个用于业务流程管理(BPM)的开源框架。jBPM是一个强大的工具,它提供了流程定义、执行和...

    jbpm-jpdl-suite-3.2.GA.zip-6

    jbpm-jpdl-suite-3.2.GA.zip

Global site tag (gtag.js) - Google Analytics