我在Npgsqlconnection
中使用Parallel.ForEach
,循环遍历列表中的内联查询。
当我达到1400左右的数字时,我会得到一个例外
'致命:53300:剩余的连接插槽保留用于非复制超级用户连接'。
我正在使用
Pooling=true;MinPoolSize=1;MaxPoolSize=1024;ConnectionLifeTime=1
在我的代码中app.config
和con.Close()
,con.ClearPool()
,con.Dispose()
。
Parallel.ForEach(queries,query => { using(NpgsqlConnection con = new NpgsqlConnection(ConfigurationManager.ConnectionStrings [“PSQL”]。ConnectionString)) { con.ClearPool(); con.Open();
//int count = 0;
int queryCount = queries.Count;
using (NpgsqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.Text;
//cmd.CommandTimeout = 0;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
count += 1;
this.label1.Invoke(new MethodInvoker(delegate { this.label1.Text = String.Format("Processing...\n{0} of {1}.\n{2}% completed.", count, queryCount, Math.Round(Decimal.Divide(count, queryCount) * 100, 2)); }));
}
con.Close();
//con.Dispose();
//con.ClearPool();
}
});
答案 0 :(得分:0)
您正在达到postgresql本身的最大连接限制:
http://www.postgresql.org/docs/9.4/static/runtime-config-connection.html#GUC-MAX-CONNECTIONS
您的并行查询获得了大量连接,服务器无法处理它。默认情况下,Postgresql配置为允许100个并发连接。也许你应该尝试在postgresql.conf文件中增加这个值。
另一种选择是将Npgsql的池大小限制为较小的数字。当达到最大池大小时,您的并发查询将等待。
此外,不要调用ClearPool,因为您会向池逻辑添加开销,并且根本不会从池中受益。您可以尝试在连接字符串中设置Pool=false
。
我希望它有所帮助。