Singling Out Stateless Session Beans in Enterprise JavaBeans
Stateless session beans — EJBs that complete a task in a single step — are arguably the simplest of all EJB components. They make minimal demands on the EJB component developer and are used to implement very simple operations. Nevertheless, you have several important rules regarding stateless session beans that, as an EJB developer, you must remember.
Stateless session beans can be tricky because of the way the EJBHome and EJBObject interfaces are defined. All types of EJB components implement these two interfaces, which must be generic enough to support the methods needed by stateless session beans, stateful session beans, and entity beans. Because stateless session beans are so simple, they don’t need all of the methods that the EJBHome and EJBObject interfaces define. Consequently, some of the methods in the stateless session bean always throw exceptions if they’re invoked.
Take a note — when defining your own classes, it’s a good idea to define interfaces so that they hide methods that are irrelevant to a particular implementation. You accomplish this simply by defining the narrowest interface first, and then extending it to add additional methods for a broader interface, and so on until you have all the required methods defined. Doing so eliminates the messiness that results from having methods implemented in a class that doesn’t need them and can’t use them.
Stateless session bean lifecycle
Stateless session beans are EJB components designed with two simple objectives in mind:
- To perform simple tasks that can be accomplished in a single method invocation.
- To be shared by many clients at the same time. This is the root cause for the session bean being stateless.
The term stateless refers to the fact that the stateless session bean cannot hold any information for an EJB client between method invocations for that client. The reason stateless session beans are stateless is because the beans are held in a shared pool on the EJB container between each method invocation on the bean, a process shown in Figure 1.
When a client invokes a method on a stateless session bean, it is removed from the pool, the method is executed, and the bean is immediately returned to the pool. Consequently, if you executed two concurrent methods on a stateless session bean variable, different beans in the EJB container would probably service them.
Ideal cases for the use of stateless session beans are for simple tasks, such as processing a payment, making a deposit, or making a withdrawal. These tasks all have two common features: They represent actions, and they can be performed in a single step.
While stateless session beans offer a big performance benefit, they’re not the right choice for all tasks. If you need your session bean to remember information supplied by a client across multiple method invocations, then you should use stateful session beans.
When to use stateless session beans
Given the abundance of EJB component choices available to you, it can sometimes be difficult to decide whether the stateless session bean is the right tool for your task. A couple of simple guidelines can assist you in making that decision.
- First, consider if the task your application needs to perform can be accomplished in a single-method invocation. Remember that a stateless session bean cannot remember state specific to a client between method invocations. Thus, if the task at hand involves more than a single step, the stateless session bean isn’t the right choice. But if you can perform the task in a single step, then always use the stateless session bean.
- Second, determine if the process your application is performing needs to be visible to multiple clients at the same time. If the application needs to share information and state between multiple clients, then you have only one choice — an entity bean.
Remember that a stateless session bean provides substantial performance benefits to your application. Generally, if you can design the application to maximize the use of stateless session beans, your application server will be able to respond to client invocations more quickly. See The Basics of Writing Enterprise JavaBean Code.