MARS 对 NHibernate 有什么影响?

发布于 2024-12-08 03:57:52 字数 932 浏览 0 评论 0原文

我正在将实体框架与 NHibernate 进行比较,我想知道在使用 SQL Server 时,启用或禁用 MARS 支持会对 NHibernate 产生什么影响(如果有)?

MARS = 多个活动结果集

实体框架文档声明以下内容:

当您在 foreach (C#) 或 期间调用 Load 方法时对于每个 (Visual Basic) 枚举,实体框架尝试打开一个新的 数据读取器。除非您启用了多个,否则此操作将会失败 通过在中指定 multipleactiveresultsets=true 来激活结果集 连接字符串。有关详细信息,请参阅使用多个活动 MSDN 上的结果集 (MARS)。您还可以加载查询结果 进入列表集合,这会关闭数据读取器并使您能够 枚举集合以加载引用的实体。

NHibernate有同样的问题吗?

连接到 SQL Azure 时的附加信息

I'm comparing Entity Framework with NHibernate, and I'd like to know if when using SQL Server, what effect (if any) would enabling or disabling MARS support have on NHibernate?

MARS = Multiple Active Result Sets

The Entity Framwork documentation states the following:

When you call the Load method during a foreach (C#) or For Each
(Visual Basic) enumeration, the Entity Framework tries to open a new
data reader. This operation will fail unless you have enabled multiple
active results sets by specifying multipleactiveresultsets=true in the
connection string. For more information, see Using Multiple Active
Result Sets (MARS)
on MSDN. You can also load the result of the query
into a List collection, which closes the data reader and enables you
to enumerate over the collection to load referenced entities.

Does NHibernate has the same issue?

Additional information when connecting to SQL Azure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2024-12-15 03:57:52

您所指的问题与“服务器端游标”相关,据我所知,nHibernate 这不应该是一个问题,只是因为它不使用它们。
如果您使用 LINQ 在 nHibernate 中加载对象,则在第一次访问 foreach 枚举时,nHibernate 会在内存中加载查询的整个结果集,这样它就可以使用会话的连接来加载其他所有内容。
当使用 HQL 查询或 Criteria 时,它将在您调用“List()”时加载结果集,然后关闭连接。

另一方面,实体框架在通过 foreach 枚举滚动集合时,尝试变得聪明并利用服务器端游标,因此 objectContext 的连接与服务器端游标“繁忙”,直到 foreach 枚举结束。如果未启用 MARS,EF 无法使用该连接加载另一个结果集(您仍然可以发出其他语句,例如更新、插入和删除),因此它会给您一个错误,例如“已经有一个打开的 DataReader与此命令相关联,必须先关闭”或类似的内容。

希望这有帮助,

马可

编辑:

经过一些研究,我发现 nHibernate 可以使用 MARS,但仍然在版本 3.2.0.4000 中没有支持它的 SqlServer 驱动程序。当然,在 SqlClientDriver 中不支持(因为 Sql2000 不支持 MARS),但即使在 Sql2008ClientDriver 中,相关属性也设置为 false。无论如何,我会尽快将其发布给 nHibernate 团队

The issue you're referring to is linked to "server side cursors", and as far as I know of nHibernate this shouldn't be an issue, simply because it don't use them.
If you're using LINQ to load objects in nHibernate, on the first access to the foreach enumeration, nHibernate load in memory the whole resultset of the query, and this way it can use the session's connection to load everything else.
When using HQL query or Criteria, it will load the resultset when you call "List()" then it close the connection.

Entity framework on the flip side, try to be smart and make use of server side cursors when scrolling a collection via a foreach enumeration, so the objectContext's connection is "busy" with the server side cursor until the foreach enumeration is ended. If MARS is not enabled, EF can't use the connection to load another resultset (you can still issue other statements such update, insert and delete) and thus it will give you an error like "There is already an open DataReader associated with this Command which must be closed first" or something similar.

Hope this helps,

Marco

EDIT:

After some research I've found that nHibernate could use MARS, but still in ver 3.2.0.4000 there's no driver for SqlServer that supports it. Of course in the SqlClientDriver is not supported (as it is inteded for Sql2000 that has no support for MARS) but even in the Sql2008ClientDriver the relevant property is set to false. Anyway this is something I'll post to the nHibernate team as soon as possible

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文