线程和SqlFileStream。该进程无法访问指定的文件,因为它已在另一个事务中打开

时间:2015-05-11 21:01:13

标签: c# multithreading sql-server-2008 parallel.foreach sqlfilestream

我正在提取SQL文件表中的文件内容。如果我不使用Parallel,则以下代码有效。

我在同时读取sql文件流时遇到以下异常(并行)。

  

该进程无法访问指定的文件,因为它已在另一个事务中打开。

TL; DR:

在Parallel.ForEach中从FileTable读取文件(使用GET_FILESTREAM_TRANSACTION_CONTEXT)时,我得到了上述异常。

示例代码供您试用:

https://gist.github.com/NerdPad/6d9b399f2f5f5e5c6519

更长版本:

获取附件,并提取内容:

var documents = new List<ExtractedContent>();
using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
    var attachments = await dao.GetAttachmentsAsync();

    // Extract the content simultaneously
    // documents = attachments.ToDbDocuments().ToList(); // This works
    Parallel.ForEach(attachments, a => documents.Add(a.ToDbDocument())); // this doesn't

    ts.Complete();
}

DAO阅读文件表:

public async Task<IEnumerable<SearchAttachment>> GetAttachmentsAsync()
{
    try
    {
        var commandStr = "....";

        IEnumerable<SearchAttachment> attachments = null;
        using (var connection = new SqlConnection(this.DatabaseContext.Database.Connection.ConnectionString))
        using (var command = new SqlCommand(commandStr, connection))
        {
            connection.Open();

            using (var reader = await command.ExecuteReaderAsync())
            {
                attachments = reader.ToSearchAttachments().ToList();
            }
        }

        return attachments;
    }
    catch (System.Exception)
    {
        throw;
    }
}

为每个文件创建对象: 该对象包含对GET_FILESTREAM_TRANSACTION_CONTEXT的引用

public static IEnumerable<SearchAttachment> ToSearchAttachments(this SqlDataReader reader)
{
    if (!reader.HasRows)
    {
        yield break;
    }

    // Convert each row to SearchAttachment
    while (reader.Read())
    {
        yield return new SearchAttachment
        {
            ...
            ...
            UNCPath = reader.To<string>(Constants.UNCPath),
            ContentStream = reader.To<byte[]>(Constants.Stream) // GET_FILESTREAM_TRANSACTION_CONTEXT() 
            ...
            ...
        };
    }
}

使用SqlFileStream读取文件: 此处抛出异常

public static ExtractedContent ToDbDocument(this SearchAttachment attachment)
{
    // Read the file
    // Exception is thrown here
    using (var stream = new SqlFileStream(attachment.UNCPath, attachment.ContentStream, FileAccess.Read, FileOptions.SequentialScan, 4096))
    {
        ...
        // extract content from the file
    }

    ....
}

更新1:

根据this文章,它似乎可能是隔离级问题。有没有人遇到类似的问题?

1 个答案:

答案 0 :(得分:4)

交易未流入Parallel.ForEach,您必须手动将交易带入。

//Switched to a thread safe collection.
var documents = new ConcurrentQueue<ExtractedContent>();
using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
    var attachments = await dao.GetAttachmentsAsync();
    //Grab a reference to the current transaction.
    var transaction = Transaction.Current;
    Parallel.ForEach(attachments, a =>
    {
        //Spawn a dependant clone of the transaction
        using (var depTs = transaction.DependentClone(DependentCloneOption.RollbackIfNotComplete))
        {
            documents.Enqueue(a.ToDbDocument());
            depTs.Complete();
        }
    });

    ts.Complete();
}

我也从List<ExtractedContent>切换到ConcurrentQueue<ExtractedContent>因为您不允许同时从多个线程的列表中调用.Add(