Why should we use EJB? [closed]
Asked Answered
P

5

40

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

Papilloma answered 9/5, 2011 at 12:14 Comment(2)
Sometimes I feel there is a need for stackoverflow to review the guidelines, someone brand new to the technology would like someone to explain in plain English, and what could be better platform than stackoverflow? Question was asked close to 10 years ago and here I am asking the same question.Heracliteanism
here is answer by @Arjan Tijms is the simplest I could think of-- "EJB beans are specifically designed to implement the business logic of your application. As such they provide services that are often needed when implementing such logic, such as transactions, injecting of the entity manager (used for JPA, the Java Persistence API) and pooling of beans."Hebdomad
S
29

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).

Saiff answered 17/7, 2014 at 19:46 Comment(1)
>All ejb can use JAAS for secure the applications - This is not correct. EJBs do NOT use JAAS. Secured beans (via e.g. @RolesAllowed) require the container to authenticate the user/caller but the spec does not describe via which mechanism this process should take place. Container implementations could base their code on JAAS or could use something else entirely. See these for some more details: arjan-tijms.blogspot.com/2014/02/… and raymondkng.sys-con.com/node/171477Muimuir
M
19

EJB beans are specifically designed to implement the business logic of your application. As such they provide services that are often needed when implementing such logic, such as transactions, injecting of the entity manager (used for JPA, the Java Persistence API) and pooling of beans.

See this for a more elaborate answer and even more references: What use are EJBs

In the most basic wording possible; "EJB beans make it a lot easier to work with a database via JPA".

Using JPA outside an EJB requires a lot of verbose and error prone code for obtaining an entity manager, starting a transaction and committing it or rolling it back.

There are a ton of other reasons to use EJB, but from experience I think that is the number one reason.

Muimuir answered 10/5, 2011 at 22:2 Comment(0)
A
7

The Enterprise JavaBeans architecture or EJB for short is an architecture for the development and deployment of component-based robust, highly scalable business applications. These Applications are scalable, transactional, and multi-user secure. You can develop the application once and then deploy on any one of the Java EE 5 compliant application server. There are many application servers available, both free and commercial. You can choose the server for development and deployment to suit your requirement and budget.

Benefits of EJB

EJB simplifies the development of small and large enterprise applications. The EJB container provides system-level services to enterprise beans, the bean developer can just concentrate on developing logic to solve business problems.

see in wiki for more detail to getting stated

Agamogenesis answered 9/5, 2011 at 12:17 Comment(0)
T
1

There is a newer Java EE tutorial here: http://download.oracle.com/javaee/6/tutorial/doc/. Well worth the time and effort.

And while Wikipedia may be the trite place for answers, this article is a pretty good overview of what EJBs are: http://en.wikipedia.org/wiki/Enterprise_JavaBean.

Trio answered 13/5, 2011 at 13:57 Comment(0)
W
-2

Transactions, Remoteness, etc. Maybe buy a good EJB book and read it, or do some googling. Start here Java EE6 EJB . It probably answers all your initial queries.

Wholewheat answered 9/5, 2011 at 12:17 Comment(1)
Mwhoah, yes the Java EE 5 tutorial is some 5 years old, which is in Internet time ancient. On the other hand, it covers EJB3 and this is still very current. EJB 3.1 was more of an evolutionary enhancement and didn't change anything regarding the base architecture.Muimuir

© 2022 - 2024 — McMap. All rights reserved.