Stateful sessionbean stateless sessionbean

Source: Internet
Author: User

Difference between stateful Session Bean (sfsb) and stateless Session Bean (slsb)
1 stateless)
No status is retained between different method calls.
Transaction processing must end in a method.
Resources usually occupy less resources; resources can be shared (because they are stateless ).
Stateless beans do not "specifically" Save the client's status ---- (special needs to be emphasized because stateless session beans also have member variables, and member variables can be saved if they exist, but it does not save the status for a specific client .).
---- Your data operations on Entity Bean will not be maintained by containers. When other users use entity bean, the data in it will not
It will be deactivated to the server disk. That is, it is saved.

2 stateful)
You can maintain the status of each client between different method calls.
The connection with the client must be maintained; usually the overhead is large.
Stateful session beans Save the client status.
-- Your Data Operations on Entity Bean will be maintained by the container. When other users use the Entity Bean you are using, the data in it will be deactivated to the server disk, for example, online shopping cart.

Root Cause of differenceThis 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, the stateless Session Bean
The client information is not saved. Advantages and disadvantagesBecause 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 the number to a certain degree 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 and
When a request is sent, an idle instance is searched again and the saved status is restored. 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 status 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.