Spring, Hibernate and leaky abstractions
This week, I ran into the following problem: my Spring/Hibernate based application locked up. It was reproducible and seemed to depend on the number of queries that had taken place. So I started Tomcat with
told Eclipse to connect to that socket (debug remote application) and suspended operation after the application frooze. The result was clear: no more connections in the connection pool and an infinite wait until one would be available.
Now this application is reasonably old and has worked succesfully for quite a while. Everything seemed to be in order, until, after looking at the Javadoc of the HibernateDaoSupport class for the umpteenth time, it suddenly hit me: the latest DAO used getSession() instead of getSessionFactory().getCurrentSession() to obtain a session. While the first could very well be a shorthand for the latter, it most certainly isn't. I noticed the difference when I reviewed the differences when updating from Subversion and read the appropriate Javadoc, but I wasn't paying enough attention to realise that the difference between these ways of obtaining a Hibernate session was a nitty-gritty detail that was very important.
It can also lead to: Hibernate Exception: Illegal attempt to associate a collection with two open sessions
All in all I think the HibernateDaoSupport is an unnessary layer of abstraction anyway and puts a hard dependency on spring. These days I prefer having just pojo's and injection a jpa entityManager ... although I also annotate it as @Repository ... kinda defeats the point of removing hard spring dependencies
When our code by all accounts is not causing the problem, but rather our interaction with hibernate sessions and connection pooling, it makes it extremely difficult to debug without doing profiling on our production environment. I'm not very good at profiling Java apps so it introduces a new burden of expertise.
When I choose an ORM, all I want it to do is simplify the creation of SQL queries - If it does anything more it's making my life more difficult. If I have a performance problem I'll introduce a caching layer of my choosing. Every library should just solve one problem well. Between them, Spring and Hibernate seem to be trying to solve the whole Java platform.
Comments are closed