Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Exception and Session closing

 
Zeki Karaca
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi @ all

I am learning Hibernate and there are 2 things that are still confusing for me. Take a look at my example code:



Question 1.
I read a lot of tutorials on the internet and also about the differences between getCurrentSession() and openSession(). I know that if i use openSession i need to manage the sessions myself and should also flush/close it. I used getCurrentSession in my example so i did not add a finally clause to close the session used. Is this correct or should i still close the session myself?

Question 2.
Almost all example codes on the internet show a similar structure with a try/catch clause. But getCurrentSession also throws a HibernateException so why should i not put this in the try clause and catch an exception. I know it is a runtime exception but beginTransaction, save and commit also throw the same exception. Why do i catch these and not the one for getCurrentSession?

Please could someone help me to clarify these 2 questions? Thanks in advance.
 
Zeki Karaca
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, is there anyone who can help me with these questions?
 
Zeki Karaca
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last attempt...
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code that i get:


with currentSession(), you dont need to close it manually. All of that is automatically taken care for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic