Oracle CommandTimeout不起作用。挂锁表锁直到锁定被释放

时间:2017-09-25 07:36:53

标签: .net oracle oracle11g odp.net odac

在我的.Net应用程序中,我遇到了Oracle CommandTimeout的问题。 当我使用text命令查询锁定表时没有绑定参数CommandTimeout按预期方式触发。但是如果我引入并绑定任何参数到命令当前线程挂起并且超时异常永远不会触发(直到释放表上的锁)。

我尝试了从http://www.oracle.com/下载的最新ODAC 12.2c第1版(12.2.0.1.0)(Oracle.ManagedDataAccess和Oracle.DataAccess)以及来自nuget的Oracle.ManagedDataAccess。它在11g Express Edition和Oracke 12c数据库上测试没有成功。

这是我的c#代码:

我用查询锁定表:

LOCK TABLE document  
IN EXCLUSIVE MODE NOWAIT; 

C#代码:

using System;  
using System.Data;  
using Oracle.ManagedDataAccess.Client;  

namespace ConsoleApplication  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            using (var connection = new OracleConnection("oracle connection string"))  
            using (var command = new OracleCommand("update Document set pagecount = 0 where rownum = :rn", connection))  
            {  
                connection.Open();  
                command.CommandType = CommandType.Text;  
                command.CommandTimeout = 5;  
                command.Parameters.Add(new OracleParameter("rn", 1));  

                try  
                {  
                    command.ExecuteNonQuery();  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine("Exception: " + ex.Message);  
                }  
            }  
        }  
    }  
}

如果更改此

,则可以正常工作
using (var command = new OracleCommand("update Document set pagecount = 0 where rownum = :rn", connection))

using (var command = new OracleCommand("update Document set pagecount = 0 where rownum = 1", connection))  

但我需要使用参数绑定。

配置文件:

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <configSections>  
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>  
    </configSections>  
  <startup>  
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>  
  </startup>  
  <system.data>  
    <DbProviderFactories>  
      <remove invariant="Oracle.ManagedDataAccess.Client"/>  
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"  
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>  
    </DbProviderFactories>  
  </system.data>  
  <oracle.manageddataaccess.client>  
    <version number="*">  
      <settings>  
        <setting name="Disable_Oob" value="on" />  
      </settings>  
    </version>  
  </oracle.manageddataaccess.client>  
</configuration>

我还尝试了使用&#34;在&#34;上的Disable_Oob和&#34;关&#34;选项,但没有效果。

使用PowerShell的另一个例子:

Add-Type -Path "Oracle.DataAccess.dll"  

$connectionString = "connection string here"  

#Timeout works fine (command without parameters)  
try {  
    $dbConn = New-Object Oracle.DataAccess.Client.OracleConnection  
    $dbConn.ConnectionString = $connectionString  
    $dbConn.Open()  

    $tran = $dbConn.BeginTransaction()  

    $cmd = New-Object Oracle.DataAccess.Client.OracleCommand  
    $cmd.Connection = $dbConn  
    $cmd.Transaction = $tran  
    $cmd.CommandTimeout = 5  
    $cmd.CommandText = "update Document set pagecount = 0 where rownum = 1"  

    $cmd.ExecuteNonQuery()  
}  
finally {  
    if ($tran) {$tran.Rollback()}  
    $dbConn.Close()  
    Write-Host "Timeout works fine for command without parameters."  
}  

#Timeout fired only after table lock is released (command with parameters)  
Write-Host "Attempting to execute command with parameter"  

try {  
    $dbConn = New-Object Oracle.DataAccess.Client.OracleConnection  
    $dbConn.ConnectionString = $connectionString  
    $dbConn.Open()  

    $tran = $dbConn.BeginTransaction()  

    $cmd = New-Object Oracle.DataAccess.Client.OracleCommand  
    $cmd.Connection = $dbConn  
    $cmd.Transaction = $tran  
    $cmd.CommandTimeout = 5  
    $cmd.CommandText = "update Document set pagecount = 0 where rownum = :rn"  
    $cmd.Parameters.Add(":rn", 1);  

    Write-Host "Hangs here if table is locked with exclusive lock."  
    $cmd.ExecuteNonQuery()  
}  
finally {  
    if ($tran) {$tran.Rollback()}  
    $dbConn.Close()  
    Write-Host "Never comes here if lock is not released"  
}  

我将感谢任何建议,想法,帮助。 谢谢。

0 个答案:

没有答案