请问我的会被自动关闭? [英] Will my session be automatically closed?

查看:92
本文介绍了请问我的会被自动关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

一部开拓创新的标题:我的交易是由它获取到我的回购时间关闭。我在做什么错了?

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

我得到了一个回答我的问题origanl(我忘了打开事务笑)。现在我想知道如果我的code自动关闭会话,或者,如果我必须以某种方式告诉它这样做。

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.

我使用MVC 3.0,NHibernate的,流利的NHibernate和ninject 2.0

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

Global.asax中

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

    }

会话工厂

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

NHibernate的模块

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

服务模块

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

回购模块

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

HomeController的

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

    }

回购

  public class StudentRepo : IStudentRepo
    {
        private readonly ISession session;

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

当我通过调试器看一下即将进入我的回购协议的会话。它说,会议是开放的连接,但(session.Transaction).IsActive = FALSE

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

推荐答案

您目前正在设置为使用隐式的交易,我不相信通过 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.

这篇关于请问我的会被自动关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆