T R A N S A C T I O N S
{
As you see on the top to define the default transaction attribute is as simple as adding the
XDoclet tag ejb:transaction . Below you see how you can specify a transaction attribute for
a specific method (which does not exists like this in the real example).
Now to the more interesting part: the client. In this example there is only a remote Java
client, which only differs from the web client in the way that the remote client needs a user
transaction for remote clients. The code looks like this:
InitialContext lContext = new InitialContext();
Object lObject = lContext.lookup( "ejb/bank/BankSession" );
BankSessionHome lBankHome = (BankSessionHome)
PortableRemoteObject.narrow( lObject, BankSessionHome.class );
BankSession lBankSession = lBankHome.create();
BankData lBank = lBankSession.createBank( "Test", "1234 Test Avenue, Test City" );
lObject = lContext.lookup( "ejb/bank/TellerSession" );
TellerSessionHome lTellerHome = (TellerSessionHome)
PortableRemoteObject.narrow( lObject, TellerSessionHome.class );
TellerSession lTeller = lTellerHome.create();
CustomerData lCustomerA = lTeller.createCustomer( lBank.getId(), "Andreas Schaefer", 100 );
AccountData lCheckingAccountA = lTeller.getAccount( lCustomerA.getId(), Constants.CHECKING
);
AccountData lSavingsAccountA = lTeller.createAccount( lCustomerA.getId(), Constants.SAVING,
250 );
lTeller.transfer( lSavingsAccountA.getId(), lCheckingAccountA.getId(), 125 );
CustomerData lCustomerB = lTeller.createCustomer( lBank.getId(), "Marc Fleury", 500 );
AccountData lCheckingAccountB = lTeller.getAccount( lCustomerA.getId(), Constants.CHECKING
);
UserTransaction lTransaction = (UserTransaction) lContext.lookup( "UserTransaction" );
lTransaction.begin();
try {
lTeller.withdraw( lCheckingAccountA.getId(), 165 );
lTeller.deposit( lCheckingAccountB.getId(), 165 );
lTransaction.commit();
}
catch( Exception e ) {
lTransaction.rollback();
}
The client first creates a bank and look up for a Teller Session Bean. Then trough the teller
it creates a customer and a second account, transfers money from one account to another.
For this transfer there is no user transaction necessary because the transfer method creates
it own transaction (attribute is set to Required ). Now the Teller creates another customer
and retrieves a user transaction because the withdrawal on one's customer's account and the
deposit on the other's customer's account is not part of a transaction. But here the client
wants to have the withdrawal and deposit either done completely or not done at all.
Therefore the client starts the user transaction (lTransaction.begin()), makes the withdrawal
and deposit and commits the transfer afterwards (lTransaction.commit()) if no exception is
61
footer
Our partners:
PHP: Hypertext Preprocessor Cheap Web Hosting
JSP Web Hosting
Ontario Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Java Hosting
Cheapest Hosting
Visionwebhosting.net Business web hosting division of Vision Web Hosting Inc.. All rights reserved