【问题标题】:Parallel.ForEach using NpgsqlConnectionParallel.ForEach 使用 NpgsqlConnection
【发布时间】:2015-06-15 03:12:35
【问题描述】:

我在Parallel.ForEach 中使用Npgsqlconnection,循环遍历列表中的内联查询。

当我达到 1400+ 左右的数字时,我得到一个异常提示

'致命:53300:剩余的连接槽保留用于非复制超级用户连接'。

我正在使用

 Pooling=true;MinPoolSize=1;MaxPoolSize=1024;ConnectionLifeTime=1 

在我的 app.configcon.Close()con.ClearPool()con.Dispose() 在我的代码中。

Parallel.ForEach(查询,查询 => { 使用 (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();
                    }                    
                });

【问题讨论】:

  • 您能发布导致问题的代码吗?没有它,就很难为您提供帮助。

标签: c# postgresql npgsql parallel.foreach


【解决方案1】:

您正在达到 postgresql 本身的最大连接限制:

http://www.postgresql.org/docs/9.4/static/runtime-config-connection.html#GUC-MAX-CONNECTIONS

您的并行查询获得了大量连接,而服务器无法处理它。默认情况下,Postgresql 配置为允许 100 个并发连接。也许你应该尝试在你的 postgresql.conf 文件中增加这个值。

另一个选择是将 Npgsql 的池大小限制为较小的数字。当达到最大池大小时,您的并发查询将等待。

另外,不要调用 ClearPool,因为这会增加池逻辑的开销,并且根本不会从池中受益。您可以尝试在连接字符串中设置Pool=false

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2014-08-19
    • 2012-11-18
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多