Hi,
This is similar post to https://community.jboss.org/thread/231488?tstart=0
Only that it discuss about LocalTaskService here
I use JBPM5.4.Final, and Drools-Spring 5.4.Final in a Web Application.
I create Stateful Knowledge Session and LocalTaskService in the Spring config file, they are created once for the rest of the web application life.
Everything works fine, until the Database Server, MS SQL Server 2008, is restarted.
Which then break the Stateful Knowledge Session and LocalTaskService.
After looking at the LocalTaskService code here
https://github.com/droolsjbpm/jbpm/blob/5.4.x/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/task/service/local/LocalTaskService.java#L52-L55
Local Task Service has TaskServiceSession and then TaskServiceSession has TaskPersistenceManager which contain an EntityManager.
The problem is this EntityManager is acquired from EntityManagerFactory just exactly once.
when the database connection is no longer valid (Because of Database Server restart), it will still use the same EntityManager which will of course throw exception.
My question is, is it a bug? or I use these LocalTaskService Wrongly?
Currently, to keep my application running, I create another class that implements TaskService, and have all the code just like LocalTaskService, the difference is I add a check before doing anything about the task.
This is a sample code:
public void addTask(Task task, ContentData content) {
checkEntityManagerFactory();
session.addTask(task, content);
}
and the checkEntityManagerFactory() implementation:
public void checkEntityManagerFactory() {
if (session != null) {
try {
Query query = session.getTaskPersistenceManager()
.createNewQuery("SELECT 1 FROM Task WHERE id = 0 ");
query.getResultList();
} catch (Throwable e) {
session = service.createSession();
}
} else {
session = service.createSession();
}
}
execute simple select query, when that query throws exception, that means its time to get new EntityManager which in this case is create new TaskServiceSession.
Is it the correct way to do this?
Will this issue fixed in JBPM 6 ?
Any help is appreciated.
Regards,
Thomas Setiabudi