代码之家  ›  专栏  ›  技术社区  ›  Zian Choy

与Linq-SQL Server和TransactionScope挂起

  •  0
  • Zian Choy  · 技术社区  · 16 年前

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

    代码: (请原谅代码着色……我对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可视化工具查看它时,我得到以下错误:

    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

    1 回复  |  直到 16 年前
        1
  •  1
  •   Randy Minder    16 年前

    这不是MSDTC问题。如果是的话,您会得到一个错误,说明未启用和需要启用DTC。这也不是死锁问题,因为在这方面您也会得到一个特定的错误。

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

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