📄 using the j2ee connector architecture common client interface.htm
字号:
// to invoke the ADDACCT stored procedure ix.execute(iSpec,iRec); // Close the connection to the database. closeCCIConnection(con);}catch (ResourceException ex) { ex.printStackTrace();}}</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><h3>Executing a Local Transaction</h3><p>The previous sections showed how to program with the CCI from an EJB that uses container-managed persistence. Because the EJB container ensured that the database access would take place within the context of a transaction, an <code>AcctManage</code> bean using container-managed persistence did not have to concern itself with these matters. The container handled starting and ending the transaction, making sure that the database record was updated without interference from other processes. </p><p>However, if your bean uses bean-managed persistence, then it must handle its own transaction. An enterprise bean with bean-managed persistence uses methods from the <code>LocalTransaction</code> interface to handle local transactions. </p><p>Keep in mind that resource managers typically support two types of transactions: XA transactions and local transactions. XA transactions refer to the Java mapping of the X/Open XA protocol. They are often called global transactions, because they can span multiple resource managers. Typically, XA transactions are managed by an external transaction manager located on the application server. </p><p>Local transactions, on the other hand, are managed by the resource manager itself, and you can use the CCI API to handle a local transaction. To ensure the integrity of the data, add a record to the database from within the context of a transaction. To illustrate, look at the modified the <code>AcctManage</code> bean's <code>insertAcct</code> method that uses the CCI to start and commit, or rollback, a local transaction. </p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre>public void insertAcct(String acctName, int amt) { try { Connection con = getCCIConnection(); Interaction ix = con.createInteraction(); CciInteractionSpec iSpec = new CciInteractionSpec(); iSpec.setFunctionName("ADDACCT"); iSpec.setSchema(user); iSpec.setCatalog(null); RecordFactory rf = cf.getRecordFactory(); IndexedRecord iRec = rf.createIndexedRecord( "InputRecord"); boolean flag = iRec.add(acctName); flag = iRec.add(new Integer(amt)); // Obtain a reference to a transaction // context LocalTransaction transaction = con.getLocalTransaction(); // Start the transaction transaction.begin(); try { ix.execute(iSpec, iRec); // The operation was successful; // commit the transaction transaction.commit(); } catch (ResourceException e) { // An error occurred; rollback // the transaction transaction.rollback(); } closeCCIConnection(con);}catch(ResourceException ex) { ex.printStackTrace();}}</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>As you can see, most of the code of the <code>insertAcct</code> method is identical to the container-managed persistence version: Both versions do the following, in the same order:</p><ul><li>Obtain a connection to the database.</li><li>Create an <code>Interaction</code> object.</li><li>Create and define properties for an object that implements the <code>InteractionSpec</code> interface. </li><li>Use the <code>ConnectionFactory</code> to obtain a reference to the <code>RecordFactory</code>.</li><li>Create an IndexedRecord input Record and sets the values for the two elements in the <code>Account</code> record: account name and amount.</li></ul><p>At this point, the bean-managed persistence version deviates from the container-managed version. Before invoking the <code>Interaction</code> object's <code>execute</code> method, the bean first obtains a reference to a transaction. To do this, it invokes the <code>Connection</code> object's<code>getLocalTransaction</code> method and instantiates a new <code>LocalTransaction</code> object. The <code>LocalTransaction</code> object provides the context for the transaction. Then, the bean starts the transaction by invoking the <code>LocalTransaction</code> object's <code>begin</code> method.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... // Obtain a reference to a transaction context LocalTransaction transaction = con.getLocalTransaction(); // Start the transaction transaction.begin(); ...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>The bean can now safely do its transactional work. In this case, it invokes the Interaction object's <code>execute</code> method to execute the stored procedure <code>ADDACCT</code> and insert the new Account record into the database. The call to <code>execute</code> is the same as in the container-managed version, except that it is made from within a <code>try</code> block to catch any errors. If the call to <code>execute</code> is successful, then the bean commits the transaction. Otherwise, it rolls back the transaction to undo its actions. When a transaction is rolled back, any changes made to the database are undone and the database is restored to the state it was in prior to the start of the transaction. </p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... try { ix.execute(iSpec, iRec); transaction.commit(); } catch (ResourceException e) { transaction.rollback(); } // Close the connection ...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>The <code>insertAcct</code> method finishes by closing the connection.</p><h3>Writing a CCI Client</h3><p>A client application that relies on a CCI resource adapter is very much like any other J2EE client application. The AcctManageClient application uses the methods of the <code>AcctManageEJB</code> session bean to access the Account table in the underlying database. AcctManageClient invokes the <code>AcctManage getAcctAmt</code> method to read account amounts from the Account table and the <code>insertAcct</code> method to add records to the table.</p><h2>Conclusion</h2><p>By now, you should have a good understanding of how to use the J2EE Connector architecture's Common Client Interface API. You've seen how to use the different interfaces and classes defined by theCCI to access a resource adapter for an underlying database or EIS. You've also seen how to usethese methods to invoke procedures stored in the EIS, and to pass parameters to these procedures andreceive values back.</p><p>The Connector architecture and the CCI are currently supported by asignificant number of J2EE-compliant EIS vendors and tools vendors. It isanticipated that most, if not all, J2EE-compliant EIS vendors will supportthis technology. As its use becomes more widespread, and particularly asmore and more tools vendors build support of the CCI into their tools, itwill become the standard for J2EE enterprise application connectivity.</p><p><img src="/images/coffeecup13x10.gif" width="13" height="10" alt="Coffecup Logo"></p><h4>About the Author</h4><p><em><strong>Beth Stearns</strong></em> is the principal partner of ComputerEase Publishing, acomputer consulting firm she founded in 1982. Her client list includes SunMicrosystems, Inc., Silicon Graphics, Inc., Oracle Corporation, and XeroxCorporation. Among her publications are the "Java NativeInterface" chapter in "The Java Tutorial Continued" book in the AddisonWesley Java series, "The EJB Programming Guide" for Inprise Corporation,and "UnderstandingEDT", a guide to Digital Equipment Corporation's text editor. Mostrecently, she is co-author with Vlada Matena of the forthcoming AddisonWesley Java series book, "Applying Enterprise JavaBeans: Component-BasedDevelopment for the J2EE Platform."</p><!-- BEGIN FRAGMENT | HR TAG --><div class="contentdivider"><table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4"><tr><td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td></tr></table></div><!-- END FRAGMENT | HR TAG --><!-- ================ --><!-- End Main Content --><!-- ================ --><!-- =================== --><!-- END OF MAIN CONTENT --><!-- =================== --><!--stopindex--><!-- END CENTRAL COLUMN COMPONENTS --></td><td valign="top"><!-- BEGIN RIGHT COLUMN COMPONENTS --><!-- END RIGHT COLUMN COMPONENTS --></td></tr><!-- BEGIN SPACER ROW --><tr><td><img src="/im/a.gif" width="560" height="1" border="0" alt=" " /></td><td><img src="/im/a.gif" width="170" height="1" border="0" alt=" " /></td></tr><!-- END SPACER ROW --></table><!-- END WRAPPER TABLE, 2 COLUMN, MAIN/RIGHT --><!-- BEGIN VNV5 FOOTER --><table border="0" cellpadding="0" cellspacing="10" width="100%"><tr><td><table border="0" cellpadding="0" cellspacing="0" width="100%" class="vatop"><tr><td colspan="4" valign="top" class="grey3"><img src="/im/a.gif" width="1" height="2" border="0" alt=" " /></td></tr><tr><td><img src="/im/a.gif" width="190" height="1" border="0" alt=" " /><br /><a href="http://www.sun.com"><img src="/im/logo_sun_small_sdn.gif" width="61" height="29" border="0" alt=" " vspace="5"/></a></td><td width="100%" valign="top"><img src="/im/a.gif" width="350" height="1" border="0" alt=" " /><br /><div class="footer"><a href="http://developers.sun.com/dispatcher.jsp?uid=6910015">Company Info</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910010">About This Site</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910011">Press</a> | <a href="http://developers.sun.com/contact/index.jsp">Contact Us</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910014">Employment</a><br /><a href="http://developers.sun.com/dispatcher.jsp?uid=6910012">How to Buy</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910009">Licensing</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910013">Terms of Use</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910016">Privacy</a> | <a href="http://developers.sun.com/dispatcher.jsp?uid=6910017">Trademarks</a><br /><span class="sp10"> </span><br /><br /><span class="sp10"> </span><br />Copyright 1994-2004 Sun Microsystems, Inc.</div></td><td><img src="/im/a.gif" width="40" height="1" border="0" alt=" " /></td><td valign="top"><div class="footer"><b><a href="http://developers.sun.com/dispatcher.jsp?uid=6910010">A Sun Developer Network Site</a></b></div><div class="footer"><img src="/im/a.gif" width="170" height="1" border="0" alt=" " /><br />Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this <a href="http://developers.sun.com/dispatcher.jsp?uid=6910008">License</a>.<br /><span class="sp5"> </span><br /><a href="http://developers.sun.com/dispatcher.jsp?uid=6910006"><img src="/im/button_xml.gif" width="36" height="14" align="top" border="0" alt="XML" /></a> <a href="http://developers.sun.com/dispatcher.jsp?uid=6910007">Content Feeds</a></div></td></tr><tr><td colspan="4" valign="top" class="grey3"><img src="/im/a.gif" width="1" height="2" border="0" alt=" " /></td></tr></table></td></tr></table><!-- END VNV5 FOOTER --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -