stated class, stateless class, 兩種類別,代表了一種思路
在server端,不維護(hù)每個(gè)client的狀態(tài),將會(huì)減少麻煩,是否需要引入無(wú)狀態(tài)思路呢?那么每個(gè)交互都是request<-->response模式
有狀態(tài)模式,就是session維護(hù)模式,這還是非常常見的。
參考
Stateless or Stateful?
Service objects will usually be stateless. Stateless service layers are highly scalable: They pose no replication
issues and there is no need to allocate additional resources for every client. (Remember that one of
the key motivations of a middle tier is to share resources between multiple clients.) It is also much easier
for stateless service layers to support remote clients, if necessary.
The traditional stateless service objects in J2EE applications are stateless session beans (SLSBs). I’ll use
SLSBs as a starting point for discussion because they illustrate many of the basic concepts of stateless
service objects, which predate EJB.
A stateless service layer is one concession of object orientation that I find not too painful. Stateless service
objects are semi-objects. Although they cannot expose state to callers, they can hold internal state
and they can fully participate in inheritance relationships. If they are local, rather than remote, they can
use true objects as parameters and return values.
There are two main potential models for stateful service layers in J2EE: stateful session beans (SFSBs) and
web tier session objects. If we don’t use stateful session beans, session data is usually held in Servlet API
HttpSession objects. Holding session data in the web tier is usually more scalable than holding it in the
EJB tier. (See Chapter 10 of Expert One-on-One J2EE Design and Development for detailed discussion of
state replication issues.) “Thick” clients such as Swing applications will normally hold their own state.
Because stateless service layers have proven their value in numerous technologies, including both J2EE
and Microsoft platforms, we’ll focus on them in this book.
If possible, design applications to use a stateless service layer. Hold state in the web
tier, rather than in the business logic tier, if possible.
對(duì)于EJB來(lái)講,Bean實(shí)例并非使用時(shí)創(chuàng)建,而是實(shí)現(xiàn)創(chuàng)建一個(gè)對(duì)象池,當(dāng)client需要該bean中方法時(shí),如果時(shí)無(wú)狀態(tài)的,容器會(huì)隨便指定一個(gè)空閑的給client使用,但是如果是有狀態(tài)的,容器必須記住上次是那個(gè)bean實(shí)例為這個(gè)client服務(wù)的,下次同一個(gè)client請(qǐng)求也必須由這個(gè)同一個(gè)實(shí)例來(lái)服務(wù),不能換由其他的bean實(shí)例來(lái)服務(wù)