-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate under tomcat
PostPosted: Sun Nov 02, 2008 10:40 am 
Newbie

Joined: Sun Nov 02, 2008 10:27 am
Posts: 3
Hello All,

I have a very basic question on Tomcat and Hibernate. I took a deep dive into the many tutorials and I'm confused... I don't really understand
if I need to close the sessions or not....

Assuming that I have the following session factory util class :
public class HibernateUtil {

private static final SessionFactory sessionFactory;

public static final String HIBERNATE_PROPERTIES_FILE = "hibernate.cfg.xml";
static {
try {

sessionFactory = new Configuration().configure(new File(file)).buildSessionFactory();;
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}


Every thread uses the getSessionFactory and runs the openSession() method.

1. Do I need to use session.close() after committing the transactions ?
2. Do I need to use one session per thread, meaning that every thread needs to close the session before the thread dies ? notice that I
use hibernate.current_session_context_class = thread.
3. Is it more reasonable to use sessionFactory.getCurrentSession() ?

I'm sure that this is very basic, but there are too many examples and I still didn't solve this one....

Thanks for any help,
Elad.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 02, 2008 4:33 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
So you are using a web server (as opposed to a full "application server"). The fact that it be Tomcat is irrelevant.

You would need to create some sort data access utility that wraps every single one of your data access operations.
That utility should expose methods, say, create(), update(), delete() and executeQuery(), and all DAOs from your application shouls inherit from it.

Inside each method call, you would get a session (yes, using the HibernateUtil with ThreadLocal), then create a transaction, begin it, do wjat you have to do, then close the transaction, and yes, then close the session.

This approach is somewhat wasteful, because it creates a transaction every time.
A different approach would be to use some sort of framework able to take advantage on "contextual sessions" and the pattern known as "OSIV", which takes care of finding every time a right session from the environment.
These are also called "managed environments", and the most popular is Spring.

For a minimal example of setting up such environment, you can go here

http://hibernar.org/articulos_en/spring ... xample.php

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 8:54 am 
Newbie

Joined: Sun Nov 02, 2008 10:27 am
Posts: 3
Hi,

Thank you for your reply. I will dig into spring to validate. I prefer to work without it using plain hibernate but i will try.

Thanks again,
Elad.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 8:55 am 
Newbie

Joined: Sun Nov 02, 2008 10:27 am
Posts: 3
Hi,

Thank you for your reply. I will dig into spring to validate. I prefer to work without it using plain hibernate but i will try.

Thanks again,
Elad.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.