.NET应用程序中并发问题的最佳实践 [英] Best Practise on Concurrency Issues in .Net Application

查看:79
本文介绍了.NET应用程序中并发问题的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们计划将现有的VB6应用程序升级到C#(VS2008).
ADO.Net在访问数据时遵循了更多的断开连接策略.我想考虑一下实体框架,因为如果要加快开发速度,强烈建议使用ORM工具.
EF使用乐观锁定.

如何解决例如,当2个用户打开相同的客户"时,这是一个古老的问题.
ADO.net建议使用冲突异常在保存记录时检查记录是否已更改.

在旧的VB6中我们以前有2个系统的应用程序:
-记录上保留一个名为"Locked"的位和一个名为"LockedBy"的文本字段
.这很容易实现和使用,但是当应用程序崩溃或网络连接出现问题,该记录仍将被锁定.
-由于我们仅将MSSQL用作数据库,因此我们一直在为拥有SQL 2的客户使用MSSQL的行级锁定功能. 000或更高.

如何在现代.Net应用程序中处理此问题,如何解决.Net应用程序中的这一古老问题?

欢迎任何想法,评论或信息...

问候,
Sven Peeters

Hi,

We are planning to upgrade an existing VB6 application to C# ( VS2008 ).
ADO.Net follows more a disconnected strategy when accessing Data.
I'm thinking about giving the Entity Framework a shot since using an ORM-tool is higly recommended if you want to speed up the development.
And EF uses optimistic locking.

How do you solve the ancient problem when 2 users open the same 'customer' for example.
ADO.net suggests to check whether the record has changed when you save it using the conflict exceptions.

In the old VB6 app we used to have 2 systems :
- Keeping a bit called 'Locked' and a text-field called 'LockedBy' on the record
  This is very easy to implement and use but when the application crashes or there are problems with the network-connection, this record will still be locked.
- Since we only use MSSQL as database, we have been using the rowlevel-locking feature of MSSQL for those customers that have an SQL 2000 or higher.

How is this handled in modern .Net application, how do you solve this ancient problem in .Net applications?

Any idea's,remarks or info are welcome ...

Regards,
Sven Peeters

推荐答案

我个人喜欢使用这种形式的并发处理:

更新行时,您可以这样做:

更新表SET ... WHERE(每个字段=缓存的字段值)

检查受影响的行数,应为1或0.如果为0,则表示该行已被缓存,因此已被修改.

此时,您有两个选择:

1)通知用户保存失败,并询问他们是否要强制发布或重新加载记录.
2)与#1相同,但添加第三个选项,该选项将显示合并".结果(同时显示它们的更改和当前更改).

选项#1的实现非常简单,因此需要99%的时间.您可以根据具体情况实施#2,在这种情况下,记录量很大而#1会引起麻烦,或者并发问题很频繁.您可以通过分析更改并在用户更改和当前更改中的相同字段均未更改的情况下自动执行合并更新来进一步扩展#2.

此方法始终有效,因为它是100 %断开连接,尽管在经常发生并发冲突的系统中可能会很麻烦.
Personally I like to use this form of concurrency handling:

When you UPDATE a row you do it like so:

UPDATE table SET ... WHERE (every field = cached field value)

Check the number of rows affected, it should be 1 or 0. If it is 0, the row has been modified since it was cached.

At this point you have two options:

1) Inform user that the save failed and ask if they want to force the issue or reload the record.
2) Same as #1, but add a third option which will display a "merged" result (show both their changes and the current changes).

Option #1 is very simple to implement and is adequent 99% of the time. You can implement #2 on a case-by-case basis where the records are large and #1 would be inconveinent, or where concurrency issues are frequent. You can extend #2 even further by analyzing the changes and performing a merged update automatically in the case where the same field is not altered in both user and current changes.

This method always works because it is 100% disconnected, though it can be onerous in systems where concurrency conflict happens often.


这篇关于.NET应用程序中并发问题的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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