Hallo,Linq to SQL併發問題
我有可以調用多個方法的web服務。每次調用其中一種方法時,我都會將調用記錄到統計數據庫中,以便我們知道每個方法每個月調用多少次以及平均處理時間。
每次我登錄統計數據時,首先檢查數據庫以查看當前月份的方法是否已經存在,如果不存在,則創建並添加該行。如果它已經存在,我將需要的列更新到數據庫。
我的問題是,有時當我更新一行時,我得到「行未找到或更改」異常,是的,我知道這是因爲該行已被修改,因爲我讀它。
爲了解決這個問題我已經嘗試使用沒有成功如下:使用在我的DataContext
- 使用。
- 在TransactionScope中使用。
- 使用互斥量,這是行不通的,因爲Web服務是(不確定我稱之爲正確的想法)複製出來在不同的PC上的性能,但仍然使用相同的數據庫。
- 解決異常中的併發衝突,這不起作用,因爲我需要獲取新的數據庫值併爲其添加值。
下面我添加了用於記錄統計數據的代碼。任何幫助將非常感激。
public class StatisticsGateway : IStatisticsGateway
{
#region member variables
private StatisticsDataContext db;
#endregion
#region Singleton
[ThreadStatic]
private static IStatisticsGateway instance;
[ThreadStatic]
private static DateTime lastEntryTime = DateTime.MinValue;
public static IStatisticsGateway Instance
{
get
{
if (!lastEntryTime.Equals(OperationState.EntryTime) || instance == null)
{
instance = new StatisticsGateway();
lastEntryTime = OperationState.EntryTime;
}
return instance;
}
}
#endregion
#region constructor/initialize
private StatisticsGateway()
{
var configurationAppSettings = new System.Configuration.AppSettingsReader();
var connectionString = ((string)(configurationAppSettings.GetValue("sqlConnection1.ConnectionString", typeof(string))));
db = new StatisticsDataContext(connectionString);
}
#endregion
#region IStatisticsGateway members
public void AddStatisticRecord(StatisticRecord record)
{
using (db)
{
var existing = db.Statistics.SingleOrDefault(p => p.MethodName == record.MethodName &&
p.CountryID == record.CountryID &&
p.TokenType == record.TokenType &&
p.Year == record.Year &&
p.Month == record.Month);
if (existing == null)
{
//Add new row
this.AddNewRecord(record);
return;
}
//Update
existing.Count += record.Count;
existing.TotalTimeValue += record.TotalTimeValue;
db.SubmitChanges();
}
}
感謝您的回覆。我通過創建一個存儲過程來解決問題!像魅力一樣工作 – Michael 2010-09-29 12:34:35
非常棒!只是好奇,你在存儲過程中是否打擾了sp_getapplock? (因爲只是將邏輯移動到存儲過程將大大降低併發問題的機會。) – shaunmartin 2010-09-29 15:14:55
我使用了HOLDLOCK和ROWLOCK,請檢查我的答案。 – Michael 2010-09-30 10:56:36