We are in the process of moving away from entity framework to ormlite. In doing so we are currently running into a problem where we get the following exception.
Npgsql.NpgsqlOperationInProgressException: 'A command is already in progress
We are using postgres with ormlite. We have also implemented a repository pattern with the simple class as follows.
public class Repository<T> : IRepository<T> where T : class
{
private readonly IDbConnectionFactory dbConnectionFactory;
private readonly string connectionName =DatabaseSettings.ConnectionName;
public Repository(IDbConnectionFactory dbConnectionFactory)
{
this.dbConnectionFactory = dbConnectionFactory;
}
private IDbConnection db;
protected Repository(IDbConnectionFactory dbConnectionFactory, string connectionName):this(dbConnectionFactory)
{
this.connectionName = connectionName;
}
public IDbConnection Db => db ?? (db = dbConnectionFactory.OpenDbConnection(connectionName));
public T Get(long id)
{
return Db.LoadSingleById<T>(id);
}
public IDbTransaction StartTransaction()
{
return Db.OpenTransaction();
}
public List<T> Find(Expression<Func<T, bool>> predicate)
{
return Db.LoadSelect<T>(predicate);
}
public long Count(Expression<Func<T, bool>> predicate)
{
return Db.Count<T>(predicate);
}
public List<T> List(Expression<Func<T, bool>> predicate, int offset, int limit)
{
var q = Db.From<T>().Where(predicate).Limit(offset, limit);
return Db.LoadSelect(q);
}
public void Save(T model)
{
Db.Save<T>(model);
}
public int Delete(T model)
{
return Db.Delete<T>(model);
}
public void Delete(Expression<Func<T, bool>> predicate)
{
Db.Delete<T>(predicate);
}
public void DeleteByIds(List<long> ids)
{
Db.DeleteByIds<T>(ids);
}
public void Dispose()
{
db?.Dispose();
}
public void InsertAll(List<T> models)
{
Db.InsertAll(models);
}
}
The DbConnection factory is registered as a singleton in Autofac.
We have a custom requestLogger that is a carbon copy of one I found in servicestack code, but we are using our own Entity and service that calls the underlying repository.
Plugins.Add(new RequestLogsFeature
{
RequestLogger = new PostgresRequestLogger(container.Resolve<IRequestLoggerService>()),}
Once deployed, it works fine for a while, then starts throwing the exception listed above.
Perhaps we have a misunderstanding of how connections are handled. Should all calls (as seen in the Repository class above) actually be wrapped in usings that directly open and close the db? I would think there’s a big performance hit there unless the guts are using some sort of connection pool internally?