简体   繁体   中英

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. I'm toying with various mapping settings and am watching the session processes via the latest version of NHProfiler.

I'm noticing something odd, and it may not be of concern, but thought I'd check with the intertubes:

[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:

-- 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. 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.

You may need to call ProfilerInfrastructure.FlushAllMessages(); to force NHProf to flush all its messages so that you can see the end transaction statement. I call this in the [TearDown] method in my [SetUpFixture] class for NUnit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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