SetCommandTimeout?

I have been having a problem with a query against a SQL Server database timing out after 30 seconds. To address this I am now setting the Command timeout. However, I am still receiving a timeout exception after 30 seconds.

Could I be missing something?

   private async Task<List<T>> GetRecordsForInsertAsync(CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
   
            using (var destination = connectionFactory.CreateDestinationConnection())
            {
                destination.SetCommandTimeout(ThreeMinutes);

                var sql = //query
                try
                {
                    return await destination.SelectAsync<T>(sql, token: token).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                   //handle error
                }
            }
        }

CreateDestinationConnection() basically calls OrmLiteConnectionFactory.OpenDbConnection()

I think you’ll need to use OrmLiteConfig.CommandTimeout = ThreeMinutes.

As SelectAsync will end up overriding the CommandTimeout you set using SetCommandTimeout.

1 Like

Is the code that you have linked to executed when I call IDbConnection.SelectAsync<T>()?

dbCmd.CommandTimeout = ormLiteConn != null 
                ? (ormLiteConn.CommandTimeout ??   OrmLiteConfig.CommandTimeout) 
                : OrmLiteConfig.CommandTimeout

If this code is executed when SelectAsync is called and since I call SetCommandTimeout() on the connection then it seems that CommandTimeout that I have just set should be used and not the OrmLiteConfig default?

OK - I’ve cloned the repo and written a test and, by stepping through the code, can see that even though I am calling SetCommandTimeout(180) the actual command timeout is being taken from OrmLiteConfig.CommandTimeout i.e. 30 seconds.

Can I ask in what cases does SetCommandTimeout have an effect?

[Test]
public async Task Can_Set_CommandTimeout()
{
   using (var db = OpenDbConnection())
   {
       db.DropAndCreateTable<Poco>();
       db.SetCommandTimeout(180);

       await db.SelectAsync<Poco>().ConfigureAwait(false);

       Assert.AreEqual(((OrmLiteConnection)db).CommandTimeout, 180);
   }
}

@mythz can the assignments to CommandTimeout in the dbCmd extensions be removed? The timeout is already set in the exec filter - which only sets it to the default if it doesn’t have a value.

Yep thanks for the pointer, doesn’t need to reset in ExecReaderAsync so I’ve removed it from this commit.