Asp.Net。同步访问(互斥) [英] Asp.Net. Synchronization access(mutex)

查看:781
本文介绍了Asp.Net。同步访问(互斥)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同步在网络环境下我的NHibernate会话访问我尝试使用互斥:

for synchronizing access to my NHibernate session at web environment I try use Mutex:

public class FactoryRepository
{
    private FactoryRepository() { }
    private static Mutex _sessionMutex = new Mutex();
    private static ISessionFactory factory;
    public static ISessionFactory SessionFactory
    {
        get
        {
            factory = factory ?? new Configuration().Configure().BuildSessionFactory();
            return factory;
        }
    }

    public static ISession Session
    {
        get
        {
            ISession currentSession;
            _sessionMutex.WaitOne();
            if (HttpContext.Current != null)
            {
                HttpContext context = HttpContext.Current;
                currentSession = context.Items[SessionKey] as ISession;
                if (currentSession == null || !currentSession.IsOpen)
                {
                    currentSession = SessionFactory.OpenSession();
                    context.Items[SessionKey] = currentSession;
                }
            }

            _sessionMutex.ReleaseMutex();
            return currentSession;

        }
    }
}

在我的错误日志,我得到:

At my error logging I get:

System.Threading.AbandonedMutexException: The wait completed due to an abandoned mutex.
Method: Boolean WaitOne(Int64, Boolean)

Stack Trace:

 at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext)
 at System.Threading.WaitHandle.WaitOne(Int32 millisecondsTimeout, Boolean exitContext)
 at System.Threading.WaitHandle.WaitOne()

为什么会出现这种异常,并调用ReleaseMutex();

Why do I get this exception with calling ReleaseMutex();

推荐答案

您的问题是在这条线

 _sessionMutex.WaitOne();

WaitOne的的()抛出此 的例外因为一些其他的线程锁定,退出与出释放它

The WaitOne() can throw this exceptions because some other thread that lock it, exit with out releasing it

在你的情况的WaitOne抛出,因为在其他线程的放弃同一个互斥的这个异常。

In your case the WaitOne throw this exception because of an abandon of the same mutex in an other thread.

我建议你扭曲互斥中的一类,并使用code,如:

I suggest to warp your mutex in a class and use a code like:

            try
            {
                cLock = _sessionMutex.WaitOne();
                // call your work
            }
            catch (AbandonedMutexException)
            {
                cLock = true;
                // call your work
            }
            catch (Exception x)
            {
                //Error
            }
            finally
            {
               _sessionMutex.ReleaseMutex();
            }

在上面的code中的ReleaseMutex可能无法运行,如果用户停止/放弃页面和线程丢失/删除它。而这就是为什么你得到这个例外。

In the above code the ReleaseMutex may fail to run if a user stop/abandon the page and the thread is lost/delete it. And thats why you get this exception.

要洁具互斥锁可以锁定任何你做到这一点! :)其更好地添加毫秒极限的等待,和/或处理非锁定的情况下,只返回读取数据。您的用户可以锁定很长一段时间,如果互斥未能通过WaitOne的()

Be ware that mutex can lock for ever the way you do it ! :) Its better to add a millisecond limit on the wait, and / or handle the case of non lock to return read only data. You users can lock for long time if the mutex fails to pass the WaitOne()

另外要洁具,在互斥需要接近和处理。!即使这是像MSDN中的例子,在MSDN中只是一个简单的例子,你需要确保你密切和处置您的Mutex,否则你看到更多的问题,当您更新网页。例如,如果互斥留在记忆锁定欺骗您更新网页,网页可以锁定很长一段时间,直到垃圾收集杀死它,如果他们这样做。

Also be ware, the Mutex need to be close and dispose. ! Even if this is like the example in MSDN, in MSDN is only an simple Example, you need to be sure that you close and dispose your Mutex or else you see more problems when you update your page. For example if the Mutex stay on memory locked wile you update your page, then your page may lock for long time, until the Garbage collection kill it, if they do.

这篇关于Asp.Net。同步访问(互斥)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆