简体   繁体   English

NHibernate事务未关闭

[英]NHibernate transactions not closing

Out of interest of learning what's going on behind the scenes, I am running some test methods that specifically dump data to my database via NHibernate. 出于对了解幕后情况的兴趣,我正在运行一些测试方法,这些方法专门通过NHibernate将数据转储到数据库中。 I'm toying with various mapping settings and am watching the session processes via the latest version of NHProfiler. 我正在使用各种映射设置,并且正在通过最新版本的NHProfiler观看会话过程。

I'm noticing something odd, and it may not be of concern, but thought I'd check with the intertubes: 我注意到有些奇怪,可能并不在意,但以为我会检查一下intertube:

[Test]
    public void Seed_Default_Users()
    {
        Role adminRole;
        var user = new User { UserName = "nkirkes", IsActive = true, Email = "nkirkes@dtsagile.com" };
        using (var sesh = _sessionFactory.OpenSession())
        {
            userRepo = new NHibernateRepository<User>(sesh);
            roleRepo = new NHibernateRepository<Role>(sesh);

            using (var tx = sesh.BeginTransaction())
            {
                // get the administrators role
                adminRole = roleRepo.Entities.FirstOrDefault(x => x.Name == "Administrator");
                tx.Commit();
            }

            // set up the object relationship from user -> role
            user.AddRole(adminRole);

            using (var tx = sesh.BeginTransaction())
            {
                // save it
                userRepo.Save(user);
                tx.Commit();
            }
        } 
    }

And the resulting output in NHProf is: NHProf中的结果输出为:

-- statement #1
begin transaction with isolation level: Unspecified

-- statement #2
select *
from   (SELECT this_.ROLE_ID as ROLE1_2_0_,
           this_.Name    as Name2_0_
    FROM   ROLES this_
    WHERE  this_.Name = 'Administrator' /* :p0 */)
where  rownum <= 1 /* :p1 */

And that's it. 就是这样。 Now, the insert is actually happening, but NHProf is only showing the initial opening of the 1st transaction and the first select statement. 现在,插入实际上正在发生,但是NHProf仅显示第一个事务和第一个选择语句的初始打开。 What am I missing? 我想念什么? Since the method is actually working I'm inclined to leave it alone, but I also fear that if NHProf isn't seeing the rest of the statements, maybe something else is wrong. 由于该方法实际上是有效的,因此我倾向于将其保留下来,但是我也担心,如果NHProf没有看到其余的语句,可能还有其他问题。

You may need to call ProfilerInfrastructure.FlushAllMessages(); 您可能需要调用ProfilerInfrastructure.FlushAllMessages(); to force NHProf to flush all its messages so that you can see the end transaction statement. 强制NHProf刷新其所有消息,以便您可以看到end transaction语句。 I call this in the [TearDown] method in my [SetUpFixture] class for NUnit. 我在NUnit的[SetUpFixture]类的[TearDown]方法中调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM