Java Reference
In-Depth Information
Besides stateful and stateless beans, the third type of session bean is a singleton . You can
think of a singleton bean as being like a conductor on a train. The conductor knows the
fares, stops, arrival times, and times of connecting trains. Passengers can ask the conduct-
or questions but generally only one at a time. The conductor can, however, accept tickets
while answering a question. There's generally only one conductor and they are shared by
all of the passengers on the train. This is analogous to a singleton bean. There's only one
instance of a singleton bean in the application.
Regardless of the type of conversation, session beans are optimally designed to be directly
invoked by all the clients of the application's business service API. A client can be just
about anything, such as a web application component (servlet, JSF, and so on), a command-
line application, another EJB application, or even a JavaFX/Swing GUI application. A cli-
ent can even be a Microsoft .NET application using web services or an application written
for iOS or Android. Session beans are incredibly powerful and can be used to build scal-
able back-end systems for all of these clients.
At this point you should be wondering what makes session beans so special. After all, each
of the three types of session beans sounds simple enough that you could code them your-
self without a container, right? Session bean concepts transcend the EJB technology itself.
Chapter 1 mentioned that besides simply modeling the application business API, the con-
tainer provides a number of important services to session beans, such as dependency injec-
tion, lifecycle management, thread safety, transactions, security, and pooling. The next sec-
tion should help to make it clear why you should use session beans and when you should
use them.
3.1.1. When to use session beans
Session beans are much more than a simple abstraction for partitioning business logic. The
container manages session beans and provides them with a number of important services.
These services include injection, transaction management, concurrency control, security,
remoting, scheduling (timer), and interceptors. If used appropriately, session beans form
the foundation of highly scalable, reliable applications. Session beans can be abused—if
everything is a session bean, then performance will suffer due to container overhead. Con-
tainer overhead isn't a bad thing per se, however. If you weren't using the container or
EJBs, you'd still have to implement not only your business logic but also the services being
provided by the container and would probably do a worse job in terms of performance and
Search WWH ::




Custom Search