ASP.NET。同步访问(互斥)
为了在 Web 环境中同步访问我的 NHibernate 会话,我尝试使用 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;
}
}
}
在我的错误日志记录中,我得到:
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() 时会出现此异常;
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()
Why do I get this exception with calling ReleaseMutex();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题在这一行
WaitOne() 可以抛出此异常< /a> 因为其他线程锁定它,退出时不会释放 释放 在您
的情况下,WaitOne 会抛出此异常,因为在其他线程中放弃了相同的互斥体。
我建议在类中扭曲互斥体并使用如下代码:
在上面的代码中,如果用户停止/放弃页面并且线程丢失/删除它,ReleaseMutex 可能无法运行。这就是为什么你会得到这个例外。
请注意,互斥体可以按照您的方式永远锁定! :) 最好在等待上添加毫秒限制,和/或处理非锁定的情况以返回只读数据。如果互斥体未能通过 WaitOne(),用户可以长时间锁定。
另外请注意,互斥体需要关闭并释放。!即使这就像 MSDN 中的示例,在 MSDN 中只是一个简单的示例,您需要确保关闭并处置您的互斥体,否则在更新页面时您会看到更多问题。例如,如果在您更新页面时互斥体在内存上保持锁定状态,那么您的页面可能会锁定很长时间,直到垃圾收集杀死它(如果他们这样做的话)。
Your issue is on this line
The WaitOne() can throw this exceptions because some other thread that lock it, exit with out releasing it
In your case the WaitOne throw this exception because of an abandon of the same mutex in an other thread.
I suggest to warp your mutex in a class and use a code like:
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.
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()
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.
除非您使用的是非常旧版本的 NHibernate,否则我认为这可能有点过分了。 NHibernate 已经能够在 Web 环境中为您提供上下文会话管理,并且我认为它将为您更可靠地管理事物。
看一下第 2.3 节:NHibernate 第 2 章 - 架构
Unless you're using a very old version of NHibernate, I think this is probably overkill. NHibernate already has the ability to give you contextual session management in a web environment, and I think it will manage things more reliably for you.
Take a look at section 2.3 of this: NHibernate Chapter 2 - Architecture
Windows 操作系统长期以来存在一个错误,即如果未按正确的相反顺序正确解锁,则将一个互斥锁锁定在另一个互斥锁内可能会同时锁定这两个互斥锁。
基本上,竞争条件和锁定可能是由于 NHibernate 使用互斥锁来锁定资源以及您正在使用的互斥锁造成的。
Windows O/S has long had a bug where locking one mutex inside another could lock both if they are not unlocked properly in the correct reverse sequence.
Basically the race condition and locking could be due to NHibernate using a mutex to lock the resource as well as the mutex you are using.