Difference between stateful Session Bean and stateless Session BeanThe State is generally understood as an object property, so stateless means that there is no property, and stateful means that there is a property. This understanding is incorrect. The stateless Session Bean here is indeed related, but the difference between stateful Session Bean and stateless Session Bean is not whether there are attributes, but whether to save the client attributes. Stateful session beans Save the status of the client, while stateless beans do not specifically Save the status of the client. Here we need to emphasize "special" because stateless session beans also have member variables, and member variables can be saved, but they are not saved for specific clients.
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, stateless session beans do not save client information.
Advantages and disadvantagesThe stateful Session Bean needs to save the information of a specific client. A client corresponds to an instance, and the instance must be retained for the client even if the client has no connection at the 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 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 stateful session beans, 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 selectBased 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.
An instance that describes the differences between a stateless Session Bean and a stateful Session BeanThe following code is a stateful Session Bean indicating the shopping cart: Package ch19; import javax. EJB. stateful; import Java. util. list; @ statefulpublic class cartbean implements cart {private string customerid; private string customername; private arraylist <string> contents; Public void initialize (string Person) throws bookexception {If (person = NULL) {Throw new bookexception ("no user is allowed! ");} Else {customername = person;} customerid =" 0 "; contents = new arraylist <string> ();} public void initialize (string person, string ID) throws bookexception {If (person = NULL) {Throw new bookexception ("no user is allowed! ");} Else {customername = person;} idverifier idchecker = new idverifier (); If (idchecker. validate (ID) {customerid = ID;} else {Throw new bookexception ("invalid ID:" + id);} contents = new arraylist <string> ();} public void addbook (String title) {contents. add (title);} public void removebook (String title) throws bookexception {boolean result = contents. remove (title); If (result = false) {th Row new bookexception (Title + "is not in the cart. ") ;}} Public list <string> getcontents () {return contents ;}@ remove () Public void remove () {contents = NULL ;}} assume that the business interface is the cart code source: Java ee 5 practical tutorial-Based on WebLogic and eclipse if two EJB objects are injected on the client and injected through the instance, the Code is as follows: @ ejbcart cart1; @ ejbcart cart2; the Code for calling the business method is as follows: cart1.initialize ("Zhang San"); cart1.addbook ("jsp2.0 basic tutorial"); cart1.addbook ("Java ee5 practical tutorial "); cart2.initialize (""); cart2.addbook ("ASP Website development"); cart2.addbook ("Latest C # instance Development"); List <stri NG> books = cart1.getcontents (); out. println ("the books in cart1 are:"); For (INT I = 0; I <books. size (); I ++) {out. println (books. get (I);} books = cart2.getcontents (); out. println ("<br> the book in cart2 is:"); For (INT I = 0; I <books. size (); I ++) {out. println (books. get (I);} The execution result is as follows: Books in cart1 are: Books in Java ee5 practical tutorial of jsp2.0: ASP Website development latest C # instance development if the stateful in cartbean is changed to stateless, re-running will get the following execution result: the books in cart1 are: ASP Website development latest C # instance development books in cart2: ASP Website development latest C # Why are the two instance development results different? The use of cart1 and cart2 for stateful session beans corresponds to two bean instances, each of which has its own properties. In stateless session beans, cart1 and cart2 actually correspond to the same bean instance. Therefore, the books added through cart1 are overwritten by the cart2 addition operation, therefore, the books obtained by cart1 are no longer stored in the "Java Web programming basics" and "Java ee 5 practical tutorials ". Through this instance, we can better understand the differences between State Session Bean and non-Body session bean.