如何在数据库中组织 ASP.NET 请求锁定或行锁定

发布于 2024-08-11 02:14:56 字数 311 浏览 2 评论 0原文

我有 asp.net 页面/处理程序来访问图像。 当对图像执行第一个请求时,我将图像大小调整为标准大小(保存在磁盘上)并将其返回。 所以我需要锁定除一个之外的所有对图像的请求。这将调整图像大小。 图像由 URL 中的 ID 标识,因此我猜想每一张图像(URL 中的 ID)需要一个锁定对象。 我的问题是如何组织这个锁模型?

我的想法是在应用程序中添加锁对象(应用程序是同步的) 像这样 Application.Add(Request[Id], new object()); 并用它来锁定竞争线程。

此任务类似于数据库的行锁定或锁定集合中的元素。

感谢您的重播。

I've asp.net page/handler to access images.
When performed fist request to the image I resize image to standard size(save on disk) and return it.
So I need lock all request to the image except one. This one will resize image.
Image identified by ID in URL, so I guess one lock object required per one image(ID in URL).
My question is How can I organize this lock model?

My idea add lock object in Application (Application is synchronized)
like this Application.Add(Request[Id], new object());
and use it to locking competitive threads.

This task like row locking of DB or locking element in collection.

Thanks for your replay.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2024-08-18 02:14:56

最简单的方法是锁定图像文件。


using (FileStream fs = new FileStream("image.file", FileMode.Create, FileAccess.Write, FileShare.None))
{

resize image here 

}

当第二个(第三个等)线程尝试创建文件时,将抛出异常“进程无法访问文件...”。我在代码中处理这个异常。

The easiest way is locking image file with.


using (FileStream fs = new FileStream("image.file", FileMode.Create, FileAccess.Write, FileShare.None))
{

resize image here 

}

When second(third etc) thread try to create file the Exception "The process cannot access the file ..." will throw. And I process this exception in code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文