簡體   English   中英

在調用SQL CLR過程時,使應用程序池大小達到最大獲取錯誤

[英]While calling SQL CLR procedure getting application pool size reached to Max getting error

在運行asp.net應用程序時,該應用程序調用sql服務器中托管的存儲過程,而sql clr返回以下錯誤:

An error occurred: A .NET Framework error occurred during execution of user-defined routine or aggregate "Procedure Name": 
 System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
System.InvalidOperationException: 
 at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at StoredProcedures.ProcedureName(String sourceConnectionString, String destinationConnectionString, String sql, String destinationTable, String columns)”

我們正在使用asp.net樣板和實體框架。 在SQL CLR中,我們使用了ado.net並通過使用“使用”關鍵字來關閉連接。 因此,我認為連接關閉問題不應該存在。

如在存儲過程中一樣,建議通過傳遞連接字符串將數據從一個源獲取到另一個源。

您遇到的問題通常是在與數據庫建立了多個連接時發生的,但從未在使用后正確關閉/處置這些連接。

•確保使用后關閉所有連接。 •您還可以在連接/過程類上繼承IDisposable。 實現dispose方法以在使用后清除實例化的連接。

請參見以下示例:

public partial class Connection : IDisposable
{

    /// <summary>
    /// Gets or sets the connection string.
    /// </summary>
    protected static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        }
    }

    /// <summary>
    /// Default Constructor for the Database Connection Class.
    /// </summary>
    public Connection()
    {
        this.sqlConnection = new SqlConnection(Connection.ConnectionString);
        this.sqlConnection.Open();
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    public void Dispose()
    {
        if (sqlConnection != null)
        {
            if (sqlConnection.State != System.Data.ConnectionState.Closed)
                sqlConnection.Close();

            sqlConnection.Dispose();
            sqlConnection = null;
        }
    }
}

通常,每次調用遠程實例(在SQLCLR存儲過程中完成)需要花費多少時間? 而且,通常該存儲過程的調用頻率為多少? 如果您確實確實確實在using構造中正確實例化了SqlConnection (例如, using (SqlConnection _Connection = new SqlConnection(UserConnectionString)) { ... } ),那么有任何理由假定錯誤消息“”, 這可能是因為所有池化連接都在使用中,並且達到了最大池大小。 ”是否具有誤導性,或者由於某些網絡問題導致實際超時?

請記住,默認連接池大小為100個連接。 如果此代碼需要一秒鍾(或幾秒鍾)才能完成,並且如果頻繁調用此代碼,那么很可能您確實耗盡了池中的連接。 您至少可以通過在連接字符串中添加以下內容來增加池的大小來檢驗這一理論:

Max Pool Size = 200;

它不必為200,但是如果您嘗試這樣做卻再也沒有收到錯誤,那么您確實用完了默認的100個連接。 如果仍然出現該錯誤,則可能是發生了其他情況,例如網絡延遲或SQL Server用盡了可用的連接,並且一開始不允許這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM