-->
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: Thread Local Session
PostPosted: Thu Oct 02, 2003 8:15 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I have, on the hibernate site, a description of the ThreadLocal Session but i don't understand the aim of this "pattern". Can someone explain to me how it works and the use of it?

Next, i'm working on a web site that uses Hibernate as O/R mapping layer and java technology ( jsp, servlets end JavaServer Faces).
There will be a lot a users connected at a time.
What is the best way to manage such connections at a time ( Thread Local Session or others patterns) ?

Thanks.


Top
 Profile  
 
 Post subject: Thread Local Session
PostPosted: Thu Oct 02, 2003 8:41 am 
Newbie

Joined: Thu Sep 18, 2003 8:37 am
Posts: 3
The Thread Local Session design pattern provides a way to bind each thread of execution with one Hibernate Session. This enables the Session to be propagated between different classes such as DAO (another common design pattern).
Say, for example, I have DAO1 and DAO2 used in the following code :
Code:
DAO1.doSomething();
DAO2.doSomethingElse();


Using the Thread Local Session, I can open and close a single Hibernate Session which will be bound to the runnnig thread, and my two DAOs will share the same Session, thus benefiting from advanced mechanism such as concurrency checks and cache. This would give something like :

Code:
ThreadLocalSession.openNewSession();
DAO1.doSomething(); // will use the session opened just above
DAO2.doSomethingElse(); // will use that same session
ThreadLocalSession.closeSession();


Laurent[/code]


Top
 Profile  
 
 Post subject: More precisions
PostPosted: Thu Oct 02, 2003 9:35 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
Thanks for yours explanations and your example.
But in the doSomeThing() methods of DAO1 and DAO2, how to do to retrieve the session open by the ThreadLocalSession.openNewSession() call.
Is there a static method getSession() in the ThreadLocalSession class or something like that in order that the doSomeThing() methods get the opening session?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 10:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In your ThreadLocalSession class, a static method will allow you to get the current session (see the design pattern section for implementation detail)

See below the DAO code
Code:
public class MyDao {
    public String getName() {
         ...
         HibernateSession session = ThreadLocalSession .currentSession();
         ...
    }


}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2003 4:52 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
Effectively, what you're doing with a ThreadLocal Session is execute multiple data access operations (like DAO method invocations) within a kind of transaction. Note that a Hibernate Session keeps its own cache of persistent objects, therefore operations on a Session should get executed within a database transaction too, for data consistency - even read-only operations. session.beginTransaction on Session creation and a commit/rollback on Session closing would achieve that.

As noted at http://www.hibernate.org/42.html, the Spring Framework's transaction support includes special thread-local handling of Hibernate Sessions under the hood. You demarcate a transaction that spans multiple operations, be it with the Hibernate transaction strategy or JTA: Then each of your operations will receive the same Session instance, within a proper database transaction. The only programming pattern required is to look up a Session via Spring's SessionFactoryUtils.getSession(SessionFactory) method, or use Spring's HibernateTemplate helper class. See http://www.hibernate.org/110.html for details.

Juergen


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.