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.  [ 6 posts ] 
Author Message
 Post subject: Sessions and Threads
PostPosted: Fri Sep 07, 2007 4:40 pm 
Newbie

Joined: Thu Jun 14, 2007 3:46 am
Posts: 17
Hi,

I wondering how to handle with session and transaction in a windows application. In java you can use getCurrentSession and al your problems with different threads and hibernate are solved. But if I use GetCurrentThread i get an exception. (miss some settings but i can't find how to configure, only for webapplications)

So have people good examples how to work with the session? Or maybe know how to configure GetCurrentSession in a windows application

Grtz Dave

ps; i know; when i start this thread there was a link to : http://hibernate.org/42.html; but this only for java.. and i got problems to translate it to c#


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 10, 2007 3:25 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
I basically use the ThreadStatic attribute on the variable that gets my current session. I'm not certain that this is the direct C# translation but it works for me. I can now launch multiple threads which each get their own Session.

[ThreadStatic]
private static TransactionState _transaction;


Top
 Profile  
 
 Post subject: Re: Sessions and Threads
PostPosted: Thu Sep 13, 2007 5:44 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
DaveSmits wrote:
Hi,

I wondering how to handle with session and transaction in a windows application. In java you can use getCurrentSession and al your problems with different threads and hibernate are solved. But if I use GetCurrentThread i get an exception. (miss some settings but i can't find how to configure, only for webapplications)

So have people good examples how to work with the session? Or maybe know how to configure GetCurrentSession in a windows application

Grtz Dave

ps; i know; when i start this thread there was a link to : http://hibernate.org/42.html; but this only for java.. and i got problems to translate it to c#


First of I believe that the Session implementation class is not threadsafe, so you want only one instance per thread. I've used variants of the approach presented by Billy McCafferty to manage session and transactions. You basically make a ThreadSafe session manager class (singleton) that you use to hand out references to current sessions and transactions. Transaction begins, commits, and rollback are managed through this class...makes testing a bit easier too (you can begin and rollback transactions in a test fixture base/super class.

As far as storing the current transaction and session for the current thread context I use the CallContext class from the System.Runtime.Remoting.Messaging namespace.
see: http://msdn2.microsoft.com/en-us/librar ... ntext.aspx
This class is the windows application analog to ASP.Net's HttpContext. You can use it to cache both the current session and the current thread. Since your session manager will be a singleton, this will facilitate sharing transactions among components. I think this is the sort of thing you are looking for.

You also might like to check out McCafferty's article:
http://www.codeproject.com/aspnet/NHibe ... ctices.asp

He's focused on ASP.Net, but alot of the principles are applicable to desktop apps too. The session manager solution he presents supports both web and desktop contexts. Besides, it makes for good leisure reading that you can tote along throughout the day.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 14, 2007 9:15 am 
Newbie

Joined: Sat Jun 09, 2007 8:22 pm
Posts: 9
Castle's Windsor project has a really nice NHibernate facility that can handle all the session management for you.

http://www.castleproject.org/container/facilities/trunk/nhibernate/index.html

Plus you get a slick IoC container.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 14, 2007 3:14 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Castle's the cool way to go...the company I'm currently contracted with is unwilling to let me use it though...

:(

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject: Re: Sessions and Threads
PostPosted: Mon Sep 17, 2007 2:01 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
DaveSmits wrote:
You basically make a ThreadSafe session manager class (singleton) that you use to hand out references to current sessions and transactions. Transaction begins, commits, and rollback are managed through this class...


Sorry, I thought that went without saying. Pleaee consider what the 'ThreadStatic' attribute does though. This probably ends up being easier than you think.


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