我们在插入文件表时遇到问题。这是我们现在的星座,我会尽量详细的解释一下:
基本上我们有三个表:
- T_Document(文档的主要元数据)
- T_Version(文档的版本化元数据)
- T_Content(文档的二进制内容,FileTable)
我们的 WCF 服务保存文档并被多人使用。该服务将打开一个事务并调用保存文档的方法 SaveDocument:
//This is the point where the tranaction starts
using (IDBTransaction tran = db.GetTransaction())
{
try
{
m_commandFassade.SaveDocument(document, m_loginName, db, options, lastVersion);
tran.Commit();
return document;
}
catch
{
tran.Rollback();
throw;
}
}
SaveDocument 方法如下所示:
public void SaveDocument(E2TDocument document, string login, IDBConnection db, DocumentUploadOptions options, int lastVersion)
{
document.GuardNotNull();
options.GuardNotNull();
if (lastVersion == -1)
{
//inserting new T_Document
SaveDocument(document, db);
}
else
{
//updating the existing T_Document
UpdateDocument(document, db); //document already exists, updating it
}
Guid contentID = Guid.NewGuid();
//inserting the content
SaveDocumentContent(document, contentID, db);
//inserting the new / initial version
SaveDocumentVersion(document, contentID, db);
}
基本上,您看到的所有方法都是插入或更新这三个表。插入内容查询,这似乎会带来一些麻烦,如下所示:
INSERT INTO T_Content
(stream_id
,file_stream
,name)
VALUES
(#ContentID
,#Content
,#Title)
以及方法(请将此作为伪代码):
private void SaveDocumentContent(E2TDocument e2TDokument, Guid contentID, IDBConnection db)
{
using (m_log.CreateScope<MethodScope>(GlobalDefinitions.TracePriorityForData))
{
Command cmd = CommandFactory.CreateCommand("InsertContents");
cmd.SetParameter("ContentID", contentID);
cmd.SetParameter("Content", e2TDokument.Content);
string title = string.Concat(e2TDokument.Titel.RemoveIllegalPathCharacters(), GlobalDefinitions.UNTERSTRICH,
contentID).TrimToMaxLength(MaxLength_T_Contents_Col_Name, SuffixLength_T_Contents_Col_Name);
cmd.SetParameter("Title", title);
db.Execute(cmd);
}
}
我没有死锁分析的经验,但死锁图告诉我,当将内容插入文件表时,它似乎与另一个进程同时写入同一个表而死锁。
(对方显示相同的语句,我的应用程序日志确认两次并发尝试保存文档)
同样的僵局每天出现 30 次。我已经将事务缩减到最低限度,删除了所有不必要的选择,但是我没有运气解决这个问题。
我最好奇的是如何在插入文件表时发生死锁。是否有我不知道的内部正在执行的事情。我在该表的分析器跟踪中看到了一些奇怪的语句,我们不会在代码中的任何地方使用它们,例如:
set @pathlocator = convert(hierachyid, @path_locator__bin)
和类似的东西:
if exists (
select 1
from [LGOL_Content01].[dbo].[T_Contents]
where parent_path_locator = @path_locator
)
如果您需要更多详细信息,请告诉我。如何进行的任何提示都会很棒。
编辑1:\
在您找到 T_Content 插入的执行计划之后: