java - EJB instance variables thread safety -
as per understanding, stateless ejb thread safety comes fact that
concurrent requests same slsb served different intances of particular bean, each 1 own instance variables.
for example if stateless ejb has instance variable, such int counter, each pooled ejb using different counter variable.
does same apply injected variables in following example:
@stateless public class user implements userhomelocal, userhomeremote { @persistencecontext(name="j2ee") private entitymanager manager; }
more generally: there case in wich pooled beans can share instance variables result of dependency injection?
ejb spec says
the container serializes calls each stateful , stateless session bean instance. containers support many instances of session bean executing concurrently; however, each instance sees serialized sequence of method calls. therefore, stateful or stateless session bean not have coded reentrant
that means thread-safe, default. no efforts needed. @ same time note stateless beans supposed "state-less".
it not idea stateless bean have mutable state. recipe disaster. if need variable shared amongst instances, needs static variable. ejb specification restricts use of static mutable variables (class level variable counter track requests instances). "read or write nonfinal static fields" restricted.
so make pooled beans share instance variable, variable should static, , final. if looking implementation reasons, may check out singleton beans. once create singleton bean, can inject in session bean. not sure if worth pain.
and yes,the entity manager or such java ee objects (e.g. references java persistence entity managers or stateful session beans) shared , guaranteed thread-safe default (under stateless)
Comments
Post a Comment