Jan Nutcha
Jan Nutcha

Reputation: 83

The command timeout does not throw

 private void NpgSqlGetContracts(IList<Contract> con)
    {
        var conn = (NpgsqlConnection)Database.GetDbConnection();

        List<Contract> contracts = new List<Contract>();

        using (var cmd = new NpgsqlCommand("SELECT * FROM \"Contracts\";", conn))
        {
            cmd.CommandTimeout = 1;

            cmd.Prepare();

            int conCount= cmd.ExecuteNonQuery();

            using (var reader = cmd.ExecuteReader(CommandBehavior.SingleResult))
            {
                while (reader.Read())
                {
                    contracts.Add(MapDataReaderRowToContract(reader));
                }
            }
        }
    }

Here I have this code to try the command timeout in postgres, I have try debug it locally with break point in visual studio. I try both ExecuteNonQuery and ExecuteReader The query took more than 1 second to load all data (I have above 3 millions rows here). But the command timeout is set to 1 second. I wonder why it does not throw any exception here, What did I configured wrong here?

Thank you :)

Upvotes: 1

Views: 1313

Answers (1)

Shay Rojansky
Shay Rojansky

Reputation: 16722

As @hans-kesting wrote above, the command timeout isn't cumulative for the entire command, but rather for each individual I/O-producing call (e.g. Read). In that sense, it's meant to help with queries running for too long (without producing any results), or network issues.

You may also want to take a look at PostgreSQL's statement_timeout, which is a PG-side timeout for the entire command. It too has its issues, and Npgsql never sets it implicitly for you - but you can set it yourself.

Upvotes: 2

Related Questions