Hibernate Conclusions

Monday, October 1, 2007

A while ago, I posted a couple of blog entries about the fact that I was going through the learning curve with Hibernate. Since then I have talked to some friends who have used Hibernate on large projects and I have also been prototyping an application using both Hibernate and Spring JDBC to understand the pros and cons of each approach.

My initial reason for learning Hibernate was to make sure I knew enough in an interview situation if it came up. I also needed to recommend a persistence technology for a couple of projects that I am involved in and having heard so much about Hibernate I assumed that it would be a good fit.

The biggest advantage that I can see with Hibernate is that it allows for rapid prototyping and development of new applications. That alone might be sufficient reason to use it on some projects. However, once you get past the simple initial requirements of persisting objects and get to the point where you need to run complex queries across a number of tables and perhaps use aggregate functions and other SQL capabilities, things get much more complex because Hibernate uses its own HQL query language that the runtime translates into SQL and this forces the development team through yet another learning curve. Another issue with this approach is that you can’t simply develop and test SQL and then paste the working SQL into your source code or configuration files but have to then re-code them in HQL.

Spring JDBC isn’t directly comparable to Hibernate because it doesn’t have the same goals (it doesn’t provide caching or object relational mapping for instance). However, it is certainly an alternative choice for implementing persistence. Spring JDBC provides a very small abstraction layer over raw JDBC and does this in an elegant way. Setting up a new project does require more thought and effort than using Hibernate but that time is paid back once more complex application functionality is developed.

However, the real surprise for me was how the performance and scalability compared between the two approaches. I spent a couple of days building a realistic subset of the application I was recommending the technology for and then ran benchmarks against a large database on dedicated test hardware. The results showed that Hibernate didn’t scale at all well compared to Spring JDBC for this application.

I posted some questions to the Hibernate forum to validate the approach I had taken and I received the rather terse and unhelpful reply “This is called 'A useless microbenchmark' and has been addressed a thousand times.” from a member of the Hibernate team. I did some reading of the Hibernate Performance FAQ and found some interesting statements in there, such as "We claim that Hibernate performs well" and "Many people try to benchmark Hibernate. All public benchmarks we have seen so far had (and most still have) serious flaws.". It almost seems like it is taboo to question Hibernate performance. I don't understand this defensive stance since most developers would recognize that there is a trade off between ease of use and performance.

The bottom line for me is that for my application, Spring JDBC scales much, much better, which will save significant hardware dollars (or pounds). Spring JDBC also doesn't require much investment in terms of learning curves and I can have full control over the SQL I use which in turn will help me squeeze maximum throughput out of the hardware that is available to me.

If you're considering using Hibernate on your projects I highly recommend running some performance tests based on your application requirements before getting in too deep.

[14th Oct 2007] Wow! This article has certainly attracted more attention than I expected. I would like to draw attention to that fact that I state in the above article that Hibernate does not scale well for *this* *particular* *application*. Many people seem to think I'm saying that Hibernate doesn't scale well, period. That's not what I'm saying so please read again before posting comments :-)

Labels: , , ,


Waking up to Hibernate, Part 2

Wednesday, August 15, 2007

I've been busy building out a fairly realistic test application for the past couple of days so I can really get a handle on Hibernate's performance and scalability. The application is a book store e-commerce site very loosely based on Amazon. I'll make the code available once it's complete.

I've defined a stateless session bean interface with the business methods e.g. login, findBooks, createOrder, addBookToOrder, and so on. I currently have two implementations of the bean - one that uses JDBC DAOs and one that uses Hibernate. I'm considering implementing another version that uses Spring JDBC as well.

I've developed a tool that can populate the database with configurable amounts of random data so that the tests are representative of real-world data volumes.

I then have one standard multi-threaded test client that will work with any of these bean implementations. I'm hosting the beans in JBoss on a dedicated server so that each implementation will be using the same connection pool and transacation management to try and make the tests as fair as possible.

The initial results are showing poor scalability with Hibernate and I have a thread open on the Hibernate forum where I'm looking for guidance on my configuration and approach in case I'm not using Hibernate correctly. At this stage it's also possible I have bugs in my code so I'm not going to post any figures until I'm confident that my tests are correct.

I'm starting a new contract tomorrow so I'll have to put this work on hold for a couple of days but hopefully I'll be able to post some stats at the weekend.

Labels: ,


Waking up to Hibernate

Monday, August 13, 2007

Those of you who know me will be well aware of my bias towards the DAO approach to writing Java persistence code but I'm currently spending time getting to grips with Hibernate. As a freelance developer, Hibernate is now an essential skill to list on my resume due to it's widespread use. It's easy to see why Hibernate is so popular - it allows database driven applications to be developed using natural idiomatic Java code and doesn't require volumes of tedious code to be written to a low level API like JDBC.

My only real concern with Hibernate is the overhead it adds to an application compared to using JDBC directly but I don't have any empirical evidence to back this up. Also, Hibernate could potentially have better performance than JDBC because, as a runtime framework, there are opportunities for caching data. Ultimately, as with most technology comparisons, I expect that both Hibernate and plain JDBC code have particular use cases that they are best suited to.

I'm currently putting together a test application to try and understand the performance characteristics of Hibernate compared to low level JDBC code. I'm also using this as a means to learn Hibernate.

I'll post the results here as I get them.

Labels: ,