Over a million developers have joined DZone.

Why I Like EJB 3.0 And Really Like EJB 3.1

DZone 's Guide to

Why I Like EJB 3.0 And Really Like EJB 3.1

· Java Zone ·
Free Resource

Marc Fleury wrote a readworthy Blue Paper "Why I Love EJBs" in 2002 (download), which for that time was really surprising. I was already working with EJBs at the time, but didn't really love them much :-). I wouldn't go quite as far as saying "love" now either, but I really like EJB 3.0. The reasons are:

  1. The performance overhead is low. In Glassfish v2 only about 3%.
  2. EJB 3.0 are thread save. This is a huge benefit. Every thread gets an own instance - also all injected resources (Entity Manager, Data Source, JMS) are thread save as well. The best of all: the container manages the resources for you. Only an annotation is required (@EJB for EJBs, @PersistenceContext for Persistence and @Resource for remaining resources :-)). Just try it with "plain old Web Container" :-). Hint: Servlets are thread-unsafe singletons.
  3. EJB 3.0 are transactional. So in case the method invocation is successfull, all resources will be synchronized (data written to DB, messages sent to server). The container does it for you - you have only know what you would like to achieve (this is platform independent, but sometimes hard enough :-))
  4. In all of my projects we get rid of deployment descriptors and used annotations. Only a lean persistence.xml was deployed. Refactoring, deployment and portability just worked well.
  5. EJB 3.0 are really portable (and so vendor, framework neutral). There is lot more specified, than EJB 2.1 days - so proprietary deployment descriptors aren't needed any more. Actually no descriptors are needed. You only have to deploy one clean jar, with persistence.xml in meta-inf. Everything else is optional (and somehow suspicious).
  6. The synergy between Convention Over Configuration (or officially: Configuration By Exception) and Dependency Injection is great. Actually in most cases you get with EJBs less code, than without (no "new" invocations, just declarations).
    public class SampleBean implements Sample {
    private EntityManager em;

    private Another anotherBean;
  7. Getters And Setters are optional: for EJBs, as well as for JPA. It's your decision. Resources can be directly injected into the fields.
  8. The defaults and annotation configuration can overruled by XML-descriptors. The XML descriptors do not have to be complete - only interesting portions can be specified. You can easily override the production settings for e.g. staging (test, integration etc).
  9. Actually there is no special tooling required. You only need the annotations, a Java 6 compiler and a Jar. However the support in IntelliJ, Eclipse (with 500 additional plugis :-)), or Netbeans 6.1 are really superb. The applications servers are already well integrated - deployment, undeployment, configuration etc. can be managed directly from the IDE.
  10. EJB 3.1 will be embeddable out-of-the box. However Glassfish v3 is already emeddable. I'm testing now the WebContainer - it's surprising (starts in 500ms). JBoss is embeddable as well.
  11. EJB 3.1 will come with a lot of useful features: Singletons (good for configuration, startup classes etc.), better timer support (cron-like), async methods with Futures, optional local-interfaces and WAR-deployment.
  12. Pool settings and thread pool configuration is really useful to control the scalability (and not scale indefinitely until the container crashed :-))
  13. They play well with scripting (JavaScript, Groovy etc.)
  14. EJBs are managable and monitorable by default. You just deploy them into container - and you can monitor the method invocations, performance etc.
  15. No additional frameworks, libraries etc. are needed first. The ejb-jar and the container only contains your application code. So NoClassDefFoundErrors, ClassCastExceptions etc. can be minimized.
  16. EJB 3.0 are ultra-lean. You actually cannot take something away :-). However: I'm open for suggestions.
  17. They are really easy to test - it's just class with an interface. You can even start them outside the container.
  18. Even simple use cases can be efficiently be realized with EJB 3. See e.g. CRUD:
    public class CrudServiceBean implements CrudService<Integer,Customer> {

    private EntityManager em;

    public Customer create(Customer t) {
    return t;

    public void delete(Customer t) {
    t = this.em.merge(t);

    public Customer find(Integer id) {
    return this.em.find(Customer.class, id);

    public Customer update(Customer t) {
    return this.em.merge(t);
  19. ...and they really work well: I'm using EJB 3.0 + JPA 1.0 since about 2 years in projects. It worked not only surprisingly well for me - the team members were surprised as well.

However there is still room for improvement in the Java EE 6 Platform. JMS spec could be easily redesigned to be more "fluent". The JNDI registry is archaic. Fixing/simplifying the JNDI-API would be really beneficial not only to EJBs, but for the whole platform as well...

From http://blog.adam-bien.com/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}