-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to properly close the Session/transaction?
PostPosted: Fri Apr 08, 2005 10:49 pm 
Newbie

Joined: Thu Mar 24, 2005 11:29 pm
Posts: 14
Hibernate version: 3.0

I couldn't find this in any docs or on google - what's the 100% proper way to close sessions? Normally, my code looks like:



Code:
AccountPrivilege ap = new AccountPrivilege();
      ap.setName(name);
      ap.setDescription(desc);
      
      Session session = SessionFactory.currentSession();
      Transaction tx = session.beginTransaction();
      session.save(ap);
      tx.commit();
      SessionFactory.closeSession();


Is this good enough? Do I need to add any "on-server-shutdown" code?

The reason I ask is because when I run a page with this, I notice a SIGNIFICANT "lag" - aka my page takes almost .5 seconds to load (whereas without that code, it's instant).

A quick question: Is there any good examples of using "map" collection mapping? The stuff in the "docs" is impossible for new users to read, any help there?

THANKS!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 09, 2005 6:45 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Quote:
Is this good enough?

No. Make use of try-finally clauses to ensure cleanup. This is good practice for any resource that you access in java code. There is a decent example in the hibernate reference documentation at: http://www.hibernate.org/hib_docs/v3/re ... emarcation . You need to look a little harder.

Quote:
Do I need to add any "on-server-shutdown" code?

Well what server are you using? Any decent server e.g JBoss will handle this for you.

Quote:
Is there any good examples of using "map" collection mapping? The stuff in the "docs" is impossible for new users to read, any help there?
Buy the HIA book.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 09, 2005 6:47 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Quote:
Is there any good examples of using "map" collection mapping? The stuff in the "docs" is impossible for new users to read, any help there?

Buy the HIA book. Better still - find out, and contribute an improvement to the docs if you feel they are unreadable.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 09, 2005 4:13 pm 
Newbie

Joined: Thu Mar 24, 2005 11:29 pm
Posts: 14
What do you mean JBoss will handle it for me? I'm using Resin, and for some libraries I had to add some extra code on server shutdown, so it would close all the connections cleanly.

As for map - I don't think I'm ready to buy a book yet. Isn't there any good examples?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 6:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Have a look at http://www.hibernate.org/78.html
and download the caveat emptor app http://www.hibernate.org/159.html

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.