Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Hibernate Exception and Session closing

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One last attempt...
 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic