Distributed Transactions   «Prev  Next»

Bean-managed transactions - Exercise

Objective:Program bean-managed transactions

Background/overview

The Teller bean will provide some of the services provided by a real teller in a bank. Its interface is as follows:
public int mkAccount(String account, double initialBalance)
public void transfer(int from, int to, double amt)
public String getBalances()

The mkAccount() method creates a new account and returns an index to the just-created BankAccount instance that can be used in other method calls such as transfer().
transfer() will move an amount from one open account to another using their indexes. getBalances() returns a string containing the current balances of all the BankAccount instances that it has created.
As it may have been some time since you worked with the BankAccount bean, its remote interface is as follows:

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface BankAccount extends EJBObject {
public double getBalance() throws RemoteException;
public void credit(double amount) throws RemoteException;
public void debit(double amount)    
  throws RemoteException, NSFException;
public String getInfo() throws RemoteException;
}

In this exercise, you will write the bean-managed transaction Teller bean using the skeleton provided. The provided client will create two accounts using the mkAccount() method and then will transfer $100 from one to the other. Then it will display the current balances. The BankAccount bean instances will only exist during the life of the container, as they are currently implemented as session beans. In later project exercises, the BankAccount bean will store the balance in a database so its state is not discarded.

Download files

Download the following files from the zip file available on the < ahref="../resources/courses/jv409/">Resources page. Look in the EJB-part2/skeletons/project-1 directory named teller.

BankAccount.java
NSFException.java
compileClient.bat
BankAccountBean.java
Teller.java
compileEJB.bat
BankAccountHome.java
TellerBean.java
runClient.bat
BankCustomer.java
TellerHome.java

Exercise Instructions

Edit the TellerBean skeleton file to provide the functionality described in the overview. It should implement the mkAccount() method that creates a new instance of a BankAccount bean and returns an index that can be used to access it later. Store the remote references to the instance in an array and use the offset to it as the index. The client should have no idea how the teller is creating and managing the BankAccount instances.
Implement the transfer() method that moves $100 from one account to another. This method should get the UserTransaction object, start a transaction, move the $100 from one account to another, then commit the transaction. Make sure that there is not already a current transaction context before beginning the transaction. If there is any problem, roll back the transaction.
Implement the getBalances() method that returns a String containing the information about each of the BankAccount instances that currently exist in the Teller.
Create the application and name it "BankTellerApp." Generate the application using the deploytool and include both the Teller and the BankAccount beans in a single jar file. Do not forget to add the NSFException.class to the jar file. Make sure that in step 9 you indicate that transactions are managed by the EJB.
Deploy the application and then test it with the BankCustomer class provided. When you deploy the application, set the JNDI name for the Teller bean to "Teller" and the JNDI name for the BankAccount bean to "BankAccount."
When building and running the application, don't forget to the use the script files (compileEJB, compileClient,and runClient) after editing them for the local environment. Do not use low-level commands. Make sure that the runClient script file uses the correct jar file that was created by the server at deployment time.

Exercise Hints

This exercise does not provide a detailed explanation of the application, jar creation, and deployment steps for the Java 2 Enterprise Edition reference implementation. If you need to refresh yourself on these steps, please refer back to the detailed explanation in EJB Architecture and Session Beans, the first course in this series.
If you have problems getting the exercise to work, it is sometimes helpful to shutdown the j2ee platform and the deploytool, run the command "cleanup," and then restart them from scratch. If you have to change the code for the beans, you should delete the application, uninstall the bean, and start from scratch.
If you have problems, do not forget to look at the messages displayed in the j2ee server and the deploytool windows for an indication of what is going wrong. These will contain messages from your bean, the container, the server, and so on.
If you continue to have problems, refer back to the exercise hints for clues that may help you.

Submitting your exercise

When you have completed the exercise, paste the code for the TellerBean.java into the text box below. Click the Submit button to submit the exercise.