SqlConnection 作为静态单例对象

发布于 2024-08-08 11:06:54 字数 501 浏览 3 评论 0原文

public class db
{
    public static string connectionString =
           WebConfigurationManager.ConnectionStrings["connectString"].ConnectionString;
    public static SqlConnection OpenConnection() 
    {
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        return connection;
    }
}

我看到这样的代码,它大错特错了!它适用于 ASP.NET (2.0)。我理解这是错误的。

第一,您不应该打开 SqlConnection 并返回它;第二,为什么要创建静态 SqlConnection?如果多个人尝试同时使用它会不会产生问题?

public class db
{
    public static string connectionString =
           WebConfigurationManager.ConnectionStrings["connectString"].ConnectionString;
    public static SqlConnection OpenConnection() 
    {
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        return connection;
    }
}

I see code like this and it screams WRONG! It's for an ASP.NET (2.0). I understand it to be wrong.

For one you shouldn't open the SqlConnection and return it and two why would you make a static SqlConnection? Won't that create problems if multiple people are trying use it at the same time?

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

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

发布评论

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

评论(4

治碍 2024-08-15 11:06:55

静态的是返回连接的OpenConnection()方法。然而,每次都会返回一个新的连接(假设调用者将负责在适当的时候处理该连接对象)。

换句话说,显示的 db 类根本不是单例。混乱可能源于这样一个事实:不需要实例化 db 实例即可使用其 OpenConnection() 方法(因为它是静态的),并且代码的其余部分可能包含多个片段,例如

myConn = db.OpenConnection();
-- then do something with myConn

What is static is OpenConnection() the Method which returns the connection. A new connection gets returned each time however (the assumption is that caller will be in charge of disposing of this connection object when appropriate).

In other words, the db class shown is not a singleton at all. The confusion may arise from the fact that one does not need to instantiate an instance of db in order to use its OpenConnection() method (since it is static), and the rest of the code may contain multiple snippets like

myConn = db.OpenConnection();
-- then do something with myConn
最单纯的乌龟 2024-08-15 11:06:55

我不是 100% 确定问题是什么,但还是回答一下

如果多个的话会不会产生问题
人们正在尝试同时使用它
时间?

不,不会有问题,因为每次调用 OpenConnection() 都会构造一个新的 SqlConnection 实例。这并不意味着由于其他原因该代码不是垃圾。我至少希望对此方法的调用看起来像这样:

using(var conn = db.OpenConnection())
{
  // stuff
}

I'm not 100% sure what the question is, but to answer this

Won't that create problems if multiple
people are trying use it at the same
time?

no, there wouldn't be problems because each call to OpenConnection() constructs a new SqlConnection instance. That doesn't mean the code isn't garbage for other reasons. I at least hope calls to this method look something like this:

using(var conn = db.OpenConnection())
{
  // stuff
}
樱花细雨 2024-08-15 11:06:55

你不能只是说“错误的尖叫”,然后错误地应用术语“单例”。为什么单个静态连接是一个糟糕的方法?如果您不是每次都重新创建它,那么您的整个应用程序就可以共享该连接。您是否需要为每个 frigg'n sql 调用创建、打开和关闭,这真是愚蠢的尖叫。无论哪种方式,ADO 都应该对此进行管理。

You can't just say "Screams of being wrong" and then apply the term "Singleton" incorrectly. Why is a single static connection a poor approach? It you do not recreate it each time, then your entire application can share the connection. Do you need to create, open and close for each frigg'n sql call, that screams of stupid. Either way ADO should manage this.

权谋诡计 2024-08-15 11:06:55

连接对象的作用域必须限于执行事务的方法。
在线程之间共享相同的连接对象会破坏连接池。
请参阅有关 SQLDataReader.GetOrdinal 的此问题() 很少因 IndexOutOfRange 失败。

Connection objects must be scoped to the method that executes the transaction.
Sharing the same connection object between threads will corrupt the connection pool.
See this question on SQLDataReader.GetOrdinal() fails rarely with IndexOutOfRange.

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