Spring, Springmodules, JBPM持久化集成理解系列二
【本系列如需转载,请注明作者及出处】
本系列文章假设阅者具备以下知识:
1,ThreadLocal相关知识
2,Spring持久化,事务管理相关知识
3,Hibernate 持久化相关知识
4,Jbpm 持久化相关知识
5,Springmodules项目相关知识
6,相关J2EE知识
且具备以下材料:
1,Spring源代码
2,Hibernate源代码
3,Jbpm源代码
4,Springmodules源代码
本篇尝试探讨SpringModules集成的背后逻辑,这涉及到JbpmContext持久化的哲学。
JbpmContext持久化管理的哲学是,如果用户提供Context数据库连接,那么用户就要管理该数据库的打开与关闭。如果使用JbpmContext自己产生的持久化服务,那么该服务就要管理与持久化相关的操作。很像各人自扫门前雪,哪管他人瓦上霜。
这里首先引用Jbpm User Guide里面关于持久化的描述:
7.5. User provided stuff
You can also programmatically provide JDBC connections, hibernate sessions and hibernate session factories to jBPM.
When such a resource is provided to jBPM, it will use the provided resources rather then the configured ones.
The JbpmContext class contains some convenience methods to inject resources programmatically. For example, to provide a JDBC connectio to jBPM, use the following code:
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
Connection connection = ...;
jbpmContext.setConnection(connection);
// invoke one or more persistence operations
} finally {
jbpmContext.close();
}
the JbpmContext class has following convenience methods for providing resource programmatically:
JbpmContext.setConnection(Connection);
JbpmContext.setSession(Session);
JbpmContext.setSessionFactory(SessionFactory);
上面的文字Jbpm就声明了它只管它自己的持久化逻辑,至于别人塞过来的,对不起,那是别人的事情。我想,很多人在看到Jbpm User guide的这些文字的时候,都会想弄明白藏在这些文字背后的逻辑的。 Jbpm中所有数据库操作的入口都是JbpmContext,各种的数据库操作例如save,JbpmContext都会委派给不同的xxxSession,例如GraphSession,TaskMgmtSession等来处理。
假设这个操作是loadTaskInstance,我将整个过程就缩小到我们关心的领域,一个是连接的打开,另外一个是连接的关闭:
第一部分,Session的打开:
【位置JbpmContext】
public TaskInstance loadTaskInstance(long taskInstanceId) {
return getTaskMgmtSession().loadTaskInstance(taskInstanceId);
}
public TaskMgmtSession getTaskMgmtSession() {
PersistenceService persistenceService = getPersistenceService();
if (persistenceService==null) return null;
return (persistenceService!=null ? persistenceService.getTaskMgmtSession() : null);
这个PersistenceService就是jbpm.cfg.xml里面配置的org.jbpm.persistence.db.DbPersistenceService了,在上面getTaskMgmtSession这个方面里面PersistenceService就返回了一个带有一个Hibernate session的TaskMgmtSession了。
如下所示:
【位置DbPersistenceService】
public TaskMgmtSession getTaskMgmtSession() {
if (taskMgmtSession==null) {
Session session = getSession();
if (session!=null) {
taskMgmtSession = new TaskMgmtSession(session);
}
}
return taskMgmtSession;
}
好了,到此我们就差不多达到Jbpm管理Session的环节了,我们再进入到getSession方法,看看它到底做了什么事情:
准备工作:1)以下是会涉及的布尔变量,以及其默认值:
【位置DbPersistenceService】
Connection connection = null;
boolean mustConnectionBeClosed = false;
Transaction transaction = null;
boolean isTransactionEnabled = true;
boolean isCurrentSessionEnabled = false;
boolean isRollbackOnly = false;
Session session;
boolean mustSessionBeFlushed = false;
boolean mustSessionBeClosed = false;
准备工作2)由于DbPersistenceService只有一个Constructor,也就是
【位置DbPersistenceService】
public DbPersistenceService(DbPersistenceServiceFactory persistenceServiceFactory) {
this.persistenceServiceFactory = persistenceServiceFactory;
this.isTransactionEnabled = persistenceServiceFactory.isTransactionEnabled();
this.isCurrentSessionEnabled = persistenceServiceFactory.isCurrentSessionEnabled();
}
在这里我们就知道了,进行数据库操作所用到Session,SessionFactory就只能通过相关的Setter和Getter方法来取到了。 现在进入这个方法:
【位置DbPersistenceService】
public Session getSession() {
if ( (session==null)
&& (getSessionFactory()!=null)
) {
Connection connection = getConnection(false);
if (isCurrentSessionEnabled) {
session = getSessionFactory().getCurrentSession();
mustSessionBeClosed = false;
mustSessionBeFlushed = false;
mustConnectionBeClosed = false;
} else if (connection!=null) {
log.debug("creating hibernate session with connection "+connection);
session = getSessionFactory().openSession(connection);
mustSessionBeClosed = true;
mustSessionBeFlushed = true;
mustConnectionBeClosed = false;
} else {
log.debug("creating hibernate session");
session = getSessionFactory().openSession();
mustSessionBeClosed = true;
mustSessionBeFlushed = true;
mustConnectionBeClosed = false;
}
if (isTransactionEnabled) {
log.debug("beginning hibernate transaction");
transaction = session.beginTransaction();
}
}
return session;
}
因为注入到JbpmContext的session和sessionFactory Setter方法都是将其注入到persistenceService里面的 在上面这个方法里 1)如果用户向JbpmContext提供了Session,那么会立刻返回该session,并且不会修改mustConnectionBeClosed,mustSessionBeClosed等的值。
2)如果用户向JbpmContext提供了SessionFactory,在getConnection时将mustConnectionBeClosed设置成了false,另外使用返回的Connection创建了一个Hibernate Session,即session = getSessionFactory().openSession(connection);紧接着将mustSessionBeClosed和mustSessionBeFlushed设置成了true。
分享到:
相关推荐
spring-modules-jbpm31-0.6.jar,spring整合jbpm的jar包
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
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与jbpm的集成包(符集成方法)
jbpm-jpdl-suite-3.2GA -(5)分开压缩(共5个文件)jbpm下载 jbpm-jpdl-suite-3.2GA -(5)分开压缩(共5个文件)jbpm下载
JBPM-----PPT 工作流JBPM完整PPT文件。帮你更快速上手。
jbpm-starters-kit-3.1.2 part1 和part2 合集完整版,希望能帮助大家,等好评哦,谢谢~
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.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-jpdl-suite-3.2.GA.zip
jbpm-workitems-5.1.0.Final-sources.jar jbpm5 源码
Spring与JBPM4的集成
jbpm-bpmn2-5.1.0.Final.jar jbpm5
jbpm-jpdl-suite-3.2.GA.zip
Spring与JBoss JBPM工作流集成开发指南
jbpm-jpdl-designer资源
jbpm-flow-builder-5.1.0.Final.jar jbpm5
jbpm-eclipse插件,这个插件随jbpm4.4 一起发布,位于 jbpm-4.4/install/src/gpd 目录下,由于许多人找不到这个包,所有传上来,赚点积分。