Entity Beans  «Prev  Next»

Bean Managed Persistence - Exercise

Create the client for a bean-managed persistent entity bean

Objective:Write and test the code for the client of the BankAccount entity bean with bean-managed persistence.

Reminder

Referring to hard copy of an exercise can be helpful if the exercise is long or has many steps.
You may want to print the exercise or refer to your course PDF download before starting.

Exercise scoring

To receive full credit, write the BankCustomer client to access the entity bean BankAccount object. Once you have completed your answer, submit the exercise.

Background/overview

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;

}

You should create a client for this bean using the skeleton provided. The client will implement the findByPrimaryKey() method in addition to the methods that the client had when it existed as a session bean. You should then test this bean using the BankAccount class files provided.

Download files

From the exercise skeleton files available on the Resources page, move the following files from the EJB-part2/skeletons/project-2 directory into a directory named bmt-bank. Make this directory current.

BankAccount.class
BankCustomer2.java
runClient.bat
BankAccountBean.class
NSFException.class
runClient2.bat
BankAccountHome.class
compileClient.bat
runIJ.bat
BankCustomer.java
createTable.sql

Instructions

Edit the BankCustomer.java skeleton file so that it creates a new BankAccount and then find that BankAccount using the findByPrimaryKey() method. It then will verify that the bean works by crediting and debiting the account and displaying the account information.
When building the application and the DD in the deploytool, indicate the following:
  1. Name the application BankAccountApp when creating the application.
  2. In step 3 indicate that it is an entity bean.
  3. In step 4 indicate that it is bean-managed persistence and the primary key class is java.lang.String. Do not press tab when you have changed the primary key to String. There is a bug in the beta 1.2 (November 99) that causes the deploytool to error if the cursor enters the primary key field name field with bean-managed transactions.
  4. In step 7, click Add. Replace the ResourceRefName with "jdbc/BankAccountDB" which is the name of the database used in this application. Do not change the other two fields that are part of the entry you are adding.
  5. In step 9, indicate that the server manages the transactions. Indicate "Required" for the credit() and debit() methods.
  6. In step 11, add NSFException.class to the DD.
  7. When deploying the bean, set the JNDI name for the BankAccountDB to the data "jdbc/Cloudscape. " Cloudscape is a full function database that is provided as part of the J2EE reference implementation.
  8. Set the JNDI name of the bean to "BankAccount."
  9. Before running the client do the following:
    1. 9a. In a separate window start the Cloudscape database with the command "cloudscape -start".
    2. 9b. Create the table (or clean out an existing one) by running the provided batch file as follows: "runIJ createTable.sql". This will create the database. Re-run this command each time you re-run the application otherwise you will get a duplicate row error when you try to create the accounts.
    3. 9c. Run the client BankCustomer with the script file "runClient."
  10. Complete the BankCustomer2.java skeleton so that it finds the bank account created in BankCustomer.java using the findByPrimaryKey() method.
  11. Access the balance information and display it to the user to illustrate that the entity object was created and found and that the balance was preserved across clients (BankCustomer.java and BankCustomer2.java).
  12. Credit and debit this account again to verify that it still works.
  13. Remove the bean instance. This will remove the entity object from the database. There is no need to change the deployed bean.

Hints

Remember to start the database before running this application. The command to do this is "cloudscape -start". Be sure to create the table before you start, and to rerun this command after every iteration. If you don't, the BankCustomer client will bomb. Do not forget to set the transaction controls to "required' for both the credit and the debit methods. If you do not, the database will not be updated.
If you need to start from scratch, shutdown the deploytool and the j2ee implementation and then run the command "cleanup". This will remove all the temporary files used by the server or the deploytool.
If you have problems, don't 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.

Submitting your exercise

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