• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Should I close the session?

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this hibernate code correct, or should I close the session in the end of the method, to avoid memory leak?
Thanks!


 
eddy johns
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, do I need to catch exceptions in this code?
Thanks!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you must close the session
 
eddy johns
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Anupama. Can you by any chance show me which code will you use in this example?
Eddy
 
Anupama Sudhakaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try reading about the SessionFactory API
 
author and cow tipper
Posts: 5001
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm not sure if it's a 'memory LEAK' that you'd get, but I know what you mean.

Yeah, in this case you'll want to close the session. I would say, the usually, you'd want to have longer transactions. You've got a very fine grained transaction here, and if there are multiple databse hits, you'd be using ALOT of resources needlessly.

If it's an academic example to figure out how things work, it's good. If it's in an application, you'd probably want the transaction to be alot longer in duration, or at least, demarcated outsilde of the method.

-Cameron McKenzie
 
eddy johns
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Cameron. Do you mean that I'll get a "memory FLOOD?"

I have a manager class that does quite a few of these small units of work. Would you recommend getting a session in the manager's constructor, and using it throughout?

In any case, I think I read somewhere that session.getTransaction().commit() does the work and then closes the session. Is that correct? If so, why do I still need to close it? And how exactly would I do it?
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5001
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yup! You're killing your application by doing the create/begin/commit stuff over and over again. That type of stuff tends to be the biggest performance problem in these apps - just too many Sessions and transactions created when just one would suffice.

There are lots of design patterns and best practices out there to guide you on these types of travels.
 
eddy johns
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I tried that, and created just one session in the constructor. Like this:



All the other methods are using this session. When I tested it, the first transaction went fine, but on the second one I got a session is closed exception. What am I missing?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic