Why should we use EJB?

mriganka3 picture mriganka3 · May 9, 2011 · Viewed 49.8k times · Source

What is EJB, and why we should use it? Please explain in simple language. Thank you.

Answer

Ernesto Campohermoso picture Ernesto Campohermoso · Jul 17, 2014

The EJB or Enterprise Java Beans are plain java classes (since version 3.0) with annotations that enable to you write the business logic of your applications and later deploy it (or install) on a Java Enterprise Edition Server.

You must consider use EJB if you wish to take advantage of the following services provided by the Java Enterprise Edition (Java EE) server:

  • Clustering. You can deploy your EJB on a cluster environment (dependent of Java EE Application Server), this provides to you Fault Tolerance and High Availability.
  • Concurrency without use Threads. All EJBs are instantiated through a pool of objects then your application gains on performance and without Thread complexity.
  • Transactionality through JTA. All EJBs can benefit from Transactionality management for different resources, the most important Databases, using annotations is easy to delimit the frontier of every transaction and manage them.
  • Connection Pool to Database. All ejb can access to connection pools defined into the Java EE Application Server, this connection pools provide an abstraction of the database complexity, by example you can use a XA Datasource that enables to you do Two Phased Commit to different databases.
  • Security. All ejb can use JAAS for secure the applications. JAAS is configured into the Java EE Application Server and lets you to Authenticate and Authorize the methods of your EJB through different providers just with configuration (By example using Active Directory, LDAP or Database).
  • Schedule service. All ejb can use the Timer Service that enables to you implement task for further execution or inclusive for repetitive execution.

There is other services and benefits but I think that these are the most importants. If you don't need these benefits my recommendation is that you don't use EJB (not all applications are Enterprise Applications).