在VB线程中从Oracle填充数据集

时间:2010-11-20 11:23:15

标签: vb.net multithreading oracle

我正在尝试从VB应用程序中运行几个PL / SQL过程。这些程序需要一段时间才能运行,所以我打算将它们分成不同的主题(我不熟悉的主题)

目前,每个线程都调用一个函数,该函数又调用以下代码:

' Function getDataSet
' builds a data set from given information
' Parameters:
'   aCommand - the PL/SQL procedure to call
'   aParamCollection - an oraParameterCollection object containing all the parameters
'   aDataSet - a data set to hold the results
' Returns:
'   number of rows retrieved

Public Function getDataSet( _
    ByVal aCommand As String, _
    ByVal aParamCollection As oraParameterCollection, _
    ByRef aDataSet As DataSet _
    ) As Integer

    Dim result As Integer = 0

    ' define commands etc
    Dim cmd As OracleCommand = New OracleCommand(aCommand, dbConn)
    cmd.CommandType = CommandType.StoredProcedure

    ' add parameters for the query
    For i As Integer = 0 To aParamCollection.getParameterCount() - 1
        cmd.Parameters.Add(New OracleParameter( _
                                aParamCollection.getParameterByIndex(i).ParameterName, _
                                aParamCollection.getParameterByIndex(i).ParameterType)).Value = _
                                aParamCollection.getParameterByIndex(i).ParameterValue
    Next

    Dim param As OracleParameter = New OracleParameter("io_cursor", OracleDbType.RefCursor)
    cmd.Parameters.Add(param).Direction = ParameterDirection.Output
    CheckConnection()
    cmd.ExecuteNonQuery()

    Dim cur As OracleRefCursor
    cur = param.Value
    cmd.Dispose()

    Dim theAdaptr As New OracleDataAdapter

    If aDataSet IsNot Nothing Then
        theAdaptr.Fill(aDataSet, cur)
        result = aDataSet.Tables(0).Rows.Count
    End If

    Return result

End Function

其中:
aCommand =要执行的PL / SQL proc aParamCollection =一个包含参数,类型和值的自定义类 aDataSet =保存结果数据的数据集

但是,当我运行应用程序时,此时会出现访问冲突:

theAdaptr.Fill(aDataSet, cur)

我假设单独的线程不能同时调用Fill,这是正确的吗?我目前正在开始每个线程:

    Dim th_Mean As New Thread(AddressOf getMeanData)
    Dim th_Stdev As New Thread(AddressOf getStdevData)

    th_Mean.Name = "th_Mean"
    th_Stdev.Name = "th_Stdev"

    th_Mean.Start()
    th_Stdev.Start()    

    Do While th_Mean.IsAlive Or th_Stdev.IsAlive
    Loop

最终,我的问题是,我应该应该这样做以避免发生访问冲突?

感谢阅读 BBZ

1 个答案:

答案 0 :(得分:0)

您是否考虑过在数据库中添加并行性?使用声明性SQL,并行性(相对)容易。根据您的存储过程,它可能像查找慢查询和添加提示一样简单,如:

select /*+ parallel(my_table) */ * from my_table;

如果该过程更多地是关于逐行处理而不是查询,则可能需要查看并行流水线函数。