If you see stateful session beans and stateless session beans, you will first think of them here.StatusWhat does it mean?
1,Stateful)
The status of each client can be maintained between different method calls.
The connection with the client must be maintained, so the overhead is higher.
It can also be understood as stateful because it has the storage capability, that is, at least one attribute to identify its current status, for example:
public class Stateful {private String name;public Stateful(String name){this.name = name;}public String Hello(){return "hello " + this.name;}public static void main(String[] args) {Stateful stateless1 = new Stateful("chain");System.out.println(stateless1.Hello());Stateful stateless2 = new Stateful("world");System.out.println(stateless2.Hello());}}
Output result:
hello chainhello world
Note: stateful Session Bean, each user has its own unique instance. During the user's lifetime, bean maintains user information, that is, "stateful "; once the user dies (the call ends or the instance ends), the life cycle of the bean ends. That is, each user will initially get an initial bean.
2,Stateless)
No status is retained between different method calls.
Transaction processing must end in a method, which occupies less resources and can be shared.
Stateless literally means that there is no bean that can identify its current state attribute, for example:
public class Stateless {public Stateless(){}public String Hello(){return "hello world";}public static void main(String[] args) {Stateless stateless1 = new Stateless();System.out.println(stateless1.Hello());Stateless stateless2 = new Stateless();System.out.println(stateless2.Hello());}}
Output result:
hello worldhello world
The two instance objects generated are stateless. for the client, they are no different (but stateless1! = Stateless2), so the same stateless Session Bean instance is the same and can be reused by different clients.
Stateless Session Bean: Once instantiated, the bean is added to the session pool, which can be shared by all users. Even if the user has been extinct, the bean life cycle may not end, and it may still exist in the session pool for other users to call. Because there is no specific user, the status of a user cannot be maintained, so it is called stateless bean. However, the stateless Session Bean is not stateless. If it has its own attributes (variables), these variables will be affected by all users who call it, this must be noted in practical applications.
3. Summary
Stateless session beans do not maintain any session state with the client. Each request sent to the bean must provide data for request processing at the same time. A stateful session bean can be in the opposite state, and the State can exist in multiple sessions with the customer.
Stateful beans bring about the benefits of supporting session states, but at the same time the performance cost. Stateless beans have better performance, but they do not have affinity with clients. In struts2, when action and actionform are combined into one, there is a state, because each request creates a new instance, so there is no thread security problem, and struts1 cannot do this, the action of struts1 adopts the singleton mode. Only one instance of action is used to process all requests. Therefore, be careful when using struts1, to ensure that action resources are thread-safe, it is best to adopt stateless.