📄 bmp4.html
字号:
for a listing, and <CODE>AuctionItemBean</CODE> to add new auction
items to the database.
<P>
The transacton begins in the <CODE>insertItem</CODE> method
with the account debit and ends when the entire
transaction either commits or rolls back. The entire transaction including the
50 cents debit rolls back if the auction item is <CODE>null</CODE>
(the insertion failed), or if an exception is caught.
If the auction item is not <CODE>null</CODE>
and the insertion succeeds, the entire transaction including the 50
cents debit commits.
<A NAME="code"></A>
<H4>Code</H4>
To use session synchronization, a session Bean implements the
<CODE>SessionSynchronzation</CODE> interface and its three
methods, <CODE>afterBegin</CODE>, <CODE>beforeCompletion</CODE>,
and <CODE>afterCompletion</CODE>. This example adapts the
<A HREF="./Code/seller/SessSynch/SellerBean.java">SellerBean.java</A>
code to use session synchronization.
</FONT>
<PRE>
public class SellerBean implements SessionBean,
SessionSynchronization {
private transient SessionContext ctx;
private transient Properties p = new Properties();
private transient boolean success = true;
public void afterBegin() {}
public void beforeCompletion() {
if (!success ) {
ctx.setRollbackOnly();
}
}
public void afterCompletion(boolean state) {}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
<STRONG><CODE>afterBegin</CODE></STRONG>: The container calls this
method before the debit to notify the session Bean a new transaction
is about to begin. You can implement this method to do any preliminary
database work that might be needed for the transaction. In this example,
no preliminary database work is needed so this method has no implementation.
<P>
<STRONG><CODE>beforeCompletion</CODE></STRONG>: The container calls
this method when it is ready to write the auction item and debit to
the database, but before it actually does (commits). You can implement
this method to write out any cached database updates or roll back the
transaction. In this example, the method calls the <CODE>setRollbackOnly</CODE>
method on its session context in the event the <CODE>success</CODE>
variable is set to <CODE>false</CODE> during the transaction.
<P>
<STRONG><CODE>afterCompletion</CODE></STRONG>: The container calls
this method when the transaction commits. A <CODE>boolean</CODE> value
of <CODE>true</CODE> means the data committed and <CODE>false</CODE> means
the transaction rolled back. The method uses the <CODE>boolean</CODE> value to
determine if it needs to reset the Bean's state in the case of a rollback.
In this example, there is no need to reset the state in the event of
a failure.
<P>
Here is the <CODE>insertItem</CODE> method with comments showing
where the points where the <CODE>SessionSynchronization</CODE>
methods are called.
</FONT>
<PRE>
public int insertItem(String seller,
String password,
String description,
int auctiondays,
double startprice,
String summary)
throws RemoteException {
try{
Context jndiCtx = new InitialContext(p);
RegistrationHome rhome =
(RegistrationHome) sCtx.lookup("registration");
RegistrationPK rpk=new RegistrationPK();
rpk.theuser=seller;
Registration newseller=rhome.findByPrimaryKey(rpk);
if((newseller == null) ||
(!newseller.verifyPassword(password))) {
return(Auction.INVALID_USER);
}
//Call to afterBegin
newseller.adjustAccount(-0.50);
AuctionItemHome home = (AuctionItemHome)
jndiCtx.lookup("auctionitems");
AuctionItem ai= home.create(seller,
description,
auctiondays,
startprice,
summary);
if(ai == null) {
success=false;
return Auction.INVALID_ITEM;
}
else {
return(ai.getId());
}
}catch(Exception e){
System.out.println("insert problem="+e);
success=false;
return Auction.INVALID_ITEM;
}
//Call to beforeCompletion
//Call to afterCompletion
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="commit"></A>
<H3>Transaction Commit Mode</H3>
If you configure the JDBC services to transaction commit mode, you can
have the Bean manage the transaction. To set the JDBC services to commit,
call <CODE>con.setAutoCommit(false)</CODE> on your JDBC connection.
Not all JDBC drivers support commit mode, but to have the Bean control and
manage transactions, you need a JDBC driver that does.
<P>
Transaction commit mode lets you add code that creates a safety net around a
sequence of dependent operations. The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Transaction API (JTA) provides
the hooks you need to create that safety net. But, if you are using
the Enterprise JavaBeans architecture, you can do it with a lot
less code. You only have to configure the Enterprise JavaBeans server,
and specify where the transaction starts, stops, rolls back, and commits
in your code.
<A NAME="config"></A>
<H4>Server Configuration</H4>
Configuring the Enterprise JavaBeans server involves specifying
the following settings in a configuration file for each Bean:
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">An isolation level to specify how exclusive a
transaction's access to shared data is.</FONT>
<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">A transaction attribute to specify how to handle
Bean-managed or container-managed transactions that continue in another Bean.</FONT>
<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">A transaction type to specify whether the transaction is
managed by the container or the Bean.</FONT>
</UL>
<P>
For example, you would specify these settings for the
<A HREF="http://weblogic.beasys.com/index.htm">BEA Weblogic</A>
server in a <CODE>DeploymentDescriptor.txt</CODE> file
for each Bean.
<P>
Here is the part of the <CODE>DeploymentDescriptor.txt</CODE>
for <CODE>SellerBean</CODE> that specifies the isolation level
and transaction attribute. A description of the settings
follows.
</FONT>
<PRE>
(controlDescriptors
(DEFAULT
isolationLevel TRANSACTION_SERIALIZABLE
transactionAttribute REQUIRED
runAsMode CLIENT_IDENTITY
runAsIdentity guest
); end DEFAULT
); end controlDescriptors
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
Here is the equivalent Enterprise JavaBeans 1.1 extended markup
language (XML) description that specifies the transaction type.
In this example <CODE>SellerBean</CODE> is container managed.
</FONT>
<PRE>
<container-transaction>
<method>
<ejb-name>SellerBean<ejb-name>
<method-name>*<method-name>
<method>
<transaction-type>Container<transaction-type>
<trans-attribute>Required<trans-attribute>
<container-transaction>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
In this example, <CODE>SellerBean</CODE> is Bean managed.
</FONT>
<PRE>
<container-transaction>
<method>
<ejb-name>SellerBean<ejb-name>
<method-name>*<method-name>
<method>
<transaction-type>Bean<transaction-type>
<trans-attribute>Required<trans-attribute>
<container-transaction>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="attr"></A>
<STRONG>Transaction Attribute Descriptions</STRONG>:
An enterprise Bean uses a <EM>transaction attribute</EM>
to specify whether a Bean's transactions are managed by
the Bean itself or by the container, and how to handle
transactions that started in another Bean.
<P>
The Enterprise JavaBeans server can control only one transaction
at a time. This model follows the example set by the OMG
Object Transaction Service (OTS), and means the current Enterprise
JavaBeans specification does not provide a way to nest transactions.
A nested transaction is a new transaction that starts from within
an existing transaction. While transaction nesting is not allowed,
continuing an existing transaction in another Bean is okay.
<P>
When a Bean is entered, the server creates a transaction
context to manage the transaction. When the transaction is
managed by the Bean, you access the context to begin,
commit, and rollback the transaction as needed.
<P>
Here are the transaction attributes with a brief description for
each one. The attribute names changed betweein the 1.0 and
1.1 versions of the Enterprise JavaBeans specification.
<P>
<TABLE>
<TR><TH ALIGN="LEFT" WIDTH="200"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">1.1 Specification</FONT></TH>
<TH ALIGN="LEFT"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">1.0 Specification</FONT></TH></TR>
</TABLE>
<HR>
<TABLE>
<TR><TD WIDTH="200"><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>REQUIRED</CODE></FONT></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>TX_REQUIRED</CODE></FONT></TD></TR>
</TABLE>
<TABLE>
<TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
Container-managed
transaction.
The server either starts and manages a new
transaction on behalf of the user or continues using the transaction that
was started by the code that called this Bean.</FONT>
</TD></TR>
</TABLE>
<P>
<TABLE>
<TR><TD WIDTH="200"><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>REQUIRESNEW</CODE></FONT></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>TX_REQUIRED_NEW</CODE></FONT></TD></TR>
</TABLE>
<TABLE>
<TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
Container-managed transaction.
The server starts and manages a new transaction. If an existing transaction
starts this transaction, it suspends until this transaction completes.</FONT>
</TD></TR>
</TABLE>
<P>
<TABLE>
<TR><TD WIDTH="200"><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>Specified as Bean transaction-type in
deployment descriptor</CODE></FONT></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>TX_BEAN_MANAGED</CODE></FONT></TD></TR>
</TABLE>
<TABLE>
<TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
Bean-managed transaction.
You access the transaction context to begin, commit, or rollback
the transaction as needed.</FONT>
</TD></TR>
</TABLE>
<P>
<TABLE>
<TR><TD WIDTH="200"><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>SUPPORTS</CODE></FONT></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>TX_SUPPORTS</CODE></FONT></TD></TR>
</TABLE>
<TABLE>
<TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
If the code calling this Bean has a transaction running,
include this Bean in that transaction.</FONT>
</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -