NHibernate 事务未关闭
出于了解幕后发生的事情的兴趣,我正在运行一些测试方法,专门通过 NHibernate 将数据转储到我的数据库。我正在尝试各种映射设置,并通过最新版本的 NHProfiler 观察会话进程。
我注意到一些奇怪的事情,可能并不重要,但我想我应该检查一下管间:
[Test]
public void Seed_Default_Users()
{
Role adminRole;
var user = new User { UserName = "nkirkes", IsActive = true, Email = "[email protected]" };
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();
}
}
}
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 */
就是这样。现在,插入实际上正在发生,但 NProf 仅显示第一个事务和第一个 select 语句的初始打开。我缺少什么?由于该方法实际上有效,我倾向于不去管它,但我也担心如果 NProf 没有看到其余的陈述,也许还有其他问题。
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 = "[email protected]" };
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要调用ProfilerInfrastruct.FlushAllMessages();来强制NHProf刷新其所有消息,以便您可以看到结束事务语句。我在 NUnit 的
[SetUpFixture]
类的[TearDown]
方法中调用此方法。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.