C#数据库应用并发 [英] C# Database Application Concurrency

查看:279
本文介绍了C#数据库应用并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#写了一个多用户应用程序中使用SQL Server 2005 Express的作为后端一段时间的年龄。

I wrote a multi user app in c# some time age using SQL Server 2005 express as back-end.

我有一个订单的集合。为了使用这个类,你需要实例化它,只是调用Load(CustomerCode),以填充指定customers`s订单采集方法。

I have a Orders collection. In order to use this class you would need to instantiate it and just call the Load(CustomerCode) method in order to populate the collection with the specified customers`s orders.

我的问题:

我如何强制执行并发性,所以,只有1用户可以请求订单收集特定客户?当用户与对象(当对象设置为null),
做我将需要再次使用。

How do I enforce concurrency, so that only 1 user can request a Orders collection for a specific customer? When the user is done with the object(when the object is set to null), I will need to make it available again.

推荐答案

您需要执行悲观离线锁模式。

从本质上讲,你有你把记录在代表一个在其他表中的记录锁定的表。当您想要编辑的记录,检查,看看是否有锁表锁第一,并在你的域逻辑/ UI做出相应的反应。

Essentially you have a table that you put records in that represent a "lock" on records in other tables. When you want to edit a record, you check to see if there's a lock in the lock table first, and react accordingly in your domain logic/UI.

一点也没有'吨有是一个数据库,它可能是一个内存缓存。当我说表在我的例子,我的意思是一个逻辑表,不一定是数据库中的一个。

It doesn't have to be a database, it could be an in-memory cache. When I say "table" in my example, I mean a logical table, not necessarily a database one.

悲观离线锁可防止
避免通通冲突。
它迫使商业交易到
上的一块数据
的获取锁就开始使用它之前,这样一来,
大部分的时间,一旦你开始
商业交易,你可以相当
相信你一定会完成它而不
反弹由并发控制。

Pessimistic Offline Lock prevents conflicts by avoiding them altogether. It forces a business transaction to acquire a lock on a piece of data before it starts to use it, so that, most of the time, once you begin a business transaction you can be pretty sure you'll complete it without being bounced by concurrency control.

这篇关于C#数据库应用并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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