【问题标题】:Catch Oracle timeout exceptions捕获 Oracle 超时异常
【发布时间】:2012-02-08 10:47:59
【问题描述】:

我想以不同于其他 SQL 异常的方式处理超时异常,就像 How to catch SQLServer timeout exceptions

但是,我们的应用程序同时支持 Oracle 和 MSSqlserver。

理想情况下,该解决方案将涵盖两个提供程序:System.Data.OracleClient 和 Oracle.DataAccess.Client。

这些异常抛出的错误代码是什么?

【问题讨论】:

    标签: .net database oracle timeout


    【解决方案1】:

    使用 void setQueryTimeout(int seconds) throws SQLException; 可能有效;但它可能会导致使用Oracle jdbc驱动程序执行的每个sql都创建线程,因此请谨慎使用。

    /**
     * Sets the number of seconds the driver will wait for a 
     * <code>Statement</code> object to execute to the given number of seconds.
     * If the limit is exceeded, an <code>SQLException</code> is thrown. A JDBC
     * driver must apply this limit to the <code>execute</code>,
     * <code>executeQuery</code> and <code>executeUpdate</code> methods. JDBC driver
     * implementations may also apply this limit to <code>ResultSet</code> methods
     * (consult your driver vendor documentation for details).
     *
     * @param seconds the new query timeout limit in seconds; zero means 
     *        there is no limit
     * @exception SQLException if a database access error occurs, 
     * this method is called on a closed <code>Statement</code>
     *            or the condition seconds >= 0 is not satisfied
     * @see #getQueryTimeout
     */
    void setQueryTimeout(int seconds) throws SQLException;
    

    【讨论】:

    • 我不是Java,设置超时不是问题。问题在于对超时做出反应。
    • 非常抱歉,我的错,我没有注意到 .Net 部分
    【解决方案2】:

    我最终得到的结果与此类似:

    但是,TimeOut 的错误代码似乎是 01013

        /// <summary>
        ///     Catches network problems for oracle connections and clears the session pool of invalid connections
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="triedBefore"></param>
        /// <returns></returns>
        private bool reconnectOracle(DbException ex)
        {
            var exType = ex.GetType();
            if (exType.FullName == "Oracle.DataAccess.Client.OracleException")
            {
                dynamic exOra = ex;
                int errorNo = exOra.Number;
                // Check for oracle network error numbers
                if (errorNo == 03113 ||
                    errorNo == 03114 || 
                    errorNo == 03135) // **Timeout seems to be 01013**
                {
                    AL.Warn("Connection is broken. Error number: {0}. Attempting reconnect.", errorNo);
                    // Network error, close connection
                    Command.Connection.Close();
    
                    // All connections in the pool are probably invalid. Ensure that all connections are cleared
                    dynamic con = new StaticMembersDynamicWrapper(Command.Connection.GetType());
                    con.ClearAllPools();
    
                    // Ensure that new connection object is given
                    Command.Connection = null;
    
                    return true;
                }
            }
            return false;
        }
    

    StaticMembersDynamicWrapper 解释如下:http://blogs.msdn.com/b/davidebb/archive/2009/10/23/using-c-dynamic-to-call-static-members.aspx

    我不想硬引用 Oracle 程序集,所以我改用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2011-11-02
      • 1970-01-01
      • 2018-04-17
      相关资源
      最近更新 更多