0

我试图在我的 Oracle 数据库中的表上运行插入查询。下面是我正在做的测试。

  1. 首先锁定我想从 SQL Developer 会话中插入的表

    lock table RANDOM_TABLE in exclusive mode nowait
    
  2. 尝试使用 DbCommand.ExecuteNonQuery 运行插入
  3. 我已将 CommandTimeout 属性设置为 60 秒

即使在完成 60 秒后,控件也没有返回,但是当我解锁表时,它正在执行插入并且还抛出异常。

我认为,当命令超时到期但控制没有从本机代码返回时,.NET 正确地抛出异常,直到表被解锁。在这种情况下如何避免无限期等待?

 Public Shared Function ExecuteNonQuery(ByVal ConnectionString As String, ByVal factory As DbProviderFactory, ByVal sql As String) As Integer

    Dim rowsAffected As Integer = -1

    Using conn As DbConnection = factory.CreateConnection
        conn.ConnectionString = ConnectionString
        conn.Open()
        If conn.State.Equals(ConnectionState.Open) Then

            Try
                'This works
                Dim command As DbCommand = conn.CreateCommand
                command.CommandText = sql
                command.CommandType = CommandType.Text
                command.CommandTimeout = 60

                '**********This blocks and never returns.
                rowsAffected = command.ExecuteNonQuery()
            Catch ex As Exception
                Debug.Print(ex.Message)
            End Try

        End If
    End Using
    Return rowsAffected
End Function
4

0 回答 0