You get the task in 1 page. save the task instance Id in session.
Then use pSession.getTaskMgmtSession().loadTaskInstance(pTaskInstanceId) to get back the task instance.
I can get back the taskInstance object without any problems. But when we go to actually execute that task, it needs the session that created this taskInstance in the first place.
How do we put this jBPMSession object in the HTTPSession when it is not serializable? The problem is only with this JBPMSession object.
You should create a new JbpmSession object. There is no need to use the original session that created the TaskInstance. It is much like a database conncetion, you do not keep it open across multiple requests nor do you put it in the HttpSession.
I have verified this several times before. Please look at the code below.
JbpmSession session = jbpmSessionFactory.openJbpmSession();
tasks = sa.getTaskInstances (session, "davids-fl-state");
JbpmSession session1 = jbpmSessionFactory.openJbpmSession();
iter = tasks.iterator();
sa.SubmitStateEnrollmentApproval("davids-fl-state", session1, (TaskInstance)iter.next());
So, I am creating the tasks with one session and executing them with another session. I get the exception below.
Exception in thread "main" 16:37:38,873 ERROR LazyInitializationException : failed to lazily initialize a collection of role: org.jbpm.taskmgmt.def.Task.events - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.jbpm.taskmgmt.def.Task.events - no session or session was closed
IF I do not close the session as in the code above, I still get an exception
16:50:24,287 ERROR GraphSession : org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Exception in thread "main" java.lang.RuntimeException: couldn't save process instance 'org.jbpm.graph.exe.ProcessInstance@74f5a4bc'
There is quite certainly some relationship between a task and the session that created it. The task seeems to keep a reference to the session that created it. This makes the design of a web application fairly complicated due to having to store JbpmSession in HttpSession.
Thanks and regards,
Hm.. What is sa?
Effectively, a persistent object is bound to the session that loaded or saved it. To use it in a different session, you must reattach the object. See section 12.3.3 Detached objects of the Hibernate users guide for a description.
Note that an object cannot be attached to more than one session. In order for this solution to work, the original session must be closed.