使用说明和连接池 [英] Using Statement and Connection Pooling

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

问题描述

我最近才知道连接池在.NET中的概念,因此我有一点疑问,我想任何人都可以澄清这对我来说。如果我使用以下的一段code,当将数据库连接返回到池中,以便它可以使用该应用程序的另一部分

I recently came to know the concept of 'connection pooling' in .NET, and as such I have a little doubt I would like anyone to clarify it for me. If I use the following piece of code, when will the database connection be returned to the pool so that it can be used by another part of the application?

using (SqlConnection NewConnection = new SqlConnection(ConnectionString))
{
    using (SqlCommand NewCommand = new SqlCommand("SomeCommand", NewConnection))
    {
        try
        {
            NewConnection.Open();

            // Do some work...

            NewConnection.Close(); // <-- Here?
        }
        catch
        {
            // Error handling...
        }
    }
}

// <-- Here?

非常感谢你。

Thank you very much.

推荐答案

连接的确会返回到使用块执行完毕后,池。

The connection will indeed be returned to the pool after the using block has finished executing.

使用语句 语法糖 - 编译器生成一个正确的 处置 块,关闭连接,从而将其返回到连接池。

The using statement is syntactic sugar - the compiler generates a correct Dispose block which closes the connection, thus returning it to the connection pool.

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

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