⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transaction3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<?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>Container-Managed Transactions</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="Transaction2.html" />    <link rel="Next" href="Transaction4.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="Transaction2.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="Transaction4.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="wp79725"> </a><h2 class="pHeading1">Container-Managed Transactions</h2><a name="wp79728"> </a><p class="pBody">In an enterprise bean with container-managed transactions, the EJB container sets the boundaries of the transactions. You can use container-managed transactions with any type of enterprise bean: session, entity, or message-driven. Container-managed transactions simplify development because the enterprise bean code does not explicitly mark the transaction's boundaries. The code does not include statements that begin and end the transaction.</p><a name="wp79733"> </a><p class="pBody">Typically, the container begins a transaction immediately before an enterprise bean method starts. It commits the transaction just before the method exits. Each method can be associated with a single transaction. Nested or multiple transactions are not allowed within a method.</p><a name="wp79735"> </a><p class="pBody">Container-managed transactions do not require all methods to be associated with transactions. When deploying a bean, you specify which of the bean's methods are associated with transactions by setting the transaction attributes.</p><a name="wp79737"> </a><h3 class="pHeading2">Transaction Attributes</h3><a name="wp79742"> </a><p class="pBody">A transaction attribute controls the scope of a transaction. <a  href="Transaction3.html#wp79748">Figure 25-1</a> illustrates why controlling the scope is important. In the diagram, <code class="cCode">method-A</code> begins a transaction and then invokes <code class="cCode">method-B</code> of <code class="cCode">Bean-2</code>. When <code class="cCode">method-B</code> executes, does it run within the scope of the transaction started by method-A or does it execute with a new transaction? The answer depends on the transaction attribute of <code class="cCode">method-B</code>.</p><a name="wp79746"> </a><p class="pBody"></p><div align="left"><img src="images/Fig32.gif" height="145" width="367" alt="Transaction Scope" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="79748"> </a><strong><font >Figure 25-1    Transaction Scope</font></strong></p><a name="wp79749"> </a><p class="pBody">A transaction attribute may have one of the following values:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79750"> </a><div class="pSmartList1"><li><code class="cCode">Required</code></li></div><a name="wp79751"> </a><div class="pSmartList1"><li><code class="cCode">RequiresNew</code></li></div><a name="wp79752"> </a><div class="pSmartList1"><li><code class="cCode">Mandatory</code></li></div><a name="wp79753"> </a><div class="pSmartList1"><li><code class="cCode">NotSupported</code></li></div><a name="wp79754"> </a><div class="pSmartList1"><li><code class="cCode">Supports</code></li></div><a name="wp79755"> </a><div class="pSmartList1"><li><code class="cCode">Never</code></li></div></ul></div><a name="wp79756"> </a><h4 class="pHeading3">Required</h4><a name="wp79757"> </a><p class="pBody">If the client is running within a transaction and invokes the enterprise bean's method, the method executes within the client's transaction. If the client is not associated with a transaction, the container starts a new transaction before running the method.</p><a name="wp79758"> </a><p class="pBody">The <code class="cCode">Required</code> attribute will work for most transactions. Therefore, you may want to use it as a default, at least in the early phases of development. Because transaction attributes are declarative, you can easily change them at a later time.</p><a name="wp79759"> </a><h4 class="pHeading3">RequiresNew</h4><a name="wp79760"> </a><p class="pBody">If the client is running within a transaction and invokes the enterprise bean's method, the container takes the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79761"> </a><div class="pSmartList1"><li>Suspends the client's transaction</li></div><a name="wp79762"> </a><div class="pSmartList1"><li>Starts a new transaction </li></div><a name="wp79763"> </a><div class="pSmartList1"><li>Delegates the call to the method</li></div><a name="wp79764"> </a><div class="pSmartList1"><li>Resumes the client's transaction after the method completes</li></div></ol></div><a name="wp79765"> </a><p class="pBody">If the client is not associated with a transaction, the container starts a new transaction before running the method.</p><a name="wp79766"> </a><p class="pBody">You should use the <code class="cCode">RequiresNew</code> attribute when you want to ensure that the method always runs within a new transaction.</p><a name="wp79767"> </a><h4 class="pHeading3">Mandatory</h4><a name="wp79768"> </a><p class="pBody">If the client is running within a transaction and invokes the enterprise bean's method, the method executes within the client's transaction. If the client is not associated with a transaction, the container throws the <code class="cCode">TransactionRequiredException</code>.</p><a name="wp79770"> </a><p class="pBody">Use the <code class="cCode">Mandatory</code> attribute if the enterprise bean's method must use the transaction of the client.</p><a name="wp79771"> </a><h4 class="pHeading3">NotSupported</h4><a name="wp79772"> </a><p class="pBody">If the client is running within a transaction and invokes the enterprise bean's method, the container suspends the client's transaction before invoking the method. After the method has completed, the container resumes the client's transaction.</p><a name="wp79773"> </a><p class="pBody">If the client is not associated with a transaction, the container does not start a new transaction before running the method. </p><a name="wp79774"> </a><p class="pBody">Use the <code class="cCode">NotSupported</code> attribute for methods that don't need transactions. Because transactions involve overhead, this attribute may improve performance.</p><a name="wp79775"> </a><h4 class="pHeading3">Supports</h4><a name="wp79776"> </a><p class="pBody">If the client is running within a transaction and invokes the enterprise bean's method, the method executes within the client's transaction. If the client is not associated with a transaction, the container does not start a new transaction before running the method. </p><a name="wp79777"> </a><p class="pBody">Because the transactional behavior of the method may vary, you should use the <code class="cCode">Supports</code> attribute with caution.</p><a name="wp79779"> </a><h4 class="pHeading3">Never</h4><a name="wp79780"> </a><p class="pBody">If the client is running within a transaction and invokes the enterprise bean's method, the container throws a <code class="cCode">RemoteException</code>. If the client is not associated with a transaction, the container does not start a new transaction before running the method.</p><a name="wp79781"> </a><h4 class="pHeading3">Summary of Transaction Attributes</h4><a name="wp79785"> </a><p class="pBody"><a  href="Transaction3.html#wp79802">Table 25-1</a> summarizes the effects of the transaction attributes. Both the T1 and T2 transactions are controlled by the container. A T1 transaction is associated with the client that calls a method in the enterprise bean. In most cases, the client is another enterprise bean. A T2 transaction is started by the container just before the method executes. </p><a name="wp79790"> </a><p class="pBody">In the last column of <a  href="Transaction3.html#wp79802">Table 25-1</a>, the word &quot;None&quot; means that the business method does not execute within a transaction controlled by the container. However, the database calls in such a business method might be controlled by the transaction manager of the DBMS.</p><a name="wp79793"> </a><h4 class="pHeading3">Setting Transaction Attributes</h4><a name="wp79795"> </a><p class="pBody">Because transaction attributes are stored in the deployment descriptor, they can be changed during several phases of J2EE application development: enterprise bean creation, application assembly, and deployment. However, it is the responsibility of an enterprise bean developer to specify the attributes when creating the bean. The attributes should be modified only by an application developer who is assembling components into larger applications. Do not expect the person deploying the J2EE application to specify the transaction attributes.</p><a name="wp79885"> </a><p class="pBody"></p><div align="left"><table border="1" summary="Transaction Attributes and Scope" id="wp79802">  <caption><a name="wp79802"> </a><div class="pTableTitle">Table 25-1   Transaction Attributes and Scope&nbsp;</div></caption>  <tr align="center">    <th><a name="wp79808"> </a><div class="pCellHeading">Transaction Attribute</div></th>    <th><a name="wp79810"> </a><div class="pCellHeading">Client's Transaction</div></th>    <th><a name="wp79812"> </a><div class="pCellHeading">Business Method's Transaction</div></th></tr>  <tr align="left">    <td colspan="1" rowspan="2"><a name="wp79814"> </a><div class="pCellBody"><code class="cCode">Required<br /><br /></code></div></td>    <td><a name="wp79816"> </a><div class="pCellBody">None</div></td>    <td><a name="wp79818"> </a><div class="pCellBody">T2</div></td></tr>  <tr align="left">    <td><a name="wp79822"> </a><div class="pCellBody">T1</div></td>    <td><a name="wp79824"> </a><div class="pCellBody">T1</div></td>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -