SqlDependency触发未提交的数据

时间:2015-03-13 09:25:35

标签: c# sql sqldependency

我们有一个在半夜运行的导入/批量复制批次,类似于

using (var tx = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(1)))
{
   //Delete existing
   ...
   //Bulk copy to temp table
   ...
   //Insert
   ...
   tx.Complete();
}

我们还有一个使用SqlDependency的缓存。但是它会在Delete语句中触发allready,使得Cache在删除和重新插入之间空了几秒钟。我可以将SqlDependency配置为仅侦听提交的数据吗?

SqlDep。代码

private IEnumerable<TEntity> RegisterAndFetch(IBusinessContext context)
{
    var dependency = new SqlDependency();
    dependency.OnChange += OnDependencyChanged;

    try
    {
        CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
        var refreshed = OnCacheRefresh(context, data.AsEnumerable());
        var result = refreshed.ToArray();
        System.Diagnostics.Debug.WriteLine("{0} - CacheChanged<{1}>: {2}", DateTime.Now, typeof(TEntity).Name, result.Length);

        return result;
    }
    finally
    {
        CallContext.SetData("MS.SqlDependencyCookie", null);
    }
}

OnDependencyChanged基本调用RegisterAndFetch方法

查询:

protected override IEnumerable<BankHoliday> OnCacheRefresh(IBusinessContext context, IEnumerable<BankHoliday> currentData)
{
    var firstOfMonth = DateTime.Now.FirstDayOfMonth(); // <-- Must be a constant in the query
    return context.QueryOn<BankHoliday>().Where(bh => bh.Holiday >= firstOfMonth);
}

0 个答案:

没有答案