NHIbernate 影响非 nhibernate 查询? [英] NHIbernate affecting non-nhibernate queries?
问题描述
我在使用 NHIbernate 时遇到了一个奇怪的问题,因为这是我的第一个 NHIbernate 项目,所以我想我会问 StackOverflow.com 上的好人.
I have got an odd problem with NHIbernate, and seeing as this is my first NHIbernate project I thought I'd ask the good people of StackOverflow.com.
我遵循 ASP.Net 中的在视图中打开会话"模式,它在每个请求上打开一个休眠事务并在请求结束时提交它.
I'm following the 'Open session in view' pattern in ASP.Net, which opens a hibernate transaction on each request and commits it at the end of the request.
这正常工作正常,但是在我的其中一个页面上,这只是一个读取页面而不是一个写入页面,我遇到了问题.有问题的页面获取项目列表,并根据它们进行一些信息查询.
This works fine normally, however on one of my pages, which is simply a read page not a write one, I get an issue. The page in question gets a list of projects, and does a few queries for information based on them.
作为其中的一部分,它调用一个外部 DLL,该 DLL 具有对内部数据库的 SQL 查询.此调用似乎适用于所有项目,除了一个,它在 ExecuteReader() 调用中超时.
As a part of that, it calls an external DLL, which has an SQL Query to a database inside of it. This call appears to work for all projects except for one, where it gets a timeout on the ExecuteReader() call.
尝试在外部 DLl 中查找错误一段时间后,我决定注释掉 http 处理程序内部事务的建立.这解决了问题.
After trying to find a bug in the external DLl for some time, I decide to comment out the establishing of the transaction inside of the http handler.This fixed the issue.
因此,以某种方式休眠会话管理正在影响外部的、不相关的(好吧,某些映射有可能触及该查询中使用的相同数据库,但它在两端都是只读的)
So, somehow hibernates session management is affecting external, unrelated (well, there is a chance that some of the mappings touch the same databases that are used in that query, however its read only on both ends)
我的问题是,它为什么要这样做?nhibernate 在幕后做了什么导致其他 SQL 查询超时?我认为它锁定了数据库的某些部分,但是如果页面只读取它为什么要这样做?我怎样才能解决这个问题?
My question is, why is it doing this? What is nhibernate doing under the hood that causes other SQL queries to timeout? I take it it has a lock on some part of the database, but why is it doing that if the page only reads? How can I get around this?
本指南的以下部分http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
更多信息:我有一个 IHttpModule 在 BeginRequest 中执行以下操作
More info: I have a IHttpModule that does the following in BeginRequest
private void BeginTransaction(object sender, EventArgs e)
{
Console.WriteLine("Begin Transaction");
NHibernateSessionManager.Instance.BeginTransaction();
}
然后关闭
private void CommitAndCloseSession(object sender, EventArgs e)
{
Console.WriteLine("End Transaction");
try
{
NHibernateSessionManager.Instance.CommitTransaction();
}
finally
{
NHibernateSessionManager.Instance.CloseSession();
}
}
NHibernateSEssionManager 这样做:- 在 HTTPContext 中存储 ITransaction + ISession.这些可以作为名为 ContextTransaction 和 ContextSession 的属性访问.这些用于:
The NHIbernateSEssionManager does this: - stores an ITransaction + ISession in the HTTPContext. These are accessible as proprties named ContextTransaction and ContextSession. These are used in:
public void BeginTransaction()
{
ITransaction transaction = ContextTransaction;
if (transaction == null)
{
transaction = GetSession().BeginTransaction();
ContextTransaction = transaction;
}
}
public void CommitTransaction()
{
ITransaction transaction = ContextTransaction;
try
{
if (HasOpenTransaction())
{
transaction.Commit();
ContextTransaction = null;
}
}
catch (HibernateException)
{
RollbackTransaction();
throw;
}
}
推荐答案
从它的声音来看,我猜是有一个环境分布式事务导致了交叉影响.您是否使用 TransactionScope
类来管理事务?
From the sound of it I'd guess that there is an ambient distributed transaction which causes the cross-impacts. Are you using the TransactionScope
class for managing the transactions?
这篇关于NHIbernate 影响非 nhibernate 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!