📄 ejbconcepts3.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>What Is a Session Bean?</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="EJBConcepts2.html" /> <link rel="Next" href="EJBConcepts4.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="EJBConcepts2.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="EJBConcepts4.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp79775"> </a><h2 class="pHeading1">What Is a Session Bean?</h2><a name="wp79778"> </a><p class="pBody">A <span style="font-style: italic">session bean</span> represents a single client inside the J2EE server. To access an application that is deployed on the server, the client invokes the session bean's methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server.</p><a name="wp79779"> </a><p class="pBody">As its name suggests, a session bean is similar to an interactive session. A session bean is not shared--it can have just one client, in the same way that an interactive session can have just one user. Like an interactive session, a session bean is not persistent. (That is, its data is not saved to a database.) When the client terminates, its session bean appears to terminate and is no longer associated with the client.</p><a name="wp79789"> </a><p class="pBody">For code samples, see Chapter <a href="Session.html#wp79660">20</a><a href="BMP.html#wp80422"></a>.</p><a name="wp79790"> </a><h3 class="pHeading2">State Management Modes</h3><a name="wp79791"> </a><p class="pBody">There are two types of session beans: stateful and stateless.</p><a name="wp79792"> </a><h4 class="pHeading3">Stateful Session Beans</h4><a name="wp79794"> </a><p class="pBody">The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts ("talks") with its bean, this state is often called the <span style="font-style: italic">conversational state</span>.</p><a name="wp79795"> </a><p class="pBody">The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears. This transient nature of the state is not a problem, however, because when the conversation between the client and the bean ends there is no need to retain the state.</p><a name="wp79796"> </a><h4 class="pHeading3">Stateless Session Beans</h4><a name="wp79798"> </a><p class="pBody">A stateless session bean does not maintain a conversational state for the client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client.</p><a name="wp79799"> </a><p class="pBody">Because stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.</p><a name="wp79800"> </a><p class="pBody">At times, the EJB container may write a stateful session bean to secondary storage. However, stateless session beans are never written to secondary storage. Therefore, stateless beans may offer better performance than stateful beans.</p><a name="wp80602"> </a><p class="pBody">A stateless session bean can implement a Web service, but other types of enterprise beans cannot. </p><a name="wp79801"> </a><h3 class="pHeading2">When to Use Session Beans</h3><a name="wp79802"> </a><p class="pBody">In general, you should use a session bean if the following circumstances hold:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79803"> </a><div class="pSmartList1"><li>At any given time, only one client has access to the bean instance.</li></div><a name="wp79804"> </a><div class="pSmartList1"><li>The state of the bean is not persistent, existing only for a short period of time (perhaps a few hours).</li></div><a name="wp80601"> </a><div class="pSmartList1"><li>The bean implements a Web service. </li></div></ul></div><a name="wp79806"> </a><p class="pBody">Stateful session beans are appropriate if any of the following conditions are true:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79807"> </a><div class="pSmartList1"><li>The bean's state represents the interaction between the bean and a specific client.</li></div><a name="wp79808"> </a><div class="pSmartList1"><li>The bean needs to hold information about the client across method invocations.</li></div><a name="wp79809"> </a><div class="pSmartList1"><li>The bean mediates between the client and the other components of the application, presenting a simplified view to the client.</li></div><a name="wp79811"> </a><div class="pSmartList1"><li>Behind the scenes, the bean manages the work flow of several enterprise beans. For an example, see the <code class="cCode">AccountControllerEJB</code> session bean in Chapter <a href="Ebank.html#wp79661">32</a>.<a href="Ebank.html#wp79664"></a></li></div></ul></div><a name="wp79819"> </a><p class="pBody">To improve performance, you might choose a stateless session bean if it has any of these traits:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79820"> </a><div class="pSmartList1"><li>The bean's state has no data for a specific client.</li></div><a name="wp79821"> </a><div class="pSmartList1"><li>In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an e-mail that confirms an online order.</li></div><a name="wp79823"> </a><div class="pSmartList1"><li>The bean fetches from a database a set of read-only data that is often used by clients. Such a bean, for example, could retrieve the table rows that represent the products that are on sale this month.</li></div></ul></div> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="EJBConcepts2.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="EJBConcepts4.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -