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

📄 bmp4.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>Writing Advanced Applications, Chapter 3: Transaction Management</TITLE>
  <META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
  <META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
  <META NAME="OWNER" CONTENT="Editorial/JDC">
  <META NAME="revision" CONTENT="@(#)proj.src   1.3 05/11/99  JDC">
</HEAD>

<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->

<!-- Start PageTop Insert -->

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
  <TR ALIGN="CENTER" VALIGN="TOP">
    <TD WIDTH="157" ALIGN="LEFT">
    <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
    <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
    <BR>
    <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>

    <TD>

    <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
      <TR>
        <TD ALIGN="RIGHT">
        <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
        
        <TD WIDTH="100%">
        
        <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
          <TR>
            <TD BGCOLOR="#CC9966" WIDTH="100%">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD>
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD BGCOLOR="#CC9966">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD>
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD BGCOLOR="#CC9966">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD>
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD BGCOLOR="#CC9966">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
        </TABLE>
        </TD>
        
        <TD ALIGN="LEFT">
        <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>

        <TD>
        
        <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
          <TR>
            <TD VALIGN="CENTER">
            <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
              
            <TD VALIGN="CENTER">
            <FONT FACE="Helvetica" SIZE="1">
            <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
            
            <TD VALIGN="CENTER">
            <INPUT TYPE="image" SRC="/images/search.button.gif" 
              value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
          </TR>
        </TABLE>
        </TD>
      </TR>
    </TABLE>
    </FORM>
    <P>

    <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
      <TR VALIGN="TOP">
        <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" 
          HEIGHT="18" ALT=""></TD>
          
        <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
        <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
      </TR>
      
      <TR VALIGN="TOP">
        <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
      </TR>
      
      <TR VALIGN="TOP">
        <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
      </TR>
      
      <TR VALIGN="TOP">
        <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
      </TR>
    </TABLE>

    </TD>
  </TR>
</TABLE>

<!-- End PageTop Insert -->

<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">

<!-- tab categories -->

<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>

<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News &amp; Events from Everywhere"></A></TD>
</TR>

<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>

<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>

<TR><TD>&nbsp;</TD></TR>


<!-- End NavBar Insert -->


<!-- START SUB-NAV -->

<TR>
  <TD><!-- INSERT SUB-NAV INFO -->
  </TD>
</TR>


<!-- END SUB-NAV -->

</TABLE>

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>

<!-- Template Version 2.0 -->

<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

  <A NAME="top"></A>
  <DIV ALIGN="RIGHT">
  <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
  <H2>Writing Advanced Applications<BR>
  <FONT SIZE="3">Chapter 3 Continued: Managing Transactions</FONT></H2>

  <FONT SIZE="-1">[<A HREF="bmp3.html">&lt;&lt;BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="bmp5.html">NEXT&gt;&gt;</A>]</FONT></DIV>

<P>
Wouldn't it be great if every operation your application attempts
succeeds? Unfortunately, in the multi-threaded world of
distributed applications and shared resources, this is not
always possible. 

<P>
Why? First of all, shared resources must maintain a consistent view of 
the data to all users. This means reads and writes have to be managed 
so users do not overwrite each other's changes, or transaction errors
do not corrupt data integrity. Also, if you factor in intermittent 
network delays or dropped connections, the potential for operations to fail 
in a web-based application increases as the number of users increases. 

<P>
If operation failures are unavoidable, the next best thing is to
recover safely, and that is where transaction management
fits in. Modern databases and transaction managers let you undo and
restore the state of a failed sequence of operations to ensure the
data is consistent for access by multiple threads.

<P>
This section adds code to <CODE>SellerBean</CODE> from the auction house 
example so it can manage its auction item insertion transaction beyond the 
default transaction management provided by its container. 
 
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#why">Why Manage Transactions?</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#synch">Session Synchronization</A></FONT>
<UL>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#cmt">Container-Managed Example</A></FONT>
        <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
        <A HREF="#code">Code</A></FONT>
</UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#commit">Transaction Commit Mode</A></FONT>
<UL>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#config">Server Configuration</A></FONT>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#attr">Transaction Attribute Descriptions</A></FONT>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#level">Isolation Level Descriptions</A></FONT>
	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
	<A HREF="#bmt">Bean-Managed Example</A></FONT>
</UL>
</UL>

<HR>

<A NAME="why"></A>
<H3>Why Manage Transactions?</H3>

When you access a databases using the JDBC<FONT SIZE="-2"><SUP>TM</SUP></FONT> application
programming interface (API), all operations are run with an 
explicit auto commit by default. This means any other
application viewing this data will see the updated data after 
each JDBC call. 

<P>
For simple applications this may be acceptable, but consider
the auction application and the sequences that occur when
<CODE>SellerBean</CODE> inserts an auction item. The user's 
account is first charged for listing the item, and the item is 
then added to the list of items up for auction. These
operations involve <CODE>RegistrationBean</CODE>
to debit the account and <CODE>AuctionItemBean</CODE> to add
the item to the auction list. 

<P>
In auto commit mode, if the auction item insertion fails, only
the listing is backed out, and you have to manually adjust the user's 
account to refund the listing charge. In the meantime, another 
thread might try to deduct from the same user's account, find no 
credit left, and abort when perhaps a few milliseconds later it would 
have completed.

<P> 
There are two ways to ensure the debit is backed out if the auction item 
insertion fails: 

<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Add session synchronization code to a container-managed session
Bean to gain control over transaction commits and roll backs. </FONT>

<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Configure JDBC services to transaction commit mode and add code to start, 
stop, commit, and rollback the transaction. This is a 
Bean-managed transaction and can be used with an entity or session Bean.</FONT>
</UL>

<A NAME="synch"></A>
<H3>Session Synchronization</H3>

A container-managed session Bean can optionally include session 
synchronization to manage the default auto commit provided by
the container. Session synchronization code lets the container notify 
the Bean when important points in the transaction are reached. Upon 
receiving the notification, the Bean can take any needed actions before 
the transaction proceeds to the next point. 

<BLOCKQUOTE>
<HR>
<STRONG>Note</STRONG>:
A session Bean using 
Bean-managed transactions does not need session synchronization 
because it is in full control of the commit.
<HR>
</BLOCKQUOTE>

<A NAME="cmt"></A>
<H4>Container-Managed Example</H4>

<CODE>SellerBean</CODE> is a session Bean that uses
<CODE>RegistrationBean</CODE> to check the user ID and password when
someone posts an auction item and debit the seller's account

⌨️ 快捷键说明

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