在处理删除参数的过程中,发现报org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role: org.jbpm.pvm.internal.model.ExecutionImpl.variables, no session or session was closed
代码如下:
ExecutionService executionService = processEngine.getExecutionService();
ProcessInstance executionService.createProcessInstanceQuery().processDefinitionId(processInstanceId).list();
((ExecutionImpl)processInstance2).removeVariable("aa");//删除变量aa时报错。因为Variable设置是延迟加载解决方式一:如果采用的是BS开发模式可以在web.xml中配制OpenSessionInViewFilter(注意:必须写在struts的配置下面)<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>在application.xml中加入
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy />
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* org.jbpm..*.*(..))"
advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* com.jihong.jbpm.api.JbpmTemplate.*(..))"
advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="pagedQuery*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="remove*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
解决方式二:通过processEngine的execute方式可以将一段代码放到一个Hibernate的事务里执行这样就可以避免延迟加载。
public class RemoveVarCommand implements Command<Void> {
private String processInstanceId;
private String varKey;
public RemoveVarCommand(String processInstanceId,String varKey){
this.processInstanceId=processInstanceId;
this.varKey=varKey;
}
@Override
public Void execute(Environment environment) throws Exception {
ExecutionService executionService = environment.get(ExecutionService.class);
ProcessInstance processInstance= executionService.findProcessInstanceById(processInstanceId);
((ExecutionImpl)processInstance).removeVariable(varKey);
return null;
}
}
processEngine.execute(new RemoveVarCommand(processInstance.getId(), "aa"));
每个命令都是一个独立的事务操作,每个excuete方法都被一个hibernate事务所包含,jbpm推荐使用这种方式扩展相关的功能。