Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static fields in stateless session beans

Tags:

java

static

ejb

If I have a static fields in a stateless bean :

@Stateless
@Local(SomeClass.class)
public class AccountBean implements SomeClass{

   private static final int STATIC_FIELD = 0;

   public AccountBean () {}
}

Will the STATIC_FIELD value be shared in all AccountBean instances, like in basic classes?

EDIT Mark the field as final as suggested bellow.

like image 933
Olivier J. Avatar asked Dec 22 '12 20:12

Olivier J.


People also ask

Can stateless session bean have instance variables?

Bypassing conversational state doesn't mean that a stateless session bean can't have instance variables or maintain any kind of internal state. Nothing prevents you from keeping a variable that tracks the number of times a bean instance has been called or a variable that saves data for debugging.

What are the annotation used in stateless session bean?

Annotations used in Stateless Session Bean@Stateless. @PostConstruct. @PreDestroy.

How does stateless session bean work?

Stateless Session Beans A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean's instance variables may contain a state specific to that client but only for the duration of the invocation.

What is the difference between stateful session beans and stateless session beans?

An instance of a stateful session bean has a unique identity that is assigned by the container at create time. Stateless: A stateless session bean does not maintain conversational state. Instances of a stateless session bean have no conversational state.


1 Answers

Yes, it will be shared, but only inside a single JVM of course. And its capitalization indicates that it's a constant, and should thus be final.

If it's not a constant, then it smells, doesn't respect tha Java naming conventions, and violates the EJB spec.

like image 64
JB Nizet Avatar answered Sep 22 '22 12:09

JB Nizet