简体   繁体   English

C#和PostgreSQL中已有太多客户端

[英]Too Many Clients Already in C# and PostgreSQL

I have error "Too Many Clients Already" in C# and PostgreSQL Am I did something wrong? 我在C#和PostgreSQL中出现错误“已经有太多客户端”我做错了吗?

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(); 我没有写类似conn.close()的东西; but I was wrote conn.Open in using. 但是我被写成conn.Open在使用中。

For temporary I have changed max_connections = 100 to 1000 in postgresql.conf 对于临时我在postgresql.conf中将max_connections = 100更改为1000

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. 每当创建类NationalService的新实例时,您都在与服务器建立新连接,但是这些连接永远不会关闭。 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. 您需要在将要销毁实例之前在类中添加一个clean方法,在该方法中,您必须通过调用close()方法来释放连接。

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. 我不是C#开发人员,而是Java开发人员。

In the same way check whether the dataReader you are using also need to be closed. 以同样的方式检查您正在使用的dataReader是否也需要关闭。

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(); 我没有写类似conn.close()的东西; but I was wrote conn.Open in using. 但是我被写成conn.Open在使用中。

Conn.open is nothing to do with using. Conn.open与使用无关。 If you are creating your connection inside using, then it might helpful. 如果您正在使用中创建连接,则可能会有所帮助。 Even it only calls dispose(). 即使它只调用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. 这样,它将在退出using块后立即进行处理。

I don't know anything about C#, but you must definitively close the connection after using it. 我对C#一无所知,但是您必须在使用它之后一定要关闭该连接。 Can you verify that calling your function for the 100th (1000th) time produces the 'too many connections' error? 您是否可以验证在第100(1000)次调用函数会产生“连接过多”错误? 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. Spring.NET可能会满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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