Sunday, July 09, 2006

The Persistent Persistence Question

On my current project, we faced the inevitable, persistent, annoying question of which persistence framework to use. We boiled it down to 2 choices: nHibernate or iBatis. As usual, it was not a cut-and-dried decision, as each had their strengths. nHibernate, being a meta-data mapper, writes all the annoying SQL for you, which is a huge time saver...when it can. However, when talking to complex legacy schemas, nHibernate gets tougher and tougher to configure. iBatis, on the other hand, doesn't try to generate your SQL. It just takes care of the object-relation mapping for you, from SQL you supply. That makes it much better for complex schemas, stored procedures, etc. So, which to use?

In the end, we chose both! We estimated that maintaining separate configurations would take a little time, but it would save us time on both sides: letting nHibernate do its magic when it can, and falling back to iBatis when it made more sense. It has worked out very well. We have a couple of very complex queries being handled gracefully by iBatis, and nHibernate handles all the simple persistence in the application. Sometimes, seemingly mutually exclusive options actually complement one another.


agilechilli said...

How do you deal with issues such as data integrity, transactions, and optimistic locking when you've got 2 separate mechanisms each with it's own "unit of work"? Unless you're using them to deal with very distinct and separate sets of data?

Neal Ford said...

Up until now, all our updates have been through nHibernate. We did however spike out sharing transaction contexts between them, by supplying the transaction context as we create the sessions for each. Generally, though, for the really complex queries where iBatis shines, we're just reading stuff, not updating it.