C# 和 PostgreSQL 中已有太多客户端
我在 C# 和 PostgreSQL 中遇到错误“Too Many Clients Already” 难道我做错了什么?
这是我的主要代码
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;
}
}
这就是我获得连接的方式
public class ConnectionService
{
public static NpgsqlConnection GetConnection()
{
string connStr = WebConfigurationManager.ConnectionStrings["local"].ConnectionString;
NpgsqlConnection conn = new NpgsqlConnection(connStr);
conn.Open();
return conn;
}
}
我没有写类似 conn.close(); 的东西 但我在使用中写了 conn.Open 。
暂时我已经将 postgresql.conf 中的 max_connections = 100 更改为 1000
你能给我提示吗?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当
NationalService
类的新实例创建时,您就创建了一个与服务器的新连接,但这些连接永远不会关闭。您需要向类添加一个 clean 方法,该方法将在实例被销毁之前调用,在该方法中您必须通过调用 close() 方法来释放连接。当服务器被要求创建比配置维持的连接更多的连接时,就会出现“太多客户端已经”的异常。
我不是 C# 开发人员,而是 Java 开发人员。
以同样的方式检查您正在使用的 dataReader 是否也需要 。
我强烈建议您在生产部署中使用连接池库,而不是自己创建和维护连接
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.
重写代码,以便使用声明连接而不是命令。
这样,它将在退出 using 块后立即被处理。
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.
你永远不会关闭你的连接。这就是问题所在。每个请求都会打开新的连接。这就是为什么您收到“太多客户端已经”错误的原因。
Conn.open 与使用无关。如果您正在使用内部创建连接,那么它可能会有所帮助。即使它只调用dispose()。
所以最好在操作后关闭连接,这就是重新开始的方式。
You never close your connection. Thats the problem. Each request opens new connection. Thats why you are getting "Too Many Clients Already" error.
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.
我对C#一无所知,但使用后必须明确关闭连接。您能否验证第 100(1000)次调用您的函数是否会产生“连接过多”错误?我建议您在这里使用“租赁模式”。一般来说,这可以帮助您在使用资源后清理资源。 Spring.NET 可能正好提供您所需要的。
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.