| 
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 RegistrationBeanto use
Bean-managed persistence to handle database access and manage transactions. 
 Member VariablesA container-managed environment needs clues about which variables
are for persistent storage and which are not. In the JavaTM
programming language, thetransientkeyword indicates 
variables to not include when data in an object is serialized and
written to persistent storage. In theRegistrationBean.javaclass, 
theEntityContextvariable is markedtransientto indicate that its data not be written to the underlying storage
medium.
EntityContextdata 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
declaredpublicso 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 MethodThe Bean'sejbCreatemethod is called
by the container after the client program calls thecreatemethod on theremoteinterface 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 MethodsAn entity Bean has an associatedEntityContextinstance 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 MethodThe Bean'sejbLoadmethod is called by the
container to load data from the underlying storage medium. 
This would be necessary
whenBidderBeanorSellerBeanneed 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 MethodThe Bean'sejbStoremethod 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 PoolingLoading 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 DescriptorThe 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
calledejbPool. TheattributeMapcontains
the Enterprise Bean variable on the left and the associated database field on 
the right.XML Deployment DescriptorIn Enterprise JavaBeans 1.1, the deployment descriptor usesXML. The equivalent configuration inXMLis 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 theREGISTRATIONtable.
[TOP]
 |