*
The moose likes Object Relational Mapping and the fly likes How does HibernateUtil work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How does HibernateUtil work?" Watch "How does HibernateUtil work?" New topic
Author

How does HibernateUtil work?

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Hi all,
I am reading the Hibernaate documentation. I am focusing on the creation of the session object.

1. As far as I understand it I first call the following code in a start up servlet.
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();

2. Then I use the HibernateUtil.class found in the Hibernate documentation.
Session session = HibernateUtil.currentSession();

My question is, if i use this technique, is a new instance of the Session object created for each servlet in which it is called? For example, in Login.java servlet I create a new Session object using HibernateUtil.java. Is this session object shared by all threads of Login.java or a Session object is created for each thread of Login.java

If someone could please explain how it works I would really appreciate it.

David
Jack Wiesenthaler
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
The current session is stored as a ThreadLocal variable (variable having a Thread level scope) so it can be shared by all objects that need to use it in the scope of that same request

When a new request arrives at the Login servlet, the servlet engine will create a new thread. Since it is a new thread the static class variable referencing the session will be null and the call to currentSession() will create a new session.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Originally posted by Kevin Arouza:
When a new request arrives at the Login servlet, the servlet engine will create a new thread. Since it is a new thread the static class variable referencing the session will be null and the call to currentSession() will create a new session.


Just a point of clarification: the static variable "session" is initialized to an instance of ThreadLocal; it can't be null, otherwise you'll get a NullPointerException. What is null is the value returned by the ThreadLocal instance's get() method when it is accessed by a new thread. (This may well be what you meant but it's not exactly how your reply reads)


Junilu - [How to Ask Questions] [How to Answer Questions]
Jack Wiesenthaler
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
Thanks Junilu, yeah it was a bit unclear.. I meant the actual value itself returned by get()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does HibernateUtil work?
 
Similar Threads
SessionFactory ...
Objects creation in Spring
Design question for creating a DAO using Hibernate
JNDI error message
Problem with Hibernate Cache