Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Writing Advanced Applications
Chapter 2 Continued: Examining a Container-Managed Bean

[<<BACK] [CONTENTS] [NEXT>>]

This section walks through the RegistrationBean.java code to show how easy it is to have the container manage persistent data storage to an underlying medium such as a database (the default). Chapter 3 modifies RegistrationBean to use Bean-managed persistence to handle database access and manage transactions.


Member Variables

A container-managed environment needs clues about which variables are for persistent storage and which are not. In the JavaTM programming language, the transient keyword indicates variables to not include when data in an object is serialized and written to persistent storage. In the RegistrationBean.java class, the EntityContext variable is marked transient to indicate that its data not be written to the underlying storage medium.

EntityContext data is not written to persistent storage because its purpose is to provide information on the container's runtime context. It, therefore, does not contain data on the registered user and should not be saved to the underlying storage medium. The other variables are declared public so the container in this example can discover them using the Reflection API.

  protected transient EntityContext ctx;
  public String theuser, password, creditcard, 
           emailaddress;
  public double balance;

Create Method

The Bean's ejbCreate method is called by the container after the client program calls the create method on the remote interface and passes in the registration data. This method assigns the incoming values to the member variables that represent user data. The container handles storing and loading the data, and creating new entries in the underlying storage medium.
public RegistrationPK ejbCreate(String theuser,
                                String password,
                                String emailaddress,
                                String creditcard)
        throws CreateException, RemoteException {

  this.theuser=theuser;
  this.password=password;
  this.emailaddress=emailaddress;
  this.creditcard=creditcard;
  this.balance=0;

Entity Context Methods

An entity Bean has an associated EntityContext instance that gives the Bean access to container-managed runtime information such as the transaction context.
  public void setEntityContext(
                javax.ejb.EntityContext ctx)
                throws RemoteException {
    this.ctx = ctx;
  }

  public void unsetEntityContext() 
                throws RemoteException{
    ctx = null;
  }

Load Method

The Bean's ejbLoad method is called by the container to load data from the underlying storage medium. This would be necessary when BidderBean or SellerBean need to check a user's ID or password against the stored values.

Note: Not all Bean objects are live at any one time. The Enterprise JavaBeansTM server might have a configurable number of Beans that it keeps in memory.

This method is not implemented because the Enterprise JavaBeans container seamlessly loads the data from the underlying storage medium for you.

  public void ejbLoad() throws RemoteException {}

Store Method

The Bean's ejbStore method is called by the container to save user data. This method is not implemented because the Enterprise JavaBeans container seamlessly stores the data to the underlying storage medium for you.
  public void ejbStore() throws RemoteException {}

Connection Pooling

Loading data from and storing data to a database can take a lot time and reduce an application's overall performance. To reduce database connection time, the BEA Weblogic server uses a JDBCTM connection pool to cache database connections so connections are always available when the appalication needs them.

However, you are not limited to the default JDBC connection pool. You can override the Bean-managed connection pooling behaviour and substitute your own. Chapter 8: Performance Techniques explains how.

Deployment Descriptor

The remaining configuration for a container-managed persistent Beans occurs at deployment time. The following is the text-based Deployment Descriptor used in a BEA Weblogic Enterprise JavaBeans server.

Text Deployment Descriptor

  (environmentProperties

    (persistentStoreProperties
      persistentStoreType          jdbc

      (jdbc
        tableName                  registration
        dbIsShared                 false
        poolName                   ejbPool
        (attributeMap
          creditcard               creditcard
          emailaddress             emailaddress
          balance                  balance
          password                 password
          theuser                  theuser
        ); end attributeMap
      ); end jdbc
    ); end persistentStoreProperties
  ); end environmentProperties

The deployment descriptor indicates that storage is a database whose connection is held in a JDBCTM connection pool called ejbPool. The attributeMap contains the Enterprise Bean variable on the left and the associated database field on the right.

XML Deployment Descriptor

In Enterprise JavaBeans 1.1, the deployment descriptor uses XML. The equivalent configuration in XML is as follows:
<persistence-type>Container</persistence-type>
<cmp-field><field-name>creditcard
    </field-name></cmp-field>
<cmp-field><field-name>emailaddress
    </field-name></cmp-field>
<cmp-field><field-name>balance
    </field-name></cmp-field>
<cmp-field><field-name>password
    </field-name></cmp-field>
<cmp-field><field-name>theuser
    </field-name></cmp-field>
<resource-ref>
<res-ref-name>registration</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
The container managed-fields here map directly to their counterpart names in the database table. The container resource authorization (res-auth) means the container handles the database login for the REGISTRATION table.

[TOP]


[ This page was updated: 13-Oct-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.