I'm using Fluent NHibernate and using a connection provider in my configuration.
return Fluently.Configure()
.Database(
PostgreSQLConfiguration.PostgreSQL82.Provider<TenantConnectionProvider>()
)
.Mappings(cfg =>
{
cfg.FluentMappings.AddFromAssemblyOf<NHibernateSessionSource>();
})
.ExposeConfiguration(
c => {
c.DataBaseIntegration(prop =>
{
prop.BatchSize = 20;
});
c.SetProperty(NHibernate.Cfg.Environment.SessionFactoryName, name);
c.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true");
})
.Diagnostics(x => x.Enable(true))
.BuildConfiguration();
public class TenantConnectionProvider : DriverConnectionProvider
{
public override async Task<DbConnection> GetConnectionAsync(CancellationToken cancellation = default)
{
// open connection async
}
public override DbConnection GetConnection()
{
// open connection sync
}
}
I'm facing the following issues.
1. NHibernate doesn't open the connection async using GetConnectionAsync() instead the connection is always open sync using the method GetConnection()
2. For each query that is executed a new connection is being opened. Can we use a single db connection per session?