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.