Difference between stateful Session Bean and stateless Session Bean in EJB

Source: Internet
Author: User

Session Bean: It is divided into two types: stateful Session Bean and stateless Session Bean. The main purpose is to provideProgramLogic layer;
Entity Bean: data component, mainly designed to provide program information;

The following describes the Session Bean.

 

In reality, many friends have misunderstandings about the two session beans. They think that an instance has always existed, save the status after each call, and take effect for the next call, the stateless mode is considered to be instantiated once every call without retaining user information. After careful analysis and practical testing, you will find that the facts are the opposite:
The essential difference between stateful and stateless session beans is their life cycle.
First, explain the concept to be used: User: The session bean user is actually directly calling the EJB class instance, or even a method of this instance. Different instances of the same class are different users for Session Bean.
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.
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.

 

Root Cause of difference
This is related to the running principle of stateless Session Bean and stateful Session Bean.
For stateful session beans, as long as a client sends access to a stateful Session Bean, the server creates a session bean instance that corresponds to the client, in this way, the instance corresponds to the client one by one. If the client saves the information in the bean instance, it can be used later.
For stateless session beans, the server maintains an instance pool and creates several instance objects for the client to call. When sending a request to create a session bean from a client, it is not always necessary to create an EJB. In most cases, an instance is obtained from the instance pool, and then the instance pool is put back after use. If you try again next time and then retrieve an instance from the instance pool for use, it is not necessarily the last instance. Even if the two accesses use the same instance, other clients may access the instance between the two accesses. Therefore, there is no guarantee that the information will be saved between multiple accesses. Therefore, stateless session beans do not save client information.

 

Advantages and disadvantages
Because stateful session beans need to save the information of a specific client, a client corresponds to an instance, and the instance must be retained for the client when there is no connection to the client at that time. In this way, as the number of clients increases, the number of instances to be created on the server also increases. Increasing to a certain extent will affect the server performance. In order not to affect the server performance, the server is usually optimized. When the number of clients exceeds a value, no new instance is created. Even if you do not create a new instance, you still need to respond to the user. In this case, you can share the instance. It will check which instance is in the connection status but is not accessed, and then save the instance status and use this instance as the new request service. For the original client, it is called a suspension. If the original client sends a request again, it searches for an idle instance again and restores the saved status. This process is called activation. Therefore, in the access process of a stateful Session Bean, operations such as instance search and activation suspension often occur, so the efficiency is relatively low.
When sending a request to the stateless Session Bean, you can take an idle instance as the client service, so the efficiency is relatively high.
The advantage of stateful Session Bean is that it can save the status of the client, so the client can transmit less parameters during subsequent access. The stateless Session Bean needs to pass all the parameters required during method execution.

 

How to select
Based on the advantages and disadvantages of stateful Session Bean and stateless session bean analyzed above. If frequent access is required and some information is shared among multiple accesses, a stateful Session Bean should be used. For infrequently used functions, you can use stateless session beans. Stateless session beans are used more than stateful session beans.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.