Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

closing hibernate Session in BaseRootDAO

 
pranith rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to hibernate and I am using hibernate synchronizer 3.1. The DAO classes generated by hibernate synchronizer closes the session for most of the methods (even if I try to find an object). I am not sure the reason for closing the session. Can anyone please throw light on this?

Thanks,

pranith

[spelling fixed in title - Paul Sturrock]
[ December 08, 2005: Message edited by: Paul Sturrock ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the Session is closed all objects in that Session (in whatever state they have) are persisted to the database. So if you open a Session, get a Person object, update its name property and call a save() method, nothing will actually happen in the database till the Session is closed (or flushed or cleared). This is why the Session is also known as the First Level Cache.
 
pranith rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
When the Session is closed all objects in that Session (in whatever state they have) are persisted to the database. So if you open a Session, get a Person object, update its name property and call a save() method, nothing will actually happen in the database till the Session is closed (or flushed or cleared). This is why the Session is also known as the First Level Cache.


Hi paul,

Thanks for your reply. Generally anyone want to keep the session open until a request. I think by default session is autoflushed.

Thanks,

Pranith
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Generally anyone want to keep the session open until a request

It depends on how you implement it. The recommended basic pattern is "Session per business transaction", so if one business method reads from 5 and updates 2 tables you would open the Session for the first read and close it after the last update. But there are many other valid patterns. Have a read of Hibernate site - there are plenty of useful discussions there.


I think by default session is autoflushed.

No. The default is FlushMode.AUTO, which means the Session is sometimes flushed before a query is made, but you can't rely on it happening.
 
pranith rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
It depends on how you implement it. The recommended basic pattern is "Session per business transaction", so if one business method reads from 5 and updates 2 tables you would open the Session for the first read and close it after the last update. But there are many other valid patterns. Have a read of Hibernate site - there are plenty of useful discussions there.

No. The default is FlushMode.AUTO, which means the Session is sometimes flushed before a query is made, but you can't rely on it happening.


I agree with you. But if I close the session and try to access a lazy collection in view then it will be a problem. I think using session.flush after a query is made might be a good idea.

Thanks,

Pranith
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic