I’m trying to figure out a good design for a Spring/Hibernate app. When creating such an app, it appears like there are a handful of major decisions.
The first major decision seems to be where to put the session/transaction boundary. It seems like I have 3 major choices: as a filter before controllers are even invoked, immediately below the controllers at the service call level, and stuffed way below the business level in repository calls.
It seems to me like the right call is the middle path, but I’m not sure. I don’t want my transactions open too long, but at the same time, I don’t want to constantly worry about detached objects and lazy loading in the business logic. Still, there are some downsides. For instance, it makes it hard for the business logic to make a remote call without holding up a transaction for a few seconds. I wonder if there’s a better way?
Solution 1 : open session in view filter
Solution 2 : transaction boundaries at business layer
Solution 3 : transaction boundaries at dao layer
Unless your dao methods contains business logic, there is no point in having transaction limited to a dao call. That’s way to close to ‘autocommit’ mode to be useful i think.
In any case, if you wish to keep your transaction short, i advise you to watch closely the ‘sql footprint’ of every business use case (by setting the org.hibernate.SQL log category to DEBUG) and compare the produced sql with what you would have written yourself.
Most of the time i’ve seen slow use case, it was because hibernate lazy loading feature was not configured correctly (it was either too eager, adding twelve levels of join in each query, or too lazy, issuing a query by element of collections)