To use automatic persistence, you must define a state datatype to hold your component’s instance state, implement accessor methods, and choose a storage component.
Configuring a stateful component to use automatic
persistence
Display the component’s properties, then click the Persistence tab. Configure the Persistence/General tab settings as follows:
Setting |
Value |
|---|---|
Persistence |
Choose Automatic Persistent State. |
State |
Enter the name of an IDL structure that contains your component’s state data, for example: TheCart::CartState “Defining the IDL state type” describes how to create this structure. |
State methods |
Enter the names of the component methods
that retrieve and apply an instance’s state data. If you specify
no value, the default is |
Storage component |
Specify the name of the storage component, as described in “Storage components”. |
Connection cache |
If using database storage, enter the name of a JDBC connection cache that connects to the database. The cache must have by-name access enabled. |
Table |
If using database storage, enter the name of a database table where the serialized data is to be stored. If you use Sybase Adaptive Server Enterprise or Adaptive Server Anywhere, EAServer creates the table if it does not exist. When using another data server, you or your database administrator (DBA) must create the table manually. The table must have the schema described in “Table schema for binary storage”. |
Define the IDL state type as described below.
Add code to retrieve, apply, and modify the state data as described below.
Regenerate stubs and skeletons for the component—this step will generate the Java classes for the IDL state types.
If using in-memory storage, configure the component’s Mirror Cache properties and cluster to support in-memory failover, as described in “Requirements for in-memory stateful failover”.
The IDL state type is a structure that must hold all the session data for the bean. The following IDL module shows example types used for a shopping cart component:
module TheCart
{
// This is the state type, an IDL structure that holds customer data
// and the collection (sequence) of items in the cart.
struct CartState
{
string name;
string address;
string phone;
::TheCart::ShoppingCartItems items;
};
// "ShoppingCartItems" is the sequence to hold items in the cart:
typedef sequence < ::TestInMemoryFailvoer::ShoppingCartItem > ShoppingCartItems;
// "ShoppingCartItem" holds the data for one item in the cart:
struct ShoppingCartItem
{
string item;
long quantity;
}
}
In the State field on the Persistence/General tab, you can enter the name of an IDL structure that does not exist. EAServer Manager creates the structure when you close the Component Properties dialog box. Afterwards, navigate to the module definition under the top-level IDL folder and edit the structure definition. For information on editing IDL in EAServer Manager, see Chapter 5, “Defining Component Interfaces.”
Make the following changes to the component implementation:
Add the state accessor methods Add methods with the names specified in the State Methods field in the Persistence/General tab in the Component Properties dialog box. Also add an instance variable of the generated state type. For example:
import TheCart.CartState;
private CartState data = new CartState();
private int lastItem = 0;
TheCart.CartState getState()
{
return data;
}
void setState(TheCart.CartState state)
{
data = state;
}
Add code to initialize the state type Add code to your comonents’ ctsCreate method to initialize the state type. For example:
public void ctsCreate(
name, address, phone, capacity)
{
data.name = name;
data.address = address;
data.phone = phone;
data.items = new ShoppingCartItem[capacity];
}
These assignments correspond to the IDL structure fields in the example above. The items IDL sequence is represented by an array in Java.
Add code to access the state data from business methods Where appropriate, the bean’s business methods should read and write from the state type. For example, these methods add an item to the shopping cart and return the customer’s name, respectively:
public void addItem(String item, int quantity)
{
....
lastItem++;
data.items[lastItem]
= new ShoppingCartItem(item, quantity);
....
}
public String getCartName()
{
return data.name;
}
| Copyright © 2005. Sybase Inc. All rights reserved. |
|
|