Consultor Eletrônico



Kbase 18864: Apptivity SmartBean Wizard Code Creates SmartBean Instance
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   6/15/1999
SUMMARY:

Apptivity 3.x -- When the SmartBean Wizard is used to generate code that initializes a SmartBean, the generated code creates a new SmartBean instance. If this code may be executed more than once in a session running the application, resources can be saved by creating a new instance of the SmartBean the first time it is executed, and using the existing instance of the SmartBean thereafter.

SOLUTION:

In the Apptivity IDE, once a SmartBean has been inserted into a
project, the SmartBean Wizard can be used to generate code which
initializes a SmartBean. This is done as follows:

1) Place the cursor in the Source Editor at the point where you
wish the SmartBean initialization code to appear.

2) Right-click, and select SmartBean Wizard from the pop-up menu.

3) Select the desired SmartBean from those listed, and accept
the radio group default of "Create a new SmartBean Instance"
then choose the Next button.

4) Enter a return variable for the create method to hold a reference
to the SmartBean, choose the "Generate try/catch blocks" checkbox
if desired (recommended), then choose the Finish button.

The generated code will be as follows, for a Java project named
"Project" using SmartBean "SmartBean":

SmartBeansb.SmartBeanRemote aBean = null;
try {
aBean=
((ProjectEJBHomeTable)theApp().getEJBHomeTable())
.getSmartBeanHome().create();
} catch(java.rmi.RemoteException e) {
//TODO: handle java.rmi.RemoteException
} catch(javax.ejb.CreateException e) {
//TODO: handle javax.ejb.CreateException
}

Each time this code is executed, it creates a new instance of the
SmartBean. If this code may be executed more than once when the
application runs, as would be the case if the code were placed in an
event handler, you can conserve resources by creating the SmartBean
instance only once.

To do this, add an instance variable to the form's class to hold a
reference to the SmartBean:

SmartBeansb.SmartBeanRemote aBean = null;

Then modify the Wizard-generated SmartBean initialization code to be
as follows:

try {
if (aBean == null){
aBean=
((ProjectEJBHomeTable)theApp().getEJBHomeTable())
.getSmartBeanHome().create();
}
} catch(java.rmi.RemoteException e) {
//TODO: handle java.rmi.RemoteException
} catch(javax.ejb.CreateException e) {
//TODO: handle javax.ejb.CreateException
}

This code tests whether the SmartBean has already been instantiated,
and only creates a new instance if it has not already been created.


References To Written Documentation:

Apptivity Developer's Guide - Chapter 9, SmartBeans