NHibernate连接池 [英] NHibernate Connection Pooling

查看:436
本文介绍了NHibernate连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用流利的NHibernate为SQL Server 2008的新应用程序,我无法理解我所看到的连接处理行为。



我正在监视连接使用sp_who2,这是我所看到的:


  1. 创建SessionFactory时,会打开一个连接。此连接
    似乎保持打开状态,直到应用程序被终止。

  2. 打开一个新的会话时,没有连接打开。 (没关系,我知道NHibernate会一直等到创建数据库连接的最后时刻)。
    即使通过NHibernate运行查询,也没有新的连接被打开。我必须假定它正在使用SessionFactory创建时创建的连接,该连接仍处于打开状态。我在查询之后设置了一个断点(在会话关闭之前),并且在sp_who中没有出现新的会话。


通过单个连接运行整个应用程序是不可接受的(显然)。 我怎样才能确保每个ISession获得自己的连接?我确定有什么明显的我在这里失踪...



谢谢提前。

解决方案

您看到的行为没有特定的NHibernate - 连接池是SQL Server中的默认行为。

即使这听起来可能听起来很尴尬,但实际上这是一件好事,因为创建新的连接并维护它们是很昂贵的。
(更多信息,请参阅关于连接池的维基百科文章

所以没有必要尝试让NH为每个会话打开一个新的连接,因为重用现有的连接实际上提高了SQL Server的性能。

I am considering using Fluent NHibernate for a new application with SQL Server 2008 and I am having trouble understanding the connection handling behavior I am seeing.

I am monitoring connections using sp_who2 and here is what I see:

  1. When the SessionFactory is created, a single connection is opened. This connection seems to stay open until the app is killed.

  2. No connection is opened when a new session is opened. (That's ok, I understand NHibernate waits until the last moment to create database connections).

  3. No new connection is opened even when I run a query through NHibernate. I must assume it is using the connection created when the SessionFactory was created, which is still open. I set a breakpoint after the query (before the session was closed), and no new sessions had appeared in sp_who.

Running an entire app through a single connection is not acceptable (obviously). How can I ensure each ISession gets its own connection? I'm sure there's something obvious I'm missing here...

Thanks in advance.

解决方案

The behaviour that you see is nothing NHibernate specific - Connection pooling is default behaviour in SQL Server.
Even if it may sound awkward at first glance, it actually is a good thing because creating new connections and maintaining them is expensive.
(for more information, see the Wikipedia article about connection pooling)

So there is no need to try to get NH to open a new connection for each session, because reusing existing connections actually improves SQL Server performance.

这篇关于NHibernate连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆