簡體   English   中英

PostgreSQL:負載測試拋出錯誤 - Npgsql.PostgresException:“53300:抱歉,已經有太多客戶端”

[英]PostgreSQL: Load testing throws error - Npgsql.PostgresException: '53300: sorry, too many clients already'

我有一個簡單的代碼試圖模擬我的 Postgres 數據庫的負載測試

            List<Values> values = new List<Values>();

            for (int i = 0; i < 200; i++)
            {
                var mythread = new Thread(() =>
                {
                    DatabaseConnection db = new DatabaseConnection();
                    using (var conn = db.GetDatabaseConnection())
                    {
                        var cmd = new NpgsqlCommand();
                        cmd.Connection = conn;
                        cmd.CommandText = "SELECT val.id, val.name FROM val.myvalue val";
                        cmd.Prepare();
                        var reader = cmd.ExecuteReader();
                        try
                        {
                            while (reader.Read())
                            {
                                Values value = new Values();
                                value.Id = reader.GetInt64(0);
                                value.Name = reader.GetString(1);

                                values.Add(value);
                            }
                            reader.Close();
                        }
                        catch (Exception ex)
                        {
                            reader.Close();
                            var exception = ex;
                            throw exception;
                        }
                    }
                });

                mythread.Start();
            }

但是當我運行它時,我收到錯誤Npgsql.PostgresException: '53300: 抱歉,已經有太多客戶端'

數據庫連接 class

public class DatabaseConnection
{
    public NpgsqlConnection GetDatabaseConnection()
    {
        var connectionstring = "User ID=myuser;Password=mypassword;Server=localhost;Port=5432;Database=myvaluesdb;Pooling=True;Minimum Pool Size=10;Maximum Pool Size=100;Trust Server Certificate=true;Connection Idle Lifetime=300;keepalive=10;Timeout=60";
        NpgsqlConnection connection = null;

        try
        {
            connection = new NpgsqlConnection(connectionstring);
            connection.Open();
        }
        catch (Exception)
        {
            // try to reconnect
            connection = new NpgsqlConnection(connectionstring);
            connection.Open();
        }

        if (connection.State == ConnectionState.Open)
            Console.WriteLine("connection status = OPEN");
        else
        {
            var exception = new Exception("could not get DB connection");
            throw exception;
        }

        return connection;
    }
}

檢查 max_connections 顯示 100
所以我的問題是這是正常的還是有什么地方做錯了需要糾正如果是這樣我該如何糾正
謝謝

如果您執行的查詢很短(即完成時間少於 60 秒),您應該不會看到此行為。

當達到最大連接數(最大池大小)並且您要求更多連接時,npgsql 將等待timeout (在您的情況下為 60 秒,默認為 15 秒)返回一個。 它不會建立更多,但它會等待另一個連接停止使用並返回到池中。

在您的示例中,您同時啟動了 200 個線程。 前 100 個應該沒有問題,然后接下來的 100 個應該等待前 100 個將連接返回到池中,然后可以建立/使用他們的連接。

因此,要么您正在運行的查詢非常長(超過 60 秒),要么您在完成后未能關閉連接。 我沒有看到任何close語句,所以你依賴於連接dispose ,但這是自定義代碼......你的自定義dispose()必須明確關閉(或處置)底層連接。

暫無
暫無

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

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