For those in the J2EE world, one of the hottest, most contentious topics that has arisen is what went wrong with EJB, version 2. For those not familiar with the subject, I’ll try to provide a brief description. In 2001, Sun released a specification for connecting a Java object, called an Enterprise Java Bean, hereafter referred to as an EJB, to a database table through an construction called an Entity Bean. They referred to the technique as Container Manager Persistence, or CMP for short. Through the clever use of XML files, one could define Entity Bean relationships between Java code and a database, that could change on the fly without having to recompile the code or rewrite the application.
Initially, Entity Beans were well received as ‘the wave of the future’, but the problem lies in the fact that this specification was mostly worked out only in theory. The specification was written with such detailed requirements for the developer, that anyone who tried to implement Entity Beans in EJB2, had immediate code maintenance problems. There were just too many rules and maintaining large code bases was extremely difficult. In 2006, Sun released the Java Persistence API, JPA for short, in version 3 of EJB specification that, for all intents and purposes was a complete rewrite of EJB2 Entity Beans. They streamlined a lot of the interactions that were required to set up EJBs and borrowed a lot from more grass roots technologies like Hibernate and JDO. In essence, they threw out EJB2, copied Hibernate, then renamed the whole thing as ‘the next version of EJB’. Ironically, they might have been a little too late as many organizations had all ready switched to Hibernate by the time the implementations of the new specification were released.
These days most developers prefer Hibernate to EJB, although given the pervasive nature of Sun’s EJB/J2EE terminology I wouldn’t be surprised if JPA/EJB3 (the name at least) makes a comeback. As I look back on these recent events it makes me wonder, if EJB2 was such a good idea in theory, why did it fail so miserably in practice? What was it about Entity Beans people failed to consider? I hope to use this post to address some of this issues, albeit in hindsight, that the original planners of EJB2 did not consider.
- Issue #1: Not all Servers are Created Equal
One of the most prominent features of Entity Beans in the EJB2 specification was their ability to work with any database system. A developer could, again in theory, write an application could seamlessly deploy on a variety of database systems (MySQL, Oracle, SQL Server, etc.) and applications servers (JBoss, WebLogic, WebSphere, etc.). The mechanism behind this fluid deploy was that all relationships between the database and application were stored in a single XML file. One just needed to open this XML file in a text editor, rename a few fields, and the application would work on that system. Need to deploy on a variety of systems? Just create a new XML file to map the application to that system.
Sounds wonderful in theory, but the practically speaking? This never worked, the servers were just too different. For example, while all databases have standardized on common language called SQL for 90% of the database communication, it would be a near miracle for you to find a code base that did not use any database-specific features. Most large enough systems rely on stored procedures, indexes, foreign key relationships, and key generation that while may be similar from database to database, doesn’t mean you can just drag and drop code from one system to another. Porting code from one database system to another often requires editing thousands of files line-by-line looking for changes and is a quite a difficult, often impossible task.
And that’s just database servers, application servers require their own server-tailored XML to perform the data mappings for each and every field in the relationship. Most databases have an average of a hundred or so tables with a couple dozen fields per table, so you’d have maintain a server-specific XML file for thousands of fields for each application server you wanted to deploy to. When a developer says porting an EJB2 CMP application to a new application server is hard, this is a large part of what he’s referring to.
Part of the problem with the variation among application servers was Sun’s own fault. While they defined the vast majority of the spec themselves, they, for whatever reason, allowed each application server software to define its own extensions for such features as database-specific fields. This wiggle-room allowed application server developers to create schemas as different as night and day.
- Issue #2: Maintaining large XML files in J2EE is a contradiction
Going back to a single-server application, let’s say you do implement an EJB2 CMP-based system. At the very least you have an Entity system composed of a hundred java files, a single XML file (ejb-jar.xml) that defines the entity beans, and a single application-specific server XML file (jboss-jdbc.xml, open-ejb.xml, etc) that maps the entity beans to the database. What happens if you make a change to the java code or the database? Those files must be updated, of course! That means a developer needs to have exclusive access to edit these 2, quite large, XML files. In large enough groups, developers will be fighting to make changes to these files and contention for this file will be high in the version control system.
The history of J2EE is that it was designed to be a component oriented large business platform. Have a project with only a single developer? No reason to use J2EE since it can be done a lot faster by one developer. On the other hand, have a large project that requires dozens, perhaps hundreds, of developers working together? That’s what you ‘need’ J2EE for. But now add the fact that hundreds of developers are going to be writing and rewriting 2 very large XML files and you come to the realization that the technique is fundamentally flawed. You need J2EE for large development teams but maintaining a lot of single-point large files tends to break down faster in large development groups.
- Issue #3: Databases change far less frequently than software
We’ve previously discussed how EJB2 defined all of those XML mappings in a way to allow you to change the database and application server without ever having to recompile code. The idea (I guess) being that database changes and application server changes were far more likely than code changes. Well, in practice that idea is completely wrong. The vast majority of J2EE applications work on one specific database type and one specific application server (often a specific version of the software). Furthermore, database changes are extremely rare. Often, once the database structure is defined, it is rarely, if ever, changed. Java code on the other hand is often rebuilt on a nightly basis. You would never get into the situation in practice where a change to an existing database system was needed before the code was ready to support it. Add to that the fact that most developers go out of there way to write solutions to software problems that do not require changes to the database! Most are fully away of the problems associated with changing the database (modify all related java and XML files, notify related teams, write database patches, etc), and will only change the database as a last resort.
In short, EJB2 defined a powerful spec for an ever changing persistence database layer, and no one bothers to use it in practice because of the maintenance issues involved. I’m sure I’m not the only developer that has seen poorly named database tables and fields like ‘u_Address_Line3’ (instead of ‘City’) but refrained from changing them knowing the havoc such changes would likely bring. Since the user is never supposed to view the database directly, why should it matter what things are named?
EJB2 Post Mortem and Lessons Learned
Not all of these issues were unaddressed during the life of EJB2. For example, XDoclet is a tool that provided a way for generating the large XML files for each system after a code change was made. The problem? It was never developed to support all systems and still had developers fighting to check in use their version of generated file to the version control system. Ultimately though, after thoughts like XDoclet were too little too late; people had seen how much more useful the implementation of object relational mapping tools such as Hibernate had become, and jumped shipped early on. When JPA finally did come out in EJB3 the audience was far from excited: the most dedicated of the crowd who had adopted EJB2 early on were left with no way to upgrade their systems other than to do a near-complete rewrite of their CMP system, and those in the crowd who would have probably most enjoyed the new features of JPA had all ready switched to Hibernate, with no reason to switch back.
When JPA/EJB3 was released, it virtually did away with all of the XML files, instead choosing to put the database mapping information directly in the java code file in the form of an annotation tag. In this manner, a developer only needed to look to one place to make changes to the entity bean and database. In the end, EJB2 entity beans is yet another example of why systems designed in theory should be tested more in practice before being trumpeted as the ‘the wave of the future’. It’s not that JPA is more superior to EJB2, in fact far from it. As I’ve mentioned EJB2 had amazing data control and could was capable of many powerful features. The problem lies in the inherent contradiction of creating J2EE as a component-oriented platform while enforcing centralized interactions and overly complicated management schemes. In the end, it was hard to find one developer, let alone a team of 30, capable of managing the EJB2 code base properly.
“Since the user is never supposed to view the database directly, why should it matter what things are named?”
While users usually don’t care about that, it still is important to use proper names for tables and columns in the same way it is important to use proper names for your classes, methods, etc.
Why? Because it makes it easier to comprehend the system , to maintain it. And when you directly use SQL/JDBC in your code or use annotations (XDoclet or Java 5) to map your classes and their fields to their corresponding tables and columns respectively, using proper names makes your code more readable to.
I don’t disagree that database naming should be readable, but its rare to find a database with perfect naming for every table and field. I’m not advocating databases use gibberish naming, but whether or not u_Address_Line3 is renamed to City doesn’t matter to the user. On the other hand it does matter to the programmer, and I could spend another few pages discussing appropriate naming for database systems, although I don’t think it would be very interesting.
You have written a very lucid explanation. Thank you.
I have ventured only into the shallows of EJB, but I could see the deep water. This murkiness is not a unique situation. I have seen theory raise its ugly head and squash practicality several times. I saw it happen in my specialty are, languages, as far back as PL/I. It repeated itself with Ada, and others. And, I fear, it’s beginning to happen in the later versions of Java. But that’s a different subject.
Your explanation is a delight to read. Perhaps enough of your type of thinking will cause a return to reality and practicality.
While I agree with the basic gist of EJB being a good idea that wasn’t implemented well, I disagree with some of the points made.
E.g., XDoclet was around long before Hibernate et al. ever existed, and it took a lot of the pain of maintaining the various source and config files out of EJB. But either way, I wouldn’t say that having one or two large config files is an unworkable situation; it presents some organizational challenges, and isn’t pretty to begin with, but just like 100KB Struts config files, people have learned to live with it. For sure, annotations are nicer, but EJBs were cooked up during the Java 1.2 lifetime (originally in 1998, I think).
As to the database changing w/o the app code changing, oh yes, it happens. I’ve seen weekly schema changes is huge databases, when only a single one out of the many apps running on the DB was changing. That’s were abstraction layers really help. Generally, the code and config files needed for those can be created (semi-)automatically, in which case it doesn’t matter much how large they are.
In my view, the main problem had more to do with getting EJB apps to scale well without throwing a lof of hardware at it (add comment about Sun being a hardware vendor here), and that EJB servers were too expensive to be used lightly by small companies. It was far cheaper to run a servlet container using JDBC plus some homegrown ORM solution, and implement other app server functionalities as needed.
Thanks Ulf, XDoclet did solve a lot of these problems except that it was never part of the spec, but rather an optional way to do things. Had it been part of the spec early on, it might have taken the place of annotations used in the EJB3 spec.
Oh and I’m not saying database changes never happen but you have to admit, most of the time the schema changes the code has to be changed anyway. It’s rare to change a database schema and XML mapping file for entities, without changing a line of code. The only example that even comes to mind is when you rename a column.
It takes the average human 16.8 seconds to find the face in the EJB2 failure picture above………how about you?
Pingback: Review of Flex/Flash Builder 4 – Defective By Design? | Down Home Country Coding With Scott Selikoff and Jeanne Boyarsky