dimanche 10 mai 2015

NHibernate session Is not getting Closed

I have an MVC application with the following libraries installed.

  • Autofac
  • Nhibernate

I had a bug where the session is not getting closed after the request is done. I am having multiple sessions in the SQL database, however none of them are getting closed.

public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder) {
    var config = SessionFactory.ConofigCoreDb();
    var factory = SessionFactory.BuildCloudSQLSessionFactory(config);
    builder.RegisterInstance(config).As<Configuration>().SingleInstance();
    builder.RegisterInstance(factory).As<ISessionFactory>().SingleInstance();
    var session = builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerLifetimeScope();
    //sql services
    builder.RegisterType<ConsumerManager>().As<IConsumerManager>();
    builder.RegisterType<DefaultLogger>().As<ILogger>();
    builder.RegisterType<SettingsService>().As<ISettingService>();
    // and so on...
}

The problem that am having is, with each call to any of the services the sql session is being generated and I can call the database however it is not getting disposed properlym, although the BaseRepository class has a disposable method which flushs the sessions and properly closes them.

How to get autofac to properly close the session safely?

Aucun commentaire:

Enregistrer un commentaire