从流读取时出现Npgsql异常,Postgres
本文关键字:Npgsql 异常 Postgres 读取 | 更新日期: 2023-09-27 18:16:00
我在代码中间歇性地收到这个错误。有时它会一次又一次地发生。有时这种情况十分之一。与StackOverflow上执行COPY命令的其他海报不同,我在SQL中没有做任何独特或特别的事情。我所做的只是选择。
这是堆栈跟踪:
Exception while reading from stream
at Npgsql.ReadBuffer.Ensure(Int32 count, Boolean dontBreakOnTimeouts)
at Npgsql.NpgsqlConnector.DoReadMessage(DataRowLoadingMode dataRowLoadingMode, Boolean isPrependedMessage)
at Npgsql.NpgsqlConnector.ReadMessageWithPrepended(DataRowLoadingMode dataRow LoadingMode)
at Npgsql.NpgsqlConnector.ReadMessage(DataRowLoadingMode dataRowLoadingMode)
at Npgsql.NpgsqlConnector.ReadExpecting[T]()
at Npgsql.NpgsqlDataReader.NextResultInternal()
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Npgsql.NpgsqlCommand.ExecuteReader()
at JBetaFinder.Program.portfolioSimulation(String beginResult, String endResult) in c:'Users'j'Documents'Visual Studio 2013'Projects'JBetaFinder'JBetaFinder'Program.cs:line 571
关于如何避免这个错误,有什么建议吗?这是Npgsql和postgres的问题吗?
以下是我的SQL语句,它似乎是最有问题的:
select leg1.trade_date, sum(p.qty) as totalqty, max(gas.net_change)*10000 as avggaschange,
sum(((leg1.settlement_price - leg2.settlement_price) - (leg3.settlement_price - leg4.settlement_price))*qty*1000000) as spread_value_weight
from quant_portfolio p
inner join (select distinct trade_date, hub, product, strip, settlement_price, net_change
from public.icecleared_gas where contract = 'H') leg1
on p.leg1 = leg1.strip
inner join (select distinct trade_date, hub, product, strip, settlement_price, net_change
from public.icecleared_gas where contract = 'H') leg2
on p.leg2 = leg2.strip and leg1.trade_date = leg2.trade_date
inner join (select distinct trade_date, hub, product, strip, settlement_price, net_change
from public.icecleared_gas where contract = 'H') leg3
on p.leg3 = leg3.strip and leg1.trade_date = leg3.trade_date
inner join (select distinct trade_date, hub, product, strip, settlement_price, net_change
from public.icecleared_gas where contract = 'H') leg4
on p.leg4 = leg4.strip and leg1.trade_date = leg4.trade_date
inner join (select distinct trade_date, hub, product, strip, contract, settlement_price, net_change
from public.icecleared_gas where contract = 'H') gas
on gas.strip = (select min(leg1) from quant_portfolio where commodity = 'NG') and gas.trade_date = leg1.trade_date
where p.commodity = 'NG'
AND (leg1.trade_date>='xxx' and leg1.trade_date<='yyy')
group by leg1.trade_date
order by leg1.trade_date
我尝试重新安排SQL,去掉子SELECTS并使它们全部联接;没用,同样的错误。
以下是调用Npgsql:的C#代码
query = new NpgsqlCommand(getFullQuantPortBeta.ToString().Replace("xxx", beginResult.ToString()).Replace("yyy", endResult.ToString()), conn);
dr = query.ExecuteReader();//code does not get past this line!
beta = 0;
while (dr.Read())
{
baselineData.Add(double.Parse(dr[2].ToString()));
responseData.Add(double.Parse(dr[3].ToString()));
if (baselineData.Count > 3)
{
Tuple<double, double> result = MathNet.Numerics.LinearRegression.SimpleRegression.Fit(baselineData.ToArray(), responseData.ToArray());
beta = result.Item2 * BETA_MULT;
Console.WriteLine("WEIGHT BETA = " + beta);
}
}
dr.Close();
conn = new NpgsqlConnection("Server=myserver;
User Id=postgres;
Password=somepw;
Database=somedb;
Pooling=false;
Timeout=300;
CommandTimeout=300");
我将CommandTimeout
属性添加到我的连接字符串中,现在它似乎正在工作。超时错误出现异常。。。
请尝试设置
KeepAlive = 300
Npgsql发送保活查询之前连接处于非活动状态的秒数。设置为0(默认值(可禁用。
请参阅文档。
在调用方法之前检查是否忘记使用await
。就我而言,是
advisorRepository.UpdateAsync(entity, cancellationToken);
无await