multithreading - Asp.Net. Synchronization access(mutex) -


for synchronizing access nhibernate session @ web environment try use mutex:

public class factoryrepository {     private factoryrepository() { }     private static mutex _sessionmutex = new mutex();     private static isessionfactory factory;     public static isessionfactory sessionfactory     {                 {             factory = factory ?? new configuration().configure().buildsessionfactory();             return factory;         }     }      public static isession session     {                 {             isession currentsession;             _sessionmutex.waitone();             if (httpcontext.current != null)             {                 httpcontext context = httpcontext.current;                 currentsession = context.items[sessionkey] isession;                 if (currentsession == null || !currentsession.isopen)                 {                     currentsession = sessionfactory.opensession();                     context.items[sessionkey] = currentsession;                 }             }              _sessionmutex.releasemutex();             return currentsession;          }     } } 

at error logging get:

system.threading.abandonedmutexexception: wait completed due abandoned mutex. method: boolean waitone(int64, boolean)  stack trace:   @ system.threading.waithandle.waitone(int64 timeout, boolean exitcontext)  @ system.threading.waithandle.waitone(int32 millisecondstimeout, boolean exitcontext)  @ system.threading.waithandle.waitone() 

why exception calling releasemutex();

your issue on line

 _sessionmutex.waitone(); 

the waitone() can throw exceptions because other thread lock it, exit out releasing it

in case waitone throw exception because of abandon of same mutex in other thread.

i suggest warp mutex in class , use code like:

            try             {                 clock = _sessionmutex.waitone();                 // call work             }             catch (abandonedmutexexception)             {                 clock = true;                 // call work             }             catch (exception x)             {                 //error             }                         {                _sessionmutex.releasemutex();             } 

in above code releasemutex may fail run if user stop/abandon page , thread lost/delete it. , thats why exception.

be ware mutex can lock ever way ! :) better add millisecond limit on wait, , / or handle case of non lock return read data. users can lock long time if mutex fails pass waitone()

also ware, mutex need close , dispose. ! if example in msdn, in msdn simple example, need sure close , dispose mutex or else see more problems when update page. example if mutex stay on memory locked wile update page, page may lock long time, until garbage collection kill it, if do.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -