Monday, September 28, 2009

Reasons I don't like EJB

At work, we've long since decided to use EJB3 for our main server system. As the conversion from old code goes along, I also learn some of the problems of EJB. I am more and more of the opinion that EJB3 is yet another over-engineered monstrosity as I've come to expect from Sun. Even with nifty annotations. But the worst part is that using EJB denies one access to a lot of the features of Java.
  • Package-level protection. Much as the package-level method protection scheme is inferior in Java (partly due to the lack of true subpackaging), when you switch to EJBs, you give up even that when you use EJBs. An EJB needs to implement an interface, and all other EJBs must get their dependencies through the interface. So unless you cast your injected bean to the implementation all the time, you cannot use package-level protection.
  • Object-orientation. Beans are singletons implemented with objects, their methods static for all practical purposes. Unless your bean is an entity (and thus subject to all the restrictions of being mapped to a database), you only have the one that's injected.
  • It's viral. Which is not so bad if you start out using it, but if you have a big old system it's difficult to gradually change over. Suddenly you find a cluster of classes that are unrelated to what you're doing, but you have to change them to EJBs because one of them calls a method in the class you actually want to change. And since EJB injection happens at runtime, it's tricky to determine if you've done it right. Even once you have all the EJBs injected correctly, if you miss somebody who makes an instance you'll get very subtle errors down the road.
  • EJB-injected objects are not the real objects, merely proxies implementing the interface (at least in some implementations). This means you can't do anything in the way of reflection beyond what the interface has; in particular, you cannot find annotations on the objects. This is particularly ironic with EJB3, which can be done entirely with annotations.
I'm sure more issues will pop up as the conversion goes along.