【问题标题】:Will my session be automatically closed?我的会话会自动关闭吗?
【发布时间】:2011-01-23 00:33:08
【问题描述】:

编辑

原始标题:我的交易在到达我的回购时已关闭。我做错了什么?

我的原始问题得到了答案(我忘记打开交易了,哈哈)。现在我想知道我的代码是否会自动关闭会话,或者我是否必须以某种方式告诉它这样做。


我正在使用 mvc 3.0、nhibernate、fluent nhibernate 和 ninject 2.0

全球.asax

// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        }

        protected void Application_Start()
        {
            // Hook our DI stuff when application starts
            SetupDependencyInjection();


            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }


        public void SetupDependencyInjection()
        {         
            // Tell ASP.NET MVC 3 to use our Ninject DI Container
            DependencyResolver.SetResolver(new NinjectDependencyResolver(CreateKernel()));
        }

        protected IKernel CreateKernel()
        {
            var modules = new INinjectModule[]
                              {
                                 new NhibernateModule(),
                                 new ServiceModule(),
                                 new RepoModule()
                              };

            return new StandardKernel(modules);
        }

    }

会话工厂

public class NhibernateSessionFactory
    {
        public ISessionFactory GetSessionFactory()
        {
            ISessionFactory fluentConfiguration = Fluently.Configure()
                                                  .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("test")))
                                                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyMaps>())
                                                  .BuildSessionFactory();

            return fluentConfiguration;
        }
    }

会话工厂提供程序

 public class NhibernateSessionFactoryProvider : Provider<ISessionFactory>
    {   
        protected override ISessionFactory CreateInstance(IContext context)
        {
            var sessionFactory = new NhibernateSessionFactory();
            return sessionFactory.GetSessionFactory();
        }
    }

休眠模块

 public class NhibernateModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISessionFactory>().ToProvider<NhibernateSessionFactoryProvider>().InSingletonScope();
            Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
        }
    }

服务模块

  public class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ITest>().To<Test>();
        }
    }

回购模块

 public class RepoModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IStudentRepo>().To<StudentRepo>();
        }
    }

家庭控制器

 private readonly ITest test;
        public HomeController(ITest test)
        {
            this.test = test;
        }

        //
        // GET: /Home/
        public ActionResult Index()
        {
           return View();
        }

测试(我的服务层文件)

  public class Test : ITest
    {
        private readonly IStudentRepo studentRepo;

        public Test(IStudentRepo studentRepo)
        {
            this.studentRepo = studentRepo;
        }

    }

回购

  public class StudentRepo : IStudentRepo
    {
        private readonly ISession session;

        public StudentRepo(ISession session)
        {
            this.session = session;
        }
    }

当我通过调试器查看进入我的仓库的会话时。它说会话已打开并已连接,但 (session.Transaction).IsActive = false

【问题讨论】:

  • 您从哪里开始交易?
  • 哈哈,如果我这样做会有所帮助。
  • 我猜是唯一的事情。我想知道是我必须关闭会话还是自动关闭会话?
  • 考虑使用github.com/ninject/ninject.web.mvc 最新版本保证在请求结束时绑定在请求范围内的任何对象都会被释放。您的实现不一定是这种情况。
  • 我最近使用它是因为 asp.net mvc 3 内置了类似的东西。它不做同样的事情吗?为什么这不是 InRequestScope() 方法中的内置事物而不是 mvc 事物,因为我假设如果您使用 Web 表单应用程序,您也会关闭会话。

标签: asp.net-mvc nhibernate fluent-nhibernate asp.net-mvc-3 ninject


【解决方案1】:

您当前设置为使用隐式事务,我认为这些事务不会通过 session.Transaction 公开。当然,Use of implicit transactions is discouraged

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多