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

📄 transaction4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="wp81341"> </a><div class="pSmartList1"><li>Select WarehouseEJB in deploytool's tree.</li></div><a name="wp81342"> </a><div class="pSmartList1"><li>In the Transactions tab:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81149"> </a><div class="pSmartList2"><li>Select Bean-Managed under Transaction Management.</li></div></ol></div><a name="wp81416"> </a><div class="pSmartList1"><li>In the Resource Refs tab:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81417"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp81421"> </a><div class="pSmartList2"><li>Double-click the Coded Name column for the row that was just created.</li></div><a name="wp81422"> </a><div class="pSmartList2"><li>Enter <code class="cCode">jdbc/WarehouseDB</code>.</li></div><a name="wp81423"> </a><div class="pSmartList2"><li>Under Sun-specific Settings for <code class="cCode">jdbc/WarehouseDB</code> in the JNDI Name field select <code class="cCode">jdbc/ejbTutorialDB</code>.</li></div></ol></div></ol></div><a name="wp81151"> </a><h4 class="pHeading3">Packaging the J2EE Application Client</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81152"> </a><div class="pSmartList1"><li>Create a new application client in WarehouseApp by selecting File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Application Client.</li></div><a name="wp81153"> </a><div class="pSmartList1"><li>In the JAR File Contents screen:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81154"> </a><div class="pSmartList2"><li>Select WarehouseApp under Create New AppClient Module in Application.</li></div><a name="wp81155"> </a><div class="pSmartList2"><li>Enter WarehouseClient under AppClient Name.</li></div><a name="wp81156"> </a><div class="pSmartList2"><li>Click Edit.</li></div><a name="wp81157"> </a><div class="pSmartList2"><li>Navigate to <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/warehouse/</code>.</li></div><a name="wp81158"> </a><div class="pSmartList2"><li>Select <code class="cCode">WarehouseClient.class</code>.</li></div><a name="wp81159"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp81160"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp81161"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp81162"> </a><div class="pSmartList1"><li>In the General screen:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81163"> </a><div class="pSmartList2"><li>Select <code class="cCode">WarehouseClient</code> under Main Class.</li></div><a name="wp81164"> </a><div class="pSmartList2"><li>Select (Use container-managed authentication) under Callback Handler Class.</li></div><a name="wp81165"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp81166"> </a><div class="pSmartList1"><li>Click Finish.</li></div></ol></div><a name="wp81167"> </a><h4 class="pHeading3">Specifying the Application Client's Enterprise Bean Reference</h4><a name="wp81168"> </a><p class="pBody">When it invokes the <code class="cCode">lookup</code> method, <code class="cCode">WarehouseClient</code> refers to the home of an enterprise bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Object objref = initial.lookup(&quot;java:comp/env/ejb/SimpleWarehouse&quot;);<a name="wp81169"> </a></pre></div><a name="wp81170"> </a><p class="pBody">You specify this reference as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81171"> </a><div class="pSmartList1"><li>In the tree, select <code class="cCode">WarehouseClient</code>.</li></div><a name="wp81172"> </a><div class="pSmartList1"><li>Select the EJB Refs tab.</li></div><a name="wp81173"> </a><div class="pSmartList1"><li>Click Add.</li></div><a name="wp81174"> </a><div class="pSmartList1"><li>In the Coded Name field enter <code class="cCode">ejb/SimpleWarehouse</code>.</li></div><a name="wp81175"> </a><div class="pSmartList1"><li>In the EJB Type field select <code class="cCode">Session</code>.</li></div><a name="wp81176"> </a><div class="pSmartList1"><li>In the Interfaces field select <code class="cCode">Remote</code>.</li></div><a name="wp81177"> </a><div class="pSmartList1"><li>In the Home Interface field enter <code class="cCode">WarehouseHome</code>.</li></div><a name="wp81178"> </a><div class="pSmartList1"><li>In the Local/Remote Interface field, enter <code class="cCode">Warehouse</code>.</li></div><a name="wp81179"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp81180"> </a><div class="pSmartList1"><li>Select the line you just added.</li></div><a name="wp81181"> </a><div class="pSmartList1"><li>Under Sun-specific Settings for <code class="cCode">ejb/SimpleWarehouse</code> select JNDI Name.</li></div><a name="wp81182"> </a><div class="pSmartList1"><li>In the JNDI Name field select <code class="cCode">WarehouseEJB</code>.</li></div><a name="wp81183"> </a><div class="pSmartList1"><li>Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Save.</li></div></ol></div><a name="wp81184"> </a><h4 class="pHeading3">Deploying the J2EE Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81185"> </a><div class="pSmartList1"><li>Select <code class="cCode">WarehouseApp</code> in <code class="cCode">deploytool</code>.</li></div><a name="wp81186"> </a><div class="pSmartList1"><li>Select Tools<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Deploy.</li></div><a name="wp81187"> </a><div class="pSmartList1"><li>Under Connection Settings, enter the user name and password for the J2EE application server.</li></div><a name="wp81188"> </a><div class="pSmartList1"><li>Tell deploytool to create a JAR file that contains the client stubs:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81189"> </a><div class="pSmartList2"><li>Check the Return Client JAR box.</li></div><a name="wp81190"> </a><div class="pSmartList2"><li>In the field below the check box, enter <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/warehouse/</code>.</li></div></ol></div><a name="wp81191"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp81192"> </a><div class="pSmartList1"><li>In the Distribute Module dialog box click Close when the deployment completes successfully.</li></div></ol></div><a name="wp81193"> </a><h4 class="pHeading3">Running the J2EE Application Client</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81194"> </a><div class="pSmartList1"><li>In a terminal window, go to the <code class="cVariable">&lt;INSTALL&gt;</code><code class="cCode">/j2eetutorial14/<br />examples/ejb/warehouse/</code> directory.</li></div><a name="wp81195"> </a><div class="pSmartList1"><li>Type the following command:</li></div><a name="wp81196"> </a><p class="pBodyRelative"><code class="cCode">appclient -client WarehouseAppClient.jar</code></p></ol></div><a name="wp81197"> </a><p class="pBody">In the terminal window, the client displays these lines:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">status = shipped<a name="wp81198"> </a></pre></div><a name="wp79982"> </a><h3 class="pHeading2">JTA Transactions</h3><a name="wp79983"> </a><p class="pBody">JTA is the abbreviation for the Java Transaction API. This API allows you to demarcate transactions in a manner that is independent of the transaction manager implementation. The J2EE Application Server implements the transaction manager with the Java Transaction Service (&quot;JTS&quot;). But your code doesn't call the JTS methods directly. Instead, it invokes the JTA methods, which then call the lower-level JTS routines.</p><a name="wp79986"> </a><p class="pBody">A <span style="font-style: italic">JTA transaction</span> is controlled by the J2EE transaction manager. You may want to use a JTA transaction because it can span updates to multiple databases from different vendors. A particular DBMS's transaction manager may not work with heterogeneous databases. However, the J2EE transaction manager does have one limitation--it does not support nested transactions. In other words, it cannot start a transaction for an instance until the previous transaction has ended.</p><a name="wp79989"> </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/teller/</code> directory. </p><a name="wp79993"> </a><p class="pBody">To demarcate a JTA transaction, you invoke the <code class="cCode">begin</code>, <code class="cCode">commit</code>, and <code class="cCode">rollback</code> methods of the <code class="cCode">javax.transaction.UserTransaction</code> interface. The following code, taken from the <code class="cCode">TellerBean</code> class, demonstrates the <code class="cCode">UserTransaction</code> methods. The <code class="cCode">begin</code> and <code class="cCode">commit</code> invocations delimit the updates to the database. If the updates fail, the code invokes the <code class="cCode">rollback</code> method and throws an <code class="cCode">EJBException</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void withdrawCash(double amount) {   UserTransaction ut = context.getUserTransaction();   try {      ut.begin();      updateChecking(amount);      machineBalance -= amount;      insertMachine(machineBalance);      ut.commit();   } catch (Exception ex) {       try {          ut.rollback();       } catch (SystemException syex) {           throw new EJBException              (&quot;Rollback failed: &quot; + syex.getMessage());       }       throw new EJBException           (&quot;Transaction failed: &quot; + ex.getMessage());    }}<a name="wp79998"> </a></pre></div><a name="wp80443"> </a><h3 class="pHeading2">Deploying and Running the TellerEJB Example</h3><a name="wp80444"> </a><p class="pBody">The <code class="cCode">TellerEJB</code> session bean uses bean-managed, JTA transactions. These steps assume you are familiar with the steps needed to create and deploy an enterprise application using deploytool, as described in <a  href="Session.html#wp79662">Session Bean Examples</a>. To deploy and run the <code class="cCode">TellerEJB</code> example, perform these steps:</p><a name="wp81475"> </a><h3 class="pHeading2">Compiling the TellerEJB Example</h3><a name="wp81476"> </a><p class="pBody">To compile the classes and interfaces in the TellerEJB example, follow these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81477"> </a><div class="pSmartList1"><li>In a terminal window, go to this directory:</li></div><a name="wp81478"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/teller/</code> </p><a name="wp81481"> </a><div class="pSmartList1"><li>Start the PointBase server. For instructions, see <a  href="WebApp13.html#wp83431">Starting the PointBase Database Server</a>.</li></div><a name="wp81483"> </a><div class="pSmartList1"><li>Create the database tables and data by typing:</li></div><a name="wp81484"> </a><p class="pBodyRelative"><code class="cCode">asant create-db_common</code> </p><a name="wp81485"> </a><div class="pSmartList1"><li>Type the following command to build the enterprise bean's classes and interfaces:</li></div><a name="wp81486"> </a><p class="pBodyRelative"><code class="cCode">asant build</code> </p></ol></div><a name="wp81487"> </a><h3 class="pHeading2">Packaging the TellerEJB Example</h3><a name="wp81488"> </a><p class="pBody">The <code class="cCode">TellerEJB </code>session bean uses JTA transactions. These steps assume you are familiar with the steps needed to create and deploy an enterprise application using deploytool, as described in <a  href="EJB.html#wp80471">Getting Started With Enterprise Beans</a>.</p><a name="wp81492"> </a><h4 class="pHeading3">Creating the J2EE Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81493"> </a><div class="pSmartList1"><li>Create a new application named TellerApp in:</li></div><a name="wp81494"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/teller/</code> </p></ol></div><a name="wp81495"> </a><h4 class="pHeading3">Packaging the Enterprise Bean</h4>

⌨️ 快捷键说明

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