📄 transaction4.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>Bean-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="Transaction3.html" /> <link rel="Next" href="Transaction5.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="Transaction3.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="Transaction5.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="wp79940"> </a><h2 class="pHeading1">Bean-Managed Transactions</h2><a name="wp79943"> </a><p class="pBody">In a bean-managed transaction, the code in the session or message-driven bean explicitly marks the boundaries of the transaction. An entity bean cannot have bean-managed transactions; it must use container-managed transactions instead. Although beans with container-managed transactions require less coding, they have one limitation: When a method is executing, it can be associated with either a single transaction or no transaction at all. If this limitation will make coding your bean difficult, you should consider using bean-managed transactions.</p><a name="wp79947"> </a><p class="pBody">The following pseudocode illustrates the kind of fine-grained control you can obtain with bean-managed transactions. By checking various conditions, the pseudocode decides whether to start or stop different transactions within the business method.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">begin transaction...update table-a...if (condition-x) commit transactionelse if (condition-y) update table-b commit transactionelse rollback transaction begin transaction update table-c commit transaction<a name="wp79948"> </a></pre></div><a name="wp79951"> </a><p class="pBody">When coding a bean-managed transaction for session or message-driven beans, you must decide whether to use JDBC or JTA transactions. The sections that follow discuss both types of transactions.</p><a name="wp79957"> </a><h3 class="pHeading2">JDBC Transactions</h3><a name="wp79960"> </a><p class="pBody">A <span style="font-style: italic">JDBC transaction</span> is controlled by the transaction manager of the DBMS. You may want to use JDBC transactions when wrapping legacy code inside a session bean. To code a JDBC transaction, you invoke the <code class="cCode">commit</code> and <code class="cCode">rollback</code> methods of the <code class="cCode">java.sql.Connection</code> interface. The beginning of a transaction is implicit. A transaction begins with the first SQL statement that follows the most recent <code class="cCode">commit</code>, <code class="cCode">rollback</code>, or <code class="cCode">connect</code> statement. (This rule is generally true, but may vary with DBMS vendor.)</p><a name="wp79971"> </a><p class="pBody">The source code for the following example is in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/warehouse/</code> directory. </p><a name="wp79972"> </a><p class="pBody">The following code is from the <code class="cCode">WarehouseEJB</code> example, a session bean that uses the <code class="cCode">Connection</code> interface's methods to delimit bean-managed transactions. The <code class="cCode">ship</code> method starts by invoking <code class="cCode">setAutoCommit</code> on the <code class="cCode">Connection</code> object named <code class="cCode">con</code>. This invocation tells the DBMS not to automatically commit every SQL statement. Next, the <code class="cCode">ship</code> method calls routines that update the <code class="cCode">order_item</code> and <code class="cCode">inventory</code> database tables. If the updates succeed, the transaction is committed. If an exception is thrown, however, the transaction is rolled back.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void ship (String productId, String orderId, int quantity) { try { makeConnection(); con.setAutoCommit(false); updateOrderItem(productId, orderId); updateInventory(productId, quantity); con.commit(); } catch (Exception ex) { try { con.rollback(); throw new EJBException("Transaction failed: " + ex.getMessage()); } catch (SQLException sqx) { throw new EJBException("Rollback failed: " + sqx.getMessage()); } } finally { releaseConnection(); }} <a name="wp80377"> </a></pre></div><a name="wp80441"> </a><h3 class="pHeading2">Deploying and Running the WarehouseEJB Example</h3><a name="wp80453"> </a><p class="pBody"><code class="cCode">WarehouseEJB</code> is a session bean that uses a bean-managed, JDBC 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 example, do the following:</p><a name="wp81237"> </a><h3 class="pHeading2">Compiling the WarehouseEJB Example</h3><a name="wp81238"> </a><p class="pBody">To compile the classes and interfaces in the WarehouseEJB example, follow these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81109"> </a><div class="pSmartList1"><li>In a terminal window, go to this directory:</li></div><a name="wp81110"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/warehouse/</code> </p><a name="wp81113"> </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="wp81115"> </a><div class="pSmartList1"><li>Create the database tables and data by typing:</li></div><a name="wp81116"> </a><p class="pBodyRelative"><code class="cCode">asant create-db_common</code> </p><a name="wp81117"> </a><div class="pSmartList1"><li>Type the following command to build the enterprise bean's classes and interfaces:</li></div><a name="wp81118"> </a><p class="pBodyRelative"><code class="cCode">asant build</code> </p></ol></div><a name="wp81119"> </a><h3 class="pHeading2">Packaging the WarehouseEJB Example</h3><a name="wp81120"> </a><p class="pBody">The <code class="cCode">WarehouseEJB </code>session bean uses bean-managed 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="wp81124"> </a><h4 class="pHeading3">Creating the J2EE Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81125"> </a><div class="pSmartList1"><li>Create a new application named WarehouseApp in:</li></div><a name="wp81126"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/warehouse/</code> </p></ol></div><a name="wp81273"> </a><h4 class="pHeading3">Packaging the Enterprise Bean</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81274"> </a><div class="pSmartList1"><li>Create a new enterprise bean 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> Enterprise Bean.</li></div><a name="wp81129"> </a><div class="pSmartList1"><li>In the EJB JAR screen:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81130"> </a><div class="pSmartList2"><li>Select Create New JAR Module in Application.</li></div><a name="wp81131"> </a><div class="pSmartList2"><li>Enter <code class="cCode">WarehouseJAR</code> under JAR Name.</li></div><a name="wp81132"> </a><div class="pSmartList2"><li>Click Edit.</li></div><a name="wp81133"> </a><div class="pSmartList2"><li>Navigate to <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/warehouse/</code>.</li></div><a name="wp81134"> </a><div class="pSmartList2"><li>Select <code class="cCode">Warehouse.class</code>, <code class="cCode">WarehouseBean.class</code>, and <code class="cCode">WarehouseHome.class</code>.</li></div><a name="wp81135"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp81136"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp81137"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp81138"> </a><div class="pSmartList1"><li>In the General screen:</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81139"> </a><div class="pSmartList2"><li>Select <code class="cCode">WarehouseBean</code> under Enterprise Bean Class.</li></div><a name="wp81140"> </a><div class="pSmartList2"><li>Enter <code class="cCode">WarehouseEJB</code> under Enterprise Bean Name.</li></div><a name="wp81141"> </a><div class="pSmartList2"><li>Select Stateful Session under Enterprise Bean Type.</li></div><a name="wp81142"> </a><div class="pSmartList2"><li>Select <code class="cCode">WarehouseHome</code> under Remote Home Interface.</li></div><a name="wp81143"> </a><div class="pSmartList2"><li>Select <code class="cCode">Warehouse</code> under Remote Interface.</li></div><a name="wp81144"> </a><div class="pSmartList2"><li>Select Next.</li></div></ol></div><a name="wp81145"> </a><div class="pSmartList1"><li>In the Configuration Options screen, select No under Expose Bean as Web Service End Point.</li></div><a name="wp81340"> </a><div class="pSmartList1"><li>Click Finish.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -