tags:

views:

244

answers:

6

I know this is a subjective question, but why does Hibernate seem to be designed for short lived sessions? Generally in my apps I create DAOs to abstract my data layer, but since I can't predict how the entity objects are going to be used some of its collections are lazy loaded, or I should say fail to load once the session is closed.

Why did they not design it so that it would automatically re-open the session, or have sessions always stay open?

+1  A: 

You're looking for the OpenSessionInView pattern, which is essentially a conceptual filter (and sometimes implemented as a servlet filter) that detects when a session needs to be transparently reopened. Several frameworks implement this so it handles it automagically.

David M. Karr
+4  A: 

Becuase once you move out of your transaction boundary you can't hit the database again without starting a new transaction. Having long running transactions 'just in case' is a bad thing (tm).

I guess you want to lazy load object from your view - take a look here for some options. I prefer to define exactly how much of the object map is going to be returned by my session facade methods. I find this makes it easier to unit test and to performance test my business tier.

RichH
+1  A: 

I'm writing a desktop application so using a filter isn't applicable.

Allain Lalonde
Don't lazy load your objects or write your own caching code to lazy load them in your data model maybe.
RichH
A: 

Connections are a scarce resource that need to be recycled as soon as you are done using them. If you are also using connection pooling, getting another one when you need it should be quick. This is the architecture that you have to use to make websites scale -- even though you are a desktop app, their use-cases probably concentrate on scalable sites.

If you look at MS ADO.NET, you will see a similar focus on keeping connections open for a short time -- they have a whole offline model for updating data disconnected and then applying to a database when you are ready.

Lou Franco
+2  A: 

I worked on a desktop app that used EJB and Hibernate. We had to set lazy=false everywhere, because when the objects get serialized, they lose their ability to be fetched from the backend. That's just how it goes, unfortunately.

If you are concerned with performance, you could use caching on the backend so that your non-lazy fetches are not as painful.

davetron5000
You shouldn't have set lazy equal to false. Overriding the initialization scheme by join fetching in the query is the recommended way to do it.
bpapa
A: 

Hibernate is designed as a way to map Objects to Relational Database tables. It accomplishes that job very well. But, it can't please everybody all of the time. I think there is some complexity in learning how initialization works but once you get the hang of it it makes sense. I don't know if it was necessarily "designed" to specifically to anger you, it's just the way it happened.

If it was going to magically reopen sessions in non-webapps I think the complexity of learning the framework would far outweight the benefits.

bpapa
I never said I was angry by the way. Just noting what I saw as a design flaw.
Allain Lalonde