What is the point of having a global variable in a session without a Bean state?

We know that a session without a Beans state does not have a state in any way. Then what is the meaning of a global variable in a session without a Bean state? Why is it not locked in the specification (to avoid unnecessary confusion)?

If there is any practical benefit from a global variable, please explain the code snippet.

+7
source share
2 answers

Quote from EJB 3.1 Specification

4.7 Beans Inactive Session

The idle bean segment is a beans session whose instances have a non-interactive state. This means that all instances of the bean are equivalent if they are not involved in servicing the method called by the client.

The term "idle" means that the instance has no state for a particular client . However, instance instance variables may contain the status of call calls caused by the client.

Examples of such state include an open database connection and an object reference for a bean.

The emphasis is on the lack of dialogue state . They may have a “different” state.


For example, I used it to check whether the load was distributed equally across all instances of the node cluster:

@Stateless(name = "DemoPingSb") @Remote(DemoPingSbIfc.class) public class DemoPingSb implements Serializable, DemoPingSbIfc { private final AtomicInteger instancePingCount = new AtomicInteger(0); private final static AtomicInteger classPingCount = new AtomicInteger(0); public DemoPingSb() { super(); } public String ping(final String s) { final int local = this.instancePingCount.incrementAndGet(); final int global = classPingCount.incrementAndGet(); System.out.println("INFO: local " + local + ", global " + global + ", s " + s); return s.toUpperCase(); } } 

and if there is sufficient load:

13: 13: 21,769 INFO [stdout] (http-localhost-127.0.0.1-8080-1) INFO: local 22, global 22, s hello
13: 13: 21,936 INFO [stdout] (http-localhost-127.0.0.1-8080-1) INFO: local 1, global 23, s hello

So, there are some special cases when this function can be useful.

Note

  • The specification speaks of instance variables; the use of static variables is not considered there. Thus, the code may be incorrect in relation to classPingCount
  • Using AtomicInteger for instancePingCount can be replaced with volatile int , because (4.10.13)

A container must ensure that only one thread can execute a session without a state or bean instance at any time.

  • A session without a bean is never passivated (4.2)
+7
source

Obviously, any response should indicate that a stateless session EJB should not contain state between method calls in the EJB.

Instead of taking a tough approach by denying instance variables in EJBs, EJB developers are allowed to define them for staging. There may be intermediate values, and internal private methods are used; instead of going through the parameters and / or creating an “immediate state object” that needs to be passed on for a short time, a developer (possibly lazy) can simply use instance variables.

The key here, which is considered a specification, is that an EJB developer should under no circumstances assume that any such field will support meaningful information through EJB calls.

0
source

All Articles