ServiceTask field injection and thread safety

Hi,

I am reading through the ServiceTask documentation about field injection and thread safety, and there seems to be a contradiction in what is written.

At Open Source it says:

Note: there will be only one instance of the Java class created for the serviceTask on which it is defined. All process instances share the same class instance that will be used to call execute(DelegateExecution). This means that the class must not use any member variables and must be thread-safe, as it can be executed simultaneously from different threads. This also influences the way Field injection is handled.

and then later at Open Source it says that:

With the flowable:class attribute, using field injection is always thread safe. For each service task that references a certain class, a new instance will be instantiated and fields will be injected once when the instance is created. Reusing the same class multiple times in different tasks or process definitions is no problem.

If I have not misunderstood something it seems to say both that a single instance is created and a new instance is created every time.
Is there a mistake here, or what is the actual behaviour for the flowable:class attribute?

Thanks,
Paul

I can see how the docs could be improved here, because it’s quite subtle (but correct nonetheless):

When using the class attribute, the instance will be cached, however when referencing the same class in two different service tasks there will be two instances. Injection works in this case, as the injection is defined on the service task.

For a delegateExpression this is different. The same expression can return the same instance and there can be race conditions.

So if you read the second paragraph with that in mind, the docs are correct … but maybe the explanation could be better. Suggestions are welcome :wink:

Thanks for the clarification - I understand it better now.

Regards,
Paul