Subsonic 是否以某种方式使用乐观并发?
最佳答案
如果您所说的使用是指内置到SubSonic,那么不行。然而,使用 SubSonic 可以相当简单地实现乐观并发。
假设您正在使用 SQL Server(如果没有,我会让您将以下说明转换为适用于您的数据库提供商的解决方案),这是一种方法:
在您希望确保并发性的每个表上包含一个
timestamp
类型的列。CREATE TABLE Product ( ProductID int NOT NULL IDENTITY(1,1), Name varchar(256) NOT NULL, RowStamp timestamp /* This will hold a timestamp for the table */ )
读取时间戳值和数据,以便稍后使用它进行比较。
var product = new SubSonic.Select() .From<Product>() .Where(Product.ProductIDColumn).IsEqualTo(productId) .ExecuteSingle<Product>(); var rowStamp = product.RowStamp; // ... Show a form to the user with the data from the product
执行
UPDATE
时,将时间戳值与数据库值进行比较。如果时间戳不匹配,则该行已被修改,并且可以通知用户该情况(或者您可以按照自己的喜好进行处理)// ... After retrieving the values from the form var result = new SubSonic.Update(Product.TableSchema) .Set(Product.NameColumn).Equal(newName) .Where(Product.ProductIDColumn).IsEqualTo(productId) .And(Product.RowStamp).IsEqualTo(rowStamp) .Execute(); if (result != 1) { // Notify the user there may be a problem }
关于亚音速和乐观并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1506220/