- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Parallel.ForEach
中使用 Npgsqlconnection
,循环遍历列表中的内联查询。
当我达到 1400+ 左右的数字时,我得到一个异常提示
'FATAL: 53300: remaining connection slots are reserved for non-replication superuser connections'.
我正在使用
Pooling=true;MinPoolSize=1;MaxPoolSize=1024;ConnectionLifeTime=1
在我的 app.config
和 con.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();
}
});
最佳答案
您正在达到 postgresql 本身的最大连接限制:
http://www.postgresql.org/docs/9.4/static/runtime-config-connection.html#GUC-MAX-CONNECTIONS
您的并行查询正在获得大量连接,而服务器无法处理。默认情况下,Postgresql 配置为允许 100 个并发连接。也许你应该尝试在你的 postgresql.conf 文件中增加这个值。
另一种选择是将 Npgsql 的池大小限制为较低的数字。当达到最大池大小时,您的并发查询将等待。
另外,不要调用 ClearPool,因为这会增加池逻辑的开销并且根本不会从池中受益。您可以尝试在连接字符串中设置 Pool=false
。
希望对你有帮助。
关于c# - Parallel.ForEach 使用 NpgsqlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836847/
这段代码线程安全吗? public static NpgsqlConnection getConnection() { NpgsqlConnectionStringBuild
我有一个 PostgreSQL 数据库,它使用字符编码 WIN1252。 在查询数据库时,有些记录在尝试读取数据时会产生错误,因为它正在尝试将其转换为UTF8。这发生在一些包含某些非拉丁字符的外国名字
我在 Parallel.ForEach 中使用 Npgsqlconnection,循环遍历列表中的内联查询。 当我达到 1400+ 左右的数字时,我得到一个异常提示 'FATAL: 53300: re
这是我在这里的第一个问题。 所以我有一个使用 Npgsql 库连接到远程数据库的应用程序。 我有一个连接到数据库的方法,执行查询,最后关闭连接。可以正常使用,但问题是,如果在程序运行但没有调用方法时,
最近,我在与托管在 CentOS 8 上的 PostgreSQL 13 数据库通信时偶然发现了某些问题。我无法使用 NpgsqlConnection 类中的方法 OpenAsync() 打开连接。同步
我正在使用 Npgsql 连接到我的 PostgreSQL 数据库。当我使用这样的本地地址时: Login = "Server = 10.0.0.5; Port = 5432; User Id = p
前几天,我遇到了一些奇怪的事情,我写了下面的程序,起草了一个用 F# 制作的 SQL Wrapper 的部分: open System open System.Data open System.Tra
.NET 5.6.1 Tasks Parallel Library - 基于应用程序在 NpSql.dll 3.1.6 调用 NpgsqlConnection.Open() 时停止响应。 经过进一步调
我是一名优秀的程序员,十分优秀!