-->
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: Problem: using ThreadLocal patern
PostPosted: Sat Oct 11, 2003 3:42 am 
Beginner
Beginner

Joined: Fri Sep 26, 2003 2:50 am
Posts: 32
I have a project now to use hibernate, the scene is following:
(1) I have a thread pool to manage threads for business handling.
(2) In every thread, it maybe access several datasources(two or three database servers)
(3) To every datasource, thread maybe access to the several tables.

I study the ThreadLocal sample (in document 42.html) and several other examples, but I have some questions:
(1) when should i invoke the method closeSession? (in these examples, I can find nowhere to invoke this method.)
(2) Do you have some better ways to be fit for my situation?Thanks.
(3) In a thread, how can i manage several datasource?

Thanks firstly.


Top
 Profile  
 
 Post subject: Re: Problem: using ThreadLocal patern
PostPosted: Sat Oct 11, 2003 7:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
jzhuo wrote:
I have a project now to use hibernate, the scene is following:
(1) I have a thread pool to manage threads for business handling.
(2) In every thread, it maybe access several datasources(two or three database servers)
(3) To every datasource, thread maybe access to the several tables.

I study the ThreadLocal sample (in document 42.html) and several other examples, but I have some questions:
(1) when should i invoke the method closeSession? (in these examples, I can find nowhere to invoke this method.)

At the end of each Unit of Work. Which is most cases is the end of the transaction.

Quote:
(2) Do you have some better ways to be fit for my situation?Thanks.

No - sounds like SLSB but your doing the work for yourself. Just don't
use the same session in different threads. You avoid this with ThreadLocal.

Quote:
(3) In a thread, how can i manage several datasource?

You need to create multiple SessionFactories, eg, One for each different datasource.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 11, 2003 10:56 am 
Beginner
Beginner

Joined: Fri Sep 26, 2003 2:50 am
Posts: 32
David, thanks for your reply!

Must i close the session at the end of work? why those examples do no close the Session? are they wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 11, 2003 1:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
why those examples do no close the Session

What examples? The document you mention shows some possible ways to implement the ThreadLocal pattern. There were no examples of how to utilize these impls.

As David mentioned, you're setup is very much like session beans. In a session bean setup, typically session beans control the unit-of-work or use-case. In fact, most of the time a session bean method corresponds to exactly to a use-case. So generally the session beans "prime" the thread local object prior to making the calls into the layer controlling Hibernate access (DAOs or whatever). After all Hibernate access is complete to fulfill the use-case the session bean closes the hibernate session.

So basically, wherever you logically perceive the business logic controller in your system, that layer should control the opening/closing of the Hibernate sessions.

Quote:
Must i close the session at the end of work?

You should to ensure proper transaction isolation.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 1:02 am 
Beginner
Beginner

Joined: Fri Sep 26, 2003 2:50 am
Posts: 32
Thanks Steve and David!

I will try as you say! thanks again!


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.