Before you decide what parts of your application you can represent as session beans, you should know a few more things about session beans. A couple of these things are related to the EJB specification for session beans, and a couple are specific to NAS and its support for session beans.
Creating Stateless or Stateful Beans
The EJB specification describes two state management modes for session beans:
The NAS container supports both state management modes, so you may choose the state most appropriate for your session beans. Use stateless session beans as much as possible for performance reasons. In general, stateless session beans also scale more efficiently in a highly distributed environment. All session beans must be serializable, Because stateful session beans contain much state information that must be both serializable and capable of being recreated, they tend to perform less efficiently and scale less well in a highly distributed environment.
If you decide to use stateful session beans, plan to co-locate stateful beans with their clients. Also, use sticky load balancing to reduce the number of remote procedure calls, especially for session beans that are passivated and activated frequently or for session beans that use many resources, such as database connections and handles.
Accessing NAS Functionality
You can develop session beans that adhere strictly to the EJB Specification, you can develop session beans that take advantage both of the specification and additional, value-added NAS features, and you can develop session beans that adhere to the specification in non-NAS environments, but that take advantage of NAS features if they are available. Make the choice that is best for your intended deployment scenario.
NAS offers several features through the NAS container and NAS APIs that enable your applications to take programmatic advantage of specific features of the NAS environment. You can embed API calls in your session beans if you plan on using those beans only in the NAS environment.
For example, you can trigger a named application event from an EJB using the IAppEventMgr interface, using the following steps and example:
First obtain an instance of com.kivasoft.IContext by casting javax.ejb.SessionContext or javax.ejb.EntityContext to IServerContext.
Next, use the GetAppEventMgr() method in the GXContext class to create an IAppEventMgr object.
Finally, trigger the application event with triggerEvent(). javax.ejb.SessionContext m_ctx;
....
com.netscape.server.IServerContext sc;
sc = (com.netscape.server.IServerContext) m_ctx;
com.kivasoft.IContext kivaContext = sc.getContext();
IAppEventMgr mgr = com.kivasoft.dlm.GXContext.GetAppEventMgr(ic);
mgr.triggerEvent("eventName");
Serializing Handles and References
The EJB Specification indicates that to guarantee serializable bean references, you should use handles rather than direct references to EJBs.
NAS direct references are also serializable. You may wish to take advantage of this extension, but you should be aware that not all vendors support it.
Managing Transactions
Many session beans interact with databases. You can control transactions in beans using settings in the bean's property file. This permits you to specify transaction management at bean design time, deployment time, and run time. By having a bean handle transaction management you are freed from having explicitly to start, roll back, or commit transactions in the bean's database access methods.
By moving transaction management to the bean level, you gain the ability to place all of a bean's activitieseven those not directly tied to database accessunder the same transaction control as your database calls. This guarantees that all parts of your application controlled by a session bean run as part of the same transaction, and either everything the bean undertakes is committed, or is rolled back in the case of failure. In effect, bean-managed transactional state permits you to synchronize your application without having to code any synchronization routines.
Committing a Transaction
When a session bean signals that it is time to commit a transaction, the actual commit process is handled by the bean's container. Besides affecting the data your application processes, commit time also affects the state of a session bean. The NAS container implements commit option C as described in the EJB specification.
When a commit occurs, it signals to the container that the session bean has completed its useful work, and should synchronize its state with the underlying data store. The container permits the transaction to complete, and then frees the bean. Result sets associated with a committed transaction are no longer valid. Subsequent requests for the same bean cause the container to issue a load to synchronize state with the underlying data store.
Note that transactions begun in the container are implicitly committed. Also, any participant can roll back a transaction. For more information about transactions, see Handling Transactions with EJBs.
Accessing Databases
Many session beans access and even update data. Because of the transient nature of session beans, however, be careful about how that access takes place. In general, use the JDBC API to make your calls, and always use the transaction and security management methods described in Handling Transactions with EJBs to manage transaction isolation level and transaction requirements at the bean level.
For more information see Using JDBC for Database Access.
|