DbCommand.ExecuteNonQuery hangs indefinitely

808 views Asked by At

I was trying to run an insert query on table in my Oracle database. below is the test i'm doing.

  1. First lock the table on which i want to do an insert from a SQL Developer session

    lock table RANDOM_TABLE in exclusive mode nowait
    
  2. Try running an insert using DbCommand.ExecuteNonQuery
  3. I've had the CommandTimeout property set to 60 seconds

Even after completion of 60 seconds the control did not return but when i unlocked the table, it is doing the insert and also throwing an exception.

I figured that, .NET is properly throwing an exception when the command timeout expires but control was not returning from native code until the table is unlocked. How to avoid indefinite wait in this situation?

 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
0

There are 0 answers