简体   繁体   中英

Too Many Clients Already in C# and PostgreSQL

I have error "Too Many Clients Already" in C# and PostgreSQL Am I did something wrong?

this is my main code

public class NationalService
{
    private NpgsqlConnection conn;
    protected string query;
    public NationalService()
    {
        this.conn = ConnectionService.GetConnection();
    }

    public string GetNamaWilayah(string kodePropinsi, string kodeKabupaten)
    {
        this.query = "select namakabupaten from dim_gab_wilayah where kodepropinsi='" + kodePropinsi + "' and kodekabupaten='" + kodeKabupaten + "' group by namakabupaten";
        string namaKabupaten;
        using (NpgsqlCommand command = new NpgsqlCommand(this.query, this.conn))
        {
            using (NpgsqlDataReader dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    namaKabupaten = dr["namakabupaten"].ToString();
                    return namaKabupaten;
                }
            }
        }
        return string.Empty;         
    }

}

and this is how I get connection

public class ConnectionService
{
    public static NpgsqlConnection GetConnection()
    {
        string connStr = WebConfigurationManager.ConnectionStrings["local"].ConnectionString;
        NpgsqlConnection conn = new NpgsqlConnection(connStr);            
        conn.Open();
        return conn;
    }
}

I didn`t write something like conn.close(); but I was wrote conn.Open in using.

For temporary I have changed max_connections = 100 to 1000 in postgresql.conf

can you give me hint?

Thanks in Advance

You are creating a new connection to the server when ever a new instance of the class NationalService is created, but those connections are never closed. You need to add a clean method to the class which will be called just before the instance is destroyed, in this method you have to release the connection by calling the close() method.

Too Many Clients Already" exception comes where a server is asked to create more connections than it is configured to maintain.

I'm not a C# developer, but a java developer.

In the same way check whether the dataReader you are using also need to be closed.

I'll strongly suggest you to use a connection pooling library in a production deployment instead of creating and maintaining connections yourself.

You never close your connection. Thats the problem. Each request opens new connection. Thats why you are getting "Too Many Clients Already" error.

I didn`t write something like conn.close(); but I was wrote conn.Open in using.

Conn.open is nothing to do with using. If you are creating your connection inside using, then it might helpful. Even it only calls dispose().

So better close your connection after the operation, thats the recommenced way.

Rewrite your code so that the Using declares the connection instead of the command. This way it will be disposed right after exiting the using block.

I don't know anything about C#, but you must definitively close the connection after using it. Can you verify that calling your function for the 100th (1000th) time produces the 'too many connections' error? I'd recommend you use the 'lease pattern' here. Generally this helps you cleaning up resources after their use. Spring.NET might offer just what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM