File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate Session Best Practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Session Best Practice" Watch "Hibernate Session Best Practice" New topic

Hibernate Session Best Practice

Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
Hi guys. For a web application how many hibernate session should be opened for a one web user session? So is it like?

1 User Session = 1 Hibernate Session

How would I know then on when to close a hibernate session if a user just abandoned the application without properly logging out to invalidate all sessions?

SCJP with 98% by...
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
It depends on what database resources the user will access and how they are mapped.
In the simple case, with one database, one Session is what you need, and how to open and close it is decided in the web or business tier of the application.
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
is the httpsession automatically attached to a hibernate session? So if I invalidate a httpsession, so does the hibernate session?
Nathan Hook
Ranch Hand

Joined: Jan 10, 2005
Posts: 81
To answer your question...

No once the http session is invalidated it will NOT close your hibernate session.

Also, last time I checked the Hibernate team was suggesting using one hibernate session per request. (Not per http session.)

I believe it is called the one hibernate session per request pattern. You should be able to find information about it (the pattern) on hibernates website.

Best of Luck.
I agree. Here's the link:
subject: Hibernate Session Best Practice
It's not a secret anymore!