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

📄 transaction3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
</tr>  <tr align="left">    <td colspan="1" rowspan="2"><a name="wp79826"> </a><div class="pCellBody"><code class="cCode">RequiresNew<br /><br /></code></div></td>    <td><a name="wp79828"> </a><div class="pCellBody">None</div></td>    <td><a name="wp79830"> </a><div class="pCellBody">T2</div></td></tr>  <tr align="left">    <td><a name="wp79834"> </a><div class="pCellBody">T1</div></td>    <td><a name="wp79836"> </a><div class="pCellBody">T2</div></td></tr>  <tr align="left">    <td colspan="1" rowspan="2"><a name="wp79838"> </a><div class="pCellBody"><code class="cCode">Mandatory<br /><br /></code></div></td>    <td><a name="wp79840"> </a><div class="pCellBody">None</div></td>    <td><a name="wp79842"> </a><div class="pCellBody">error</div></td></tr>  <tr align="left">    <td><a name="wp79846"> </a><div class="pCellBody">T1</div></td>    <td><a name="wp79848"> </a><div class="pCellBody">T1</div></td></tr>  <tr align="left">    <td colspan="1" rowspan="2"><a name="wp79850"> </a><div class="pCellBody"><code class="cCode">NotSupported<br /><br /></code></div></td>    <td><a name="wp79852"> </a><div class="pCellBody">None</div></td>    <td><a name="wp79854"> </a><div class="pCellBody">None</div></td></tr>  <tr align="left">    <td><a name="wp79858"> </a><div class="pCellBody">T1</div></td>    <td><a name="wp79860"> </a><div class="pCellBody">None</div></td></tr>  <tr align="left">    <td colspan="1" rowspan="2"><a name="wp79862"> </a><div class="pCellBody"><code class="cCode">Supports<br /><br /></code></div></td>    <td><a name="wp79864"> </a><div class="pCellBody">None</div></td>    <td><a name="wp79866"> </a><div class="pCellBody">None</div></td></tr>  <tr align="left">    <td><a name="wp79870"> </a><div class="pCellBody">T1</div></td>    <td><a name="wp79872"> </a><div class="pCellBody">T1</div></td></tr>  <tr align="left">    <td colspan="1" rowspan="2"><a name="wp79874"> </a><div class="pCellBody"><code class="cCode">Never<br /><br /></code></div></td>    <td><a name="wp79876"> </a><div class="pCellBody">None</div></td>    <td><a name="wp79878"> </a><div class="pCellBody">None</div></td></tr>  <tr align="left">    <td><a name="wp79882"> </a><div class="pCellBody">T1</div></td>    <td><a name="wp79884"> </a><div class="pCellBody">Error</div></td></tr></table></div><p class="pBody"></p><a name="wp79886"> </a><p class="pBody">You can specify the transaction attributes for the entire enterprise bean or for individual methods. If you've specified one attribute for a method and another for the bean, the attribute for the method takes precedence. When specifying attributes for individual methods, the requirements differ with the type of bean. Session beans need the attributes defined for business methods, but do not allow them for the <code class="cCode">create</code> methods. Entity beans require transaction attributes for the business, <code class="cCode">create</code>, <code class="cCode">remove</code>, and finder methods. Message-driven beans require transaction attributes (either <code class="cCode">Required</code> or <code class="cCode">NotSupported</code>) for the <code class="cCode">onMessage</code> method. </p><a name="wp79894"> </a><h3 class="pHeading2">Rolling Back a Container-Managed Transaction</h3><a name="wp79896"> </a><p class="pBody">There are two ways to roll back a container-managed transaction. First, if a system exception is thrown, the container will automatically roll back the transaction. Second, by invoking the <code class="cCode">setRollbackOnly</code> method of the <code class="cCode">EJBContext</code> interface, the bean method instructs the container to roll back the transaction. If the bean throws an application exception, the rollback is not automatic, but may be initiated by a call to <code class="cCode">setRollbackOnly</code>. For a description of system and application exceptions, see <a  href="BMP5.html#wp81421">deploytool Tips for Entity Beans with Bean-Managed Persistence</a>.</p><a name="wp79902"> </a><p class="pBody">The source code for the following example is in the <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/bank</code> directory. </p><a name="wp79903"> </a><p class="pBody">The <code class="cCode">transferToSaving</code> method of the <code class="cCode">BankEJB</code> example illustrates the <code class="cCode">setRollbackOnly</code> method. If a negative checking balance occurs, <code class="cCode">transferToSaving</code> invokes <code class="cCode">setRollBackOnly</code> and throws an application exception (<code class="cCode">InsufficientBalanceException</code>). The <code class="cCode">updateChecking</code> and <code class="cCode">updateSaving</code> methods update database tables. If the updates fail, these methods throw a <code class="cCode">SQLException</code> and the <code class="cCode">transferToSaving</code> method throws an <code class="cCode">EJBException</code>. Because the <code class="cCode">EJBException</code> is a system exception, it causes the container to automatically roll back the transaction. Here is the code for the <code class="cCode">transferToSaving</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void transferToSaving(double amount) throws   InsufficientBalanceException  {   checkingBalance -= amount;   savingBalance += amount;   try {      updateChecking(checkingBalance);      if (checkingBalance &lt; 0.00) {         context.setRollbackOnly();         throw new InsufficientBalanceException();      }      updateSaving(savingBalance);   } catch (SQLException ex) {       throw new EJBException           (&quot;Transaction failed due to SQLException: &quot;           + ex.getMessage());   }}<a name="wp79907"> </a></pre></div><a name="wp79908"> </a><p class="pBody">When the container rolls back a transaction, it always undoes the changes to data made by SQL calls within the transaction. However, only in entity beans will the container undo changes made to instance variables. (It does so by automatically invoking the entity bean's <code class="cCode">ejbLoad</code> method, which loads the instance variables from the database.) When a rollback occurs, a session bean must explicitly reset any instance variables changed within the transaction. The easiest way to reset a session bean's instance variables is by implementing the <code class="cCode">SessionSynchronization</code> interface.</p><a name="wp79914"> </a><h3 class="pHeading2">Synchronizing a Session Bean's Instance Variables</h3><a name="wp79915"> </a><p class="pBody">The <code class="cCode">SessionSynchronization</code> interface, which is optional, allows you to synchronize the instance variables with their corresponding values in the database. The container invokes the <code class="cCode">SessionSynchronization</code> methods--<code class="cCode">afterBegin</code>, <code class="cCode">beforeCompletion</code>, and <code class="cCode">afterCompletion</code>--at each of the main stages of a transaction.</p><a name="wp79916"> </a><p class="pBody">The <code class="cCode">afterBegin</code> method informs the instance that a new transaction has begun. The container invokes <code class="cCode">afterBegin</code> immediately before it invokes the business method. The <code class="cCode">afterBegin</code> method is a good place to load the instance variables from the database. The <code class="cCode">BankBean</code> class, for example, loads the <code class="cCode">checkingBalance</code> and <code class="cCode">savingBalance</code> variables in the <code class="cCode">afterBegin</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void afterBegin() {   System.out.println(&quot;afterBegin()&quot;);   try {      checkingBalance = selectChecking();      savingBalance = selectSaving();   } catch (SQLException ex) {       throw new EJBException(&quot;afterBegin Exception: &quot; +           ex.getMessage());   }}<a name="wp79919"> </a></pre></div><a name="wp79920"> </a><p class="pBody">The container invokes the <code class="cCode">beforeCompletion</code> method after the business method has finished, but just before the transaction commits. The <code class="cCode">beforeCompletion</code> method is the last opportunity for the session bean to roll back the transaction (by calling <code class="cCode">setRollbackOnly</code>). If it hasn't already updated the database with the values of the instance variables, the session bean may do so in the <code class="cCode">beforeCompletion</code> method.</p><a name="wp79923"> </a><p class="pBody">The <code class="cCode">afterCompletion</code> method indicates that the transaction has completed. It has a single <code class="cCode">boolean</code> parameter, whose value is <code class="cCode">true</code> if the transaction was committed and <code class="cCode">false</code> if it was rolled back. If a rollback occurred, the session bean can refresh its instance variables from the database in the <code class="cCode">afterCompletion</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void afterCompletion(boolean committed) {   System.out.println(&quot;afterCompletion: &quot; + committed);   if (committed == false) {      try {         checkingBalance = selectChecking();         savingBalance = selectSaving();      } catch (SQLException ex) {          throw new EJBException(&quot;afterCompletion SQLException:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; + ex.getMessage());      }   }}<a name="wp79925"> </a></pre></div><a name="wp80439"> </a><h3 class="pHeading2">Compiling the BankEJB Example</h3><a name="wp80617"> </a><p class="pBody">To compile the classes and interfaces in the BankEJB example, follow these steps:</p>

⌨️ 快捷键说明

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