我的会话会自动关闭吗?

发布于 2024-10-14 02:48:46 字数 4681 浏览 1 评论 0原文

编辑

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

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


您好,

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

Global.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);
        }

    }

Session Factory

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;
        }
    }

Session Factory Provider

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

Nhibernate Module

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

Service Module

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

Repo Module

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

HomeController

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

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

Test(我的服务层文件)

  public class Test : ITest
    {
        private readonly IStudentRepo studentRepo;

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

    }

Repo

  public class StudentRepo : IStudentRepo
    {
        private readonly ISession session;

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

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

Edit

Orignal Title: My transaction is closed by the time it gets to my Repo. What am I doing wrong?

I got a answer to my origanl questions(I forgot to open the transaction lol). Now I am wondering if my code is automatically closing the session or if I have to somehow tell it to do this.


Hi

I am using mvc 3.0, nhibernate, fluent nhibernate and ninject 2.0

Global.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);
        }

    }

Session Factory

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;
        }
    }

Session Factory Provider

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

Nhibernate Module

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

Service Module

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

Repo Module

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

HomeController

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

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

Test(my service layer file)

  public class Test : ITest
    {
        private readonly IStudentRepo studentRepo;

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

    }

Repo

  public class StudentRepo : IStudentRepo
    {
        private readonly ISession session;

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

When I look through my debugger at the session that is coming into my repo. It says the session is open and connected but the (session.Transaction).IsActive = false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-10-21 02:48:46

您当前设置为使用隐式事务,我认为该事务不会通过 session.Transaction 公开。当然,不鼓励使用隐式事务

You're currently set up to use implicit transactions, which I don't believe are exposed through session.Transaction. Of course, Use of implicit transactions is discouraged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文