"A command is already in progress" when 2 quick requests

I followed the documentation example to extend my autoquery endpoint to change the descending order to descending nulls last like so:

public class QueryServices : Service
{
    public IAutoQueryDb AutoQuery { get; set; }

    public object Any(QueryDomains dto)
    {
        var normalDescendingOrder = new string[]
        {
            "domainName",
        };
        var q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());

        if (!normalDescendingOrder.Contains(dto.OrderByDesc))
        {
            q.OrderByExpression = q.OrderByExpression.Replace("DESC", "DESC NULLS LAST");
        }
        
        return AutoQuery.Execute(dto, q);
    }
}

(btw there is small typo in example code in docs. It passes “request” when it should pass “dto”).

This works fine but if there are 2 requests within a short space of each other then AutoQuery.Execute(dto, q) throws this or similar exception:

A command is already in progress: SELECT COUNT(*) "COUNT(*)" 
FROM "domain_search_view"
WHERE "domain_search_view"."tf" > :0

I found thread that said it is because the IDbConnection is not threadsafe but how do I create separate connection so I can handle more than one request at a time? IAutoQueryDb is being injected so I am not sure exactly how this is supposed to be done. I intend the code to be used by multiple people at the same time so it will have to be able to handle this.

What is best way to fix this?

This is how I register IDbConnectonFactory in startup:

container.Register<IDbConnectionFactory>(c =>
	new OrmLiteConnectionFactory(
		AppSettings.Get<string>("development:pgConString"),
		PostgreSqlDialect.Provider));

Edit:

I also get this exception:

Npgsql.NpgsqlException: ‘Exception while reading from stream’

IOException: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.

SocketException: A blocking operation was interrupted by a call to WSACancelBlockingCall

I can re-create it when doing a query that takes long time to complete

AutoQuery uses a new DB connection per request so I don’t see how this can be a Thread Safety issue.

The 2nd Exception suggests an issue with the underlying Npgsql ADO.NET provider. Given connection pooling can recycle broken connections this might be the cause of the issue.

Can you try disable connection pooling (adding ‘;Pooling=False’ to the connection string) and see if it resolves the issue?

Otherwise I’d need a stand alone repro to be able to investigate.

Hello,
I’m having exactly the same issue with the same exceptions and the same code.
I have tried to disable pooling and the issue seems resolved.
Is there better workaround then disabling pooling?

Not if it’s an issue with the underlying Npgsql provider, are you using Postgres? Is the Exception StackTrace always the same? I.e. thrown in the same place? What’s the full Exception StackTrace?

If this issue was limited to AutoQuery Services it should be resolved from the latest v5.8.1 on MyGet which has had its db connection handling refactored.