OracleCommand超时

时间:2012-09-30 10:48:39

标签: .net oracle odp.net oraclecommand

ODP.NET documentation for OracleCommand.CommandTimeout

  

默认值为0秒,不会强制执行时间限制。

     

当指定的超时值在命令执行之前到期时   完成后,命令尝试取消。如果取消是   成功后,ORA-01013:user的消息抛出异常   要求取消当前的操作。如果命令及时执行   没有任何错误,不会抛出任何异常。

     

在多个OracleCommand对象使用相同的情况下   连接,其中一个OracleCommand对象的超时到期   可以终止单个连接上的任何执行。要做   OracleCommand的超时到期仅取消其自己的命令   执行,如果是这样,只需为每个连接使用一个OracleCommand   OracleCommand将CommandTimeout属性设置为大于的值   0

但像这样的代码有效:

static void Main(string[] args)
{
    OracleConnection conn = null;
    try
    {
        string connString =
            "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myOracleHost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=myServiceName)));User Id=system;Password=admin;Pooling = False;";
        string cmdString1 = "UPDATE employee SET empname = 'temp1' where id = 1";
        string cmdString2 = "Update employee set empname = 'temp2' where id = 2";
        conn = new OracleConnection(connString);
        var cmd1 = new OracleCommand(cmdString1, conn);
        cmd1.CommandTimeout = 30;
        var cmd2 = new OracleCommand(cmdString2, conn);
        cmd2.CommandTimeout = 30;
        conn.Open();
        try
        {
            //Locked the row with ID 1 with an uncommitted update operation outside this code
            cmd1.ExecuteNonQuery();
        }
        catch (Exception exception)
        {
            //Exception ORA-01013 Thrown as expected
        }
        try
        {
            //As per the documentation, this should not also work since this command object also uses the same connection as above and it timed out in the query above
            cmd2.ExecuteNonQuery();
            //But this still works fine. 
        }
        catch (Exception)
        {
            //no exception
        }
    }
    finally
    {
        conn.Close();
    }
}

我对命令对象使用了相同的OracleConnection对象 - cmd1cmd2cmd1已经超时(正如预期的那样)。 但是,根据文档,cmd2也不应该运行。但它仍然没有任何异常地运行并正确地更新另一行。

1 个答案:

答案 0 :(得分:2)

您没有在连接上运行多个命令,您有两个顺序运行的命令,一个接一个地运行。当第一个命令超时时,连接上没有其他待命命令。在第一个命令成功或抛出异常之后,您的代码才会提交第二个执行命令。

您引用的文档中的最后一段应为:在多个OracleCommand对象使用相同连接同时,...

的情况下
static void Main(string[] args)
{
    using (var conn = new OracleConnection("Pooling=False;...")) // why?
    using (var cmd1 = conn.CreateCommand())
    using (var cmd2 = conn.CreateCommand())
    {
        cmd1.CommandText = "UPDATE employee SET empname = 'temp1' WHERE id = 1";
        cmd2.CommandText = "UPDATE employee SET empname = 'temp2' WHERE id = 2";
        cmd1.CommandTimeout = 30;
        cmd2.CommandTimeout = 30;

        conn.Open();

        // there are no commands on conn yet

        try { cmd1.ExecuteNonQuery(); } // cmd1 is the only command on conn
        catch (OracleException) { } // if timeout, no other command affected

        // cmd1 is no longer on conn

        try { cmd2.ExecuteNonQuery(); } // cmd2 is the only command on conn
        catch (OracleException) { } // if timeout, no other command affected

        // cmd2 is no longer on conn
    }
}
相关问题