Difference between stateless session and bean conditions

I knew that with state preservation, beans supports a conversational session between different calls to the instance method, but without apathy it will not. My question is suppose I have a beanless implementation as shown below

import javax.ejb.Stateful; import javax.ejb.Stateless; import com.tata.ejb3.data.HelloEJBInterface; @Stateless public class ValueEJB implements ValueEJBInterface{ private int value; @Override public int getValue() { return this.value; } @Override public void setValue(int value) { this.value = value; } } 

I have my bean client (servlet) that initiates a bean call as shown below

 @EJB(mappedName="E/ValueEJB /remote") ValueEJBInterface value; .... value.setValue(250); System.out.println(value.getValue());//This statement prints the value 250 .... 

According to my understanding, since my bean is a stateless bean, it should not be displayed with a value of 250.

private int value; - this is an instantaneous variable, if one of the methods without saving the set value, the value will expire when the method exits. But here I can get the value "250" even through my second method call. Is this a violation of the stateless concept? Am I missing something?

+6
source share
3 answers

The difference between the behavior of a Stateful v Stateless bean when calling different methods.


STATEFUL: When calling different methods in a Stateful Bean, different instances of the bean are created.

 ((MyStatefulBeanRemote) ctx.lookup("ejb/MyStatefulBean")).doingStatefulThing(); ((MyStatefulBeanRemote) ctx.lookup("ejb/MyStatefulBean")).doingNothingStatefulThing(); ***Output: Note the creation of separate objects.*** INFO: Calling doingStatefulThing...com.myeclipseide.ejb3.stateful.** MyStatefulBean@2fe395 ** INFO: Calling doingNothingStatefulThing...com.myeclipseide.ejb3.stateful.** MyStatefulBean@81cfcb ** 

STATIC: When calling different methods in a Stateless Bean, the beans are combined, so no new bean instances are created.

 ((MyStatelessBeanRemote) ctx.lookup("ejb/MyStatelessBean")).doSomething(); ((MyStatelessBeanRemote) ctx.lookup("ejb/MyStatelessBean")).doNothing(); ***Output: Note the reuse of he bean object.*** INFO: Doing something ...com.myeclipseide.ejb3.stateless.** MyBean@213b61 ** INFO: Doing Nothing ...com.myeclipseide.ejb3.stateless.** MyBean@213b61 ** 
+5
source

An interesting question, and in principle you are absolutely right. I did some research and general advice: "Expect your bean to forget everything after every method call ..." ( p. 81 ). In addition, according to this resource, the algorithm responsible for maintaining the session state without the Beans state is specific to the container / provider. Thus, the container can choose to destroy, recreate, or clean up the instance after the method is executed.

You can create a multi-threaded test and see how it behaves with clients with multiple files.

+2
source

There is no violation of any concept. This is due to the fact that the same bean is selected by the container from the pool to serve another request.

Stateless beans are combined and therefore have a performance advantage over statefull beans, their main goal is to process without saving any state.

Sensitive or user data should not be stored in instance variables without beans state. They should be widely used to process data without any consideration of the state.

May refer here to their life events handled by the container.

+2
source

Source: https://habr.com/ru/post/907293/


All Articles