Discussion:
[jboss-user] [jBPM] - LocalTaskService keep using one Entity Manager, Breaks when Database is Restarted
Thomas Setiabudi
2013-08-13 03:33:32 UTC
Permalink
Thomas Setiabudi [https://community.jboss.org/people/thomas.setiabudi] created the discussion

"LocalTaskService keep using one Entity Manager, Breaks when Database is Restarted"

To view the discussion, visit: https://community.jboss.org/message/832416#832416

--------------------------------------------------------------
Hi,

This is similar post to https://community.jboss.org/thread/231488?tstart=0 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 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
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/832416#832416]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Maciej Swiderski
2013-08-13 05:10:30 UTC
Permalink
Maciej Swiderski [https://community.jboss.org/people/swiderski.maciej] created the discussion

"Re: LocalTaskService keep using one Entity Manager, Breaks when Database is Restarted"

To view the discussion, visit: https://community.jboss.org/message/832422#832422

--------------------------------------------------------------
In general, TaskServiceSession object is request scoped object, meaning it shall be created and destroyed on every request. Although LocalTaskService caches it and that is the main reason why LocalTaskService is not thread safe. So to workaround the problem you would need to create LocalTaskService per request which is lightweight enough to do so and that should resolve the problem.

When it comes to ksession, as far as I can tell it does not have issue like LocalTaskService as ksession manages entity manager on command level. Every operation is a command that will be managed independently within transaction and entity managers will be closed and cleaned on transaction completion. What's you ksession strategy? Do you use single session or many sessions? if many make sure you use dedicated environment objects for every ksession.

HTH
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/832422#832422]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Thomas Setiabudi
2013-08-13 07:02:47 UTC
Permalink
Thomas Setiabudi [https://community.jboss.org/people/thomas.setiabudi] created the discussion

"Re: LocalTaskService keep using one Entity Manager, Breaks when Database is Restarted"

To view the discussion, visit: https://community.jboss.org/message/832435#832435

--------------------------------------------------------------
Hi Maciejs,

I use a single session strategy, so I created only 1 LocalTaskService too, does it mean that it is not threadsafe this way?

Should I stick to the Many Session and Many LocalTaskService to get threadsafety?


Regards,
Thomas Setiabudi
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/832435#832435]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Loading...