• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design question for creating a DAO using Hibernate

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I m creating a DAO using Hibernate.
is it a good idea to create SessionFactory, Session and Transaction Objects as instance fields and initialize them in the Constructor?
should i close the session on every method that interact with the DB?
if not can you suggest a better design?

Thanks
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, keep the SessionFactory in a singleton, then get the session using sessionFactory.getCurrentSession() to get the current session for the thread that you're in.

Read the first chapter of the Hibernate documentation.

The transaction boundaries depend on how long your transactions are. Typically you manage one per request. In a Web-based app, you might start and end the transaction in a ServletFilter.
 
Sammy Bill
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javid Jamae wrote:No, keep the SessionFactory in a singleton, then get the session using sessionFactory.getCurrentSession() to get the current session for the thread that you're in.

Read the first chapter of the Hibernate documentation.

The transaction boundaries depend on how long your transactions are. Typically you manage one per request. In a Web-based app, you might start and end the transaction in a ServletFilter.



Thanks for your Help.
So you mean, get a new session instance from the sessionFactory and a new transaction from the session in every method and i dont need to close them untill i actually use the DAO in a servlet or so??

bsically thats what i have:


Please correct me if i m doing anything wrong.

Thanks again.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should take a look at the Open Session In View pattern: http://www.hibernate.org/43.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic