与 LINQ-SQL Server 和 TransactionScope 挂起

发布于 2024-08-25 05:33:29 字数 1707 浏览 8 评论 0原文

当程序尝试访问水果数据库时,我遇到挂起。我已经在我的开发计算机和 SQL Server 服务器上启用了网络访问 MSDTC。

代码: (请原谅代码着色...SO 误解了我的 VB .NET)

Using ts As New TransactionScope
            Dim fruit As New FruitDataContext
            Dim thingies As New ThingiesDataContext
            If (From f In fruit.tblApples Where f.Rotten = "Yes" AndAlso f.batch = 1).Count >= 1 Then
                'Record today's date as the day that the rotten apples were dumped.
            End If

            'Other complicated code that uses ThingiesDataContext and FruitDataContext

            du.SubmitChanges()
            ts.Complete()
End Using

编辑:

我进行了更多研究,结果发现问题出在 LINQ 行上。当我尝试使用 LINQ to SQL Visualizer 查看它时,出现以下错误:

System.InvalidCastException: Specified cast is not valid.
   at LinqToSqlQueryVisualizer.SqlQueryInfo.deserialize(Stream stream)
   at LinqToSqlQueryVisualizer.Visualizer.Display(IDialogVisualizerService windowService, Stream rawStream)
   at LinqToSqlQueryVisualizer.DialogChooser.Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
   at Microsoft.VisualStudio.DebuggerVisualizers.DebugViewerShim.ManagedShim.DelegatedHost.CreateViewer(IntPtr hwnd, HostServicesHelper hsh, SafeProxyWrapper proxy)

我还编辑了 LINQ 语句以使其更接近我的真实代码。

最终编辑: 我尝试使用普通的 SqlConnection 而不是“thingies as New ThingiesDataContext”,但问题仍然出现。

TransactionScope 似乎无法处理同一事务内的多个 SQL 连接。

微软官方说明

SQL Server 不支持并行事务。

来自 MSDN: http://msdn.microsoft.com/en-us/library /bb896149.aspx

I'm encountering a hang when the program tries to access the fruit database. I've already enabled network access MSDTC on both my development computer and the SQL Server server.

Code:
(pardon the code coloring...SO's misinterpreting my VB .NET)

Using ts As New TransactionScope
            Dim fruit As New FruitDataContext
            Dim thingies As New ThingiesDataContext
            If (From f In fruit.tblApples Where f.Rotten = "Yes" AndAlso f.batch = 1).Count >= 1 Then
                'Record today's date as the day that the rotten apples were dumped.
            End If

            'Other complicated code that uses ThingiesDataContext and FruitDataContext

            du.SubmitChanges()
            ts.Complete()
End Using

Edit:

I've dug around a bit more and it turns out that the problem lies in the line of LINQ. When I tried to view it with the LINQ to SQL Visualizer, I get the following error:

System.InvalidCastException: Specified cast is not valid.
   at LinqToSqlQueryVisualizer.SqlQueryInfo.deserialize(Stream stream)
   at LinqToSqlQueryVisualizer.Visualizer.Display(IDialogVisualizerService windowService, Stream rawStream)
   at LinqToSqlQueryVisualizer.DialogChooser.Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
   at Microsoft.VisualStudio.DebuggerVisualizers.DebugViewerShim.ManagedShim.DelegatedHost.CreateViewer(IntPtr hwnd, HostServicesHelper hsh, SafeProxyWrapper proxy)

I've also edited the LINQ statement to be closer to my real code.

Final edit:
I tried using a normal SqlConnection instead of a "thingies as New ThingiesDataContext" and the problem still occurs.

It appears that TransactionScope cannot handle multiple SQL connections inside the same transaction.

Official Microsoft Note

parallel transactions are not supported by SQL Server.

From MSDN: http://msdn.microsoft.com/en-us/library/bb896149.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-09-01 05:33:29

这不是 MSDTC 问题。如果是这样,您会收到一条错误消息,指出 DTC 未启用但需要启用。这也不是死锁问题,因为您也会收到与此相关的特定错误。

如果我不得不猜测,我会说“其他复杂的代码...”正在尝试执行数据库操作,并且被一个或其他数据库上下文对象阻止。

确定这一点的一种方法是运行 SQL Profiler 以查看服务器上实际执行的 SQL 语句,并检查块。

This is not an MSDTC issue. If it were, you would get an error saying DTC is not enabled and needs to be. It's also not a deadlock issue, because you would get a specific error about that as well.

If I had to guess, I would say that the 'Other complicated code...' is attempting to perform a database operation and is being blocked by one or the other database context objects.

One way you can determine this is to run SQL Profiler to see what SQL statements are actually being executed on the server, and check for blocks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文