J2EE applications use too many EJBs. The correct1 number of EJBs is one per component. My reasoning is below, but first a quick review of the technical pros and cons of using EJBs.
Pros:
Cons:
Each of the advantages gained from EJBs, is gained, in full, with the first EJB you use. Additional EJBs don’t give additional advantage.
On the other hand, (most of) the disadvantages of using EJBs accrue with each and every EJB. Each additional EJB gives a bigger helping of disadvantages.
From here it’s a very small step to the conclusion that you should use as few EJBs as possible, preferably zero. However, if you find the advantages of using an EJB container compelling, then I recommend a maximum of one stateless session bean as the interface to each component.
1 If you know of an application that would benefit from more EJBs than this, please let me know. I’ve yet to encounter one.
2 Though I’ve never seen an EJB application gain any performance or reliabilty by distributing itself over multiple machines. IMHO, most applications don’t follow the rules strictly enough to allow transparent distribution.
3 Except that they are the only mechanism available to get hold of the SessionSynchronization interface, and thus the only way for EJB 1.1 objects in a container to “listen” for whether a transaction is successful or is rolled back.
Updated: This new entry extends and clarifies my argument.