PDA

View Full Version : SessionHolder syncronization


Aleksei Kachanov
08-02-2006, 12:17 PM
java


/**
* This Map needs to be synchronized because there might be multi-threaded
* access in the case of JTA with remote transaction propagation.
*/

private final Map sessionMap = Collections.synchronizedMap(new HashMap(1));


C#

private static readonly Hashtable sessionDictionary
= System.Collections.Hashtable.Synchronized(new Hashtable(1));


by static keyword sessionDictionary is shared between diffent threads as result different instances of SessionHolder always contains the same collection with session

This problem occured when you try integrate OpenSessionInView module and use single session per request

Mark Pollack
08-02-2006, 03:12 PM
Hi,

Great find, thanks! I've removed the static keyword and also used the synchronized hashtable implementation from the Spring.Collections namespace that was added recently in order to fix other threading issues as it is 'more thread safe' than Hashtable.Synchronized.


private readonly SynchronizedHashtable sessionDictionary
= new SynchronizedHashtable(new Hashtable(1));


You can find a lively discussion on this topic on Marcus Mac Innes' Blog (http://www.styledesign.biz/weblogs/macinnesm/archive/2004/09/21/196.aspx). It is also worth to note that synchronized hashtable wrappers are different in 1.1 and 2.0 and both have issues when cast to IEnumerable or ICollection. If you search for SynchronizedHashtable on Microsoft's list of CLR Run-Time breaking changes (http://msdn.microsoft.com/netframework/programming/breakingchanges/runtime/clr.aspx). (All credit to Aleks for digging into this..)


Anyway, I guess you are getting the latest code from CVS but you can also pick it up from the download page (http://www.springframework.net/downloads/Spring.Data.NHibernate/).

Cheers,
Mark

P.S. How is the OpenSessionInView Module working out?

Aleksei Kachanov
08-02-2006, 05:07 PM
It works
But with some limitations.
1) only if SingleSession=true
put Session into SessionFactoryUtils dictionary
2) lazy load works if methods in DAO objects mark as Transactional
not call of initSyncronization in TransactionSyncronizationManager