如何在ASP.NET MVC3应用程序中强制Npgsql使用连接字符串中的CommandTimeout值
ASP.NET MVC3应用程序使用npgsql 2.0.12.0通过mod_Mono在Mono中获取数据。 有时会出现以下超时异常。如果发生异常,请求持续时间为31秒 使用将连接字符串中的CommandTimeout设置为5分钟如何在ASP.NET MVC3应用程序中强制Npgsql使用连接字符串中的CommandTimeout值,asp.net,asp.net-mvc,asp.net-mvc-3,mono,npgsql,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Mono,Npgsql,ASP.NET MVC3应用程序使用npgsql 2.0.12.0通过mod_Mono在Mono中获取数据。 有时会出现以下超时异常。如果发生异常,请求持续时间为31秒 使用将连接字符串中的CommandTimeout设置为5分钟 NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder() { CommandTimeout = 5*60, // 5 min
NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder()
{
CommandTimeout = 5*60, // 5 min
Host = Config.Server,
Database = Config.DefaultDataBase,
UserName = Config.ServerUser,
Port = Config.Port,
SslMode = SslMode.Prefer,
SSL = true,
};
如何强制npgsql使用超时5分钟,并且在31秒后不抛出excpetion?
在postgresql.con文件语句中,没有设置超时,所以看起来这不是来自服务器
使用ExecuteReader执行查询:
using (IDbConnection conn = DataAccessBase.CreateConnection())
using (IDbCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(command, paramNames);
conn.Open();
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult))
{
if (reader.Read())
{
string[] names = new string[reader.FieldCount];
for (int i = 0; i < names.Length; i++)
{
names[i] = reader.GetName(i);
}
Func<IDataRecord, MyDataContext, T> objInit = InitializerCache<T>.GetInitializer(names, ConvertValue != null);
do
{ // walk the data
yield return objInit(reader, this);
} while (reader.Read());
}
while (reader.NextResult()) { } // ensure any trailing errors caught
}
}
例外情况:
Npgsql.NpgsqlException:
A timeout has occured. If you were establishing a connection, increase Timeout value in ConnectionString. If you were executing a command, increase the CommandTimeout value in ConnectionString or in your NpgsqlCommand object.
at Npgsql.NpgsqlState.ProcessBackendResponsesEnum (Npgsql.NpgsqlConnector context) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlReadyState.QueryEnum (Npgsql.NpgsqlConnector context, Npgsql.NpgsqlCommand command) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlConnector.QueryEnum (Npgsql.NpgsqlCommand queryCommand) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlCommand.GetReader (CommandBehavior cb) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlCommand.ExecuteReader (CommandBehavior cb) [0x00000] in <filename unknown>:0
抱歉,但我认为您的命令时间不正确: CommandTimeout=5*60,//5分钟
它是30秒300毫秒。npgsql源代码和文档包含等待命令执行的时间(以秒为单位),也包括单元测试TimeoutFirstParameters将值设置为1并调用pg_sleep1.5并验证是否发生超时异常。这是以秒为单位的。为什么你认为这是以毫秒为单位的?我错了,这是以秒为单位的时间。我已经在文档中澄清了。而且,值得一提的是,300毫秒不是30秒。0.3秒。一秒钟有1000毫秒。您可以尝试设置调用ExecuteReader的命令超时吗?如果有效,它将确认将commandtimeout从connectionstring向下传递到NpgsqlCommand对象时出现问题。提前谢谢。我想我有一个类似的问题:这是NpgSQL错误吗?
Npgsql.NpgsqlException:
A timeout has occured. If you were establishing a connection, increase Timeout value in ConnectionString. If you were executing a command, increase the CommandTimeout value in ConnectionString or in your NpgsqlCommand object.
at Npgsql.NpgsqlState.ProcessBackendResponsesEnum (Npgsql.NpgsqlConnector context) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlReadyState.QueryEnum (Npgsql.NpgsqlConnector context, Npgsql.NpgsqlCommand command) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlConnector.QueryEnum (Npgsql.NpgsqlCommand queryCommand) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlCommand.GetReader (CommandBehavior cb) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlCommand.ExecuteReader (CommandBehavior cb) [0x00000] in <filename unknown>:0