"开/关" SQLConnection或保持开放? [英] "open/close" SqlConnection or keep open?

查看:142
本文介绍了"开/关" SQLConnection或保持开放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在简单的静态类实现的静态方法我的业务逻辑。每种方法打开/关闭时调用SQL连接:

I have my business-logic implemented in simple static classes with static methods. Each of these methods opens/closes SQL connection when called:

public static void AddSomething(string something)
{
    using (SqlConnection connection = new SqlConnection("..."))
    {
        connection.Open();
        // ...
        connection.Close();
    }
}

但我想避免打开和关闭连接的保存性能。我做了一些测试,loooong前段时间是的OleDbConnection 类(不知道的SqlConnection),并且它一定帮助像这样的工作(据我记得):

But I think avoiding opening and closing a connection saves performance. I made some tests loooong time ago with OleDbConnection class (not sure about SqlConnection), and it definitely helped to work like this (as far as I remember):

//pass the connection object into the method
public static void AddSomething(string something, SqlConnection connection)
{
    bool openConn = (connection.State == ConnectionState.Open);
    if (!openConn) connection.Open();
    // ....
    if (!openConn) connection.Close();
}

所以,问题是 - 我应该选择的方法(a)或(b)法?我的另一个问题计算器的连接池保存性能我读,我没有在所有的麻烦...

So the question is - should I choose the method (a) or method (b) ? I read on another stackoverflow question that connection pooling saved performance for me, I don't have to bother at all...

PS。这是一个ASP.NET应用程序 - 连接只需要一个Web请求期间存在。不是一个双赢的应用程序或服务。

PS. It's an ASP.NET app - connections exist only during a web-request. Not a win-app or service.

推荐答案

坚持选项即可。

连接池是你的朋友。

这篇关于"开/关" SQLConnection或保持开放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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