内部事务范围错误:“与底层事务管理器的通信失败”

发布于 2024-07-14 23:34:53 字数 1794 浏览 6 评论 0原文

我不确定我是否正确地执行了这些操作。 是因为我打开了两个连接吗? 不管有没有错误,我都会关闭它们。 我确实尝试放入一些内部事务范围并将第二个设置为 RequiresNew 。 这两种方法是相互独立的,但是,如果其中一种方法失败,我需要它们都回滚。 我觉得我可能必须修改创建和关闭连接的方式。 有什么想法吗? 这是我正在做的一些示例代码:

Public Sub TransMethod()
    Using sTran As New Transactions.TransactionScope
        factory1.UpdateMethod(someObject1)
        facotry2.insert(someobject2)
        sTran.Complete()
    End Using
End Sub

Public Class factory1
    Public Shared Sub UpdateMethod(obj)
        dim someSQLParams....
        DataAcces.ExecuteNonQuery(command,someSQLParams)
    End Sub
End Class

Public Class factory2
    Public Shared Sub Insert(obj)
        dim someSQLParams....
        DataAcces.ExecuteNonQuery(command,someSQLParams)
    End Sub
End Class

Public Function ExecuteNonQuery(ByVal spname As String, _
        ByVal ParamArray parameterValues() As Object) As Object
    Dim connection As SqlConnection = Nothing            
    Dim command As SqlCommand = Nothing
    Dim res As Object = Nothing
    Try
        connection = New SqlConnection(_connectionString)
        command = New SqlCommand(spname, connection)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddRange(parameterValues)
        connection.Open()
        command.ExecuteNonQuery()
        res = command.Parameters(command.Parameters.Count - 1).Value
    Catch ex As Exception
        CreateDataEntry(ex, WriteType.ToFile, spname)
        If Not (transaction Is Nothing) Then
            transaction.Rollback()
        End If
    Finally
        If Not (connection Is Nothing) AndAlso _
            (connection.State = ConnectionState.Open) Then _
                connection.Close()
        If Not (command Is Nothing) Then command.Dispose()
    End Try
    Return res
End Function

I am not sure I am doing any of this correctly. Is it because I am opening two connections? I am closing them regardless of errors. I did try putting in some inner transaction scopes and setting the second one to RequiresNew. The two methods are independent of each other, however, if one fails I need them both to rollback. I may have to modify how I am creating and closing the connections, I feel. Any thoughts? Here is some example code of what I am doing:

Public Sub TransMethod()
    Using sTran As New Transactions.TransactionScope
        factory1.UpdateMethod(someObject1)
        facotry2.insert(someobject2)
        sTran.Complete()
    End Using
End Sub

Public Class factory1
    Public Shared Sub UpdateMethod(obj)
        dim someSQLParams....
        DataAcces.ExecuteNonQuery(command,someSQLParams)
    End Sub
End Class

Public Class factory2
    Public Shared Sub Insert(obj)
        dim someSQLParams....
        DataAcces.ExecuteNonQuery(command,someSQLParams)
    End Sub
End Class

Public Function ExecuteNonQuery(ByVal spname As String, _
        ByVal ParamArray parameterValues() As Object) As Object
    Dim connection As SqlConnection = Nothing            
    Dim command As SqlCommand = Nothing
    Dim res As Object = Nothing
    Try
        connection = New SqlConnection(_connectionString)
        command = New SqlCommand(spname, connection)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddRange(parameterValues)
        connection.Open()
        command.ExecuteNonQuery()
        res = command.Parameters(command.Parameters.Count - 1).Value
    Catch ex As Exception
        CreateDataEntry(ex, WriteType.ToFile, spname)
        If Not (transaction Is Nothing) Then
            transaction.Rollback()
        End If
    Finally
        If Not (connection Is Nothing) AndAlso _
            (connection.State = ConnectionState.Open) Then _
                connection.Close()
        If Not (command Is Nothing) Then command.Dispose()
    End Try
    Return res
End Function

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

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

发布评论

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

评论(1

故事还在继续 2024-07-21 23:34:53

每当您使用 TransactionScope 打开超过 1 个连接时,它就会从针对 sql server 的普通事务更改为分布式事务,这需要设置 MSDTC。

即使连接具有相同的连接字符串,也会发生这种情况,这是“设计”问题之一。 我还没有跟进.net 3.5+上是否保持不变

Whenever you open more than 1 connection using TransactionScope, it changes from a normal transaction against sql server to a distributed transaction, which requires setting up the MSDTC.

That will happen even if the connections have the same connection string, which is one of the "by design" issues. I haven't followed up if it remains the same on .net 3.5+

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