如何关闭NHibernate连接?

时间:2023-02-06 02:32:45

I've done a site using ASP.NET MVC, NHibernate and MySQL. In my project, I have some repositories classes, each one with methods using codes like this:

我用ASP做了一个网站。NET MVC, NHibernate和MySQL。在我的项目中,我有一些存储库类,每个都有使用如下代码的方法:

using(ISession session = NHibernateHelper.OpenSession()) {
    using(ITransaction transaction = session.BeginTransaction()) {
        session.Save(cidade);
        transaction.Commit();
    }
}

I come from Classic ASP, so I'm usure if the connection with MySQL is actually closed. In fact, I'm usure if there is a connection like there was on Classic ASP.

我来自经典的ASP,所以如果与MySQL的连接实际上是关闭的,我是不会有问题的。事实上,如果有像经典ASP那样的连接,我是不会有问题的。

Should I do something to explicit close the connection/session or is it "autocloseable"?

我应该做些什么来显式关闭连接/会话,还是它是“自动关闭的”?

There is a way to do if there are a lot of opened connection on my server?

如果我的服务器上有很多打开的连接,有一种方法可以做到这一点。

Thank you very much.

非常感谢。

1 个解决方案

#1


5  

The session does the connection management in a very efficient way, so you don't have to do anything manually, as long as you don't forgot to dispose the session (like you do). The connection is not open during the whole lifetime of the session, but only when it's needed. There won't be more open connections to the database than the amount of connections in the connectionpool, which is 100 by default. 100 connections won't be to many in almost every scenario, so you don't need to do anything anything when a lot of connections are opened. On a very busy website, you even won't use all the 100 connections, it will be something like 20-40 open connections because of the size of the threadpool for the webrequests.

会话以一种非常有效的方式进行连接管理,所以您不必手工做任何事情,只要您不忘记处理会话(就像您所做的那样)。在会话的整个生命周期中,连接都没有打开,只有在需要时才打开。在数据库中,没有比连接池中的连接数更开放的连接了,连接池默认为100。在几乎所有的场景中,100个连接都不是很多,所以当打开很多连接时,您不需要做任何事情。在一个非常繁忙的网站上,你甚至不会使用所有的100个连接,而是20-40个开放连接,因为webrequest的线程池的大小。

#1


5  

The session does the connection management in a very efficient way, so you don't have to do anything manually, as long as you don't forgot to dispose the session (like you do). The connection is not open during the whole lifetime of the session, but only when it's needed. There won't be more open connections to the database than the amount of connections in the connectionpool, which is 100 by default. 100 connections won't be to many in almost every scenario, so you don't need to do anything anything when a lot of connections are opened. On a very busy website, you even won't use all the 100 connections, it will be something like 20-40 open connections because of the size of the threadpool for the webrequests.

会话以一种非常有效的方式进行连接管理,所以您不必手工做任何事情,只要您不忘记处理会话(就像您所做的那样)。在会话的整个生命周期中,连接都没有打开,只有在需要时才打开。在数据库中,没有比连接池中的连接数更开放的连接了,连接池默认为100。在几乎所有的场景中,100个连接都不是很多,所以当打开很多连接时,您不需要做任何事情。在一个非常繁忙的网站上,你甚至不会使用所有的100个连接,而是20-40个开放连接,因为webrequest的线程池的大小。