• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

desktop app & hibernate

 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any reasons why it would be bad to create & open a hibernate session during the init part of a desktop app, attach that session to a 'application' bean and be able to use that throughout my application never closing the session.

I read that using singletons is a bad thing. But I dont want to keep creating/opening & closing a session everytime I want to do something with the DB.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So long as you were careful to guard against stale data you could do this. That being said, opening and closing a session are trivial operations. What perceived benefit are you looking for by keeping the session open?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holding on to a session has these problems:

1. Your first-level cache may gradually grow and grow as persistence objects accumulate. You can always call evict or clear, but at that point, it may make more sense to bite the bullet, identify the proper "application transactions" and open sessions, use them and close them, in the typical Hibernate fashion.

1a. And the longer you hold onto a persistent object, the more likely it will be stale.
Of course, this depends on your data model.

2. This doesn't scale, if may clients are attaching to your database.
[ January 26, 2006: Message edited by: Jeff Albrectson ]
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic