views:

1022

answers:

1

problem description: This model works fine with one user at a time. As soon as I get multiple users at once, I get a serious of errors relating to not closing my SqlDataReader. When i turn off lazy loading like this:

persistenceModel.Conventions.OneToManyConvention = (prop => prop.SetAttribute("lazy", "false"));

It's fine, yet performance is slow. This uses MVC Beta 1

Any thoughts?

Below I have a snippet of my global ASAX as well as my SessionFactory inialization code.

******* THIS IS IN MY GLOBAL.ASAX ****

public class MvcApplication : NinjectHttpApplication
{
    public static IKernel Kernel { get; set; }

    protected override void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.IgnoreRoute("WebServices/*.asmx");

        routes.MapRoute("CreateCategoryJson", "Admin/CreateCategoryJson/{categoryName}");
        routes.MapRoute("User", "Admin/User/{username}", new { controller="Admin", action="user" });

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Session_Start(object sender, EventArgs e)
    {
        if (Session["rSkillsContext"] == null)
        {
            string logonName = this.User.Identity.Name.Replace("NUSOFTCORP\\", string.Empty);
            rSkillsContext context = new rSkillsContext(logonName);
            Session.Add("rSkillsContext", context);
        }
    }

    protected override IKernel CreateKernel()
    {
        log4net.Config.XmlConfigurator.Configure();
        Kernel = new StandardKernel(new RepositoryModule(), new AutoControllerModule(Assembly.GetExecutingAssembly()), new Log4netModule());
        return Kernel;
    }
}

* This is my NHibernateHelper.cs **

    private ISessionFactory CreateSessionFactory()
    {
        var configuration = MsSqlConfiguration
                                .MsSql2005
                                .ConnectionString.FromConnectionStringWithKey("ConnectionString")
                                .ShowSql()
                                .Raw("current_session_context_class", "web")
                                .ConfigureProperties(new Configuration());

        var persistenceModel = new PersistenceModel();

        persistenceModel.Conventions.GetForeignKeyName = (prop => prop.Name + "ID");
        persistenceModel.Conventions.GetForeignKeyNameOfParent = (prop => prop.Name + "ID");
        // HACK: changed lazy loading
        persistenceModel.Conventions.OneToManyConvention = (prop => prop.SetAttribute("lazy", "false"));

        persistenceModel.addMappingsFromAssembly(Assembly.Load(Assembly.GetExecutingAssembly().FullName));
        persistenceModel.Configure(configuration);

        return configuration.BuildSessionFactory();
    }
+1  A: 

It looks like that you didn't dispose your Session correctly (I had the same error with Ninject and NHibernate a month ago). It should be started at the start of request and should be disposed at the end. Could you please provide pieces of code where you starting and disposing your nhibernate session?

zihotki