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;
}
}
我看到这样的代码,它大错特错了!它适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
静态的是返回连接的OpenConnection()方法。然而,每次都会返回一个新的连接(假设调用者将负责在适当的时候处理该连接对象)。
换句话说,显示的 db 类根本不是单例。混乱可能源于这样一个事实:不需要实例化 db 实例即可使用其 OpenConnection() 方法(因为它是静态的),并且代码的其余部分可能包含多个片段,例如
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
我不是 100% 确定问题是什么,但还是回答一下
不,不会有问题,因为每次调用 OpenConnection() 都会构造一个新的 SqlConnection 实例。这并不意味着由于其他原因该代码不是垃圾。我至少希望对此方法的调用看起来像这样:
I'm not 100% sure what the question is, but to answer this
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:
你不能只是说“错误的尖叫”,然后错误地应用术语“单例”。为什么单个静态连接是一个糟糕的方法?如果您不是每次都重新创建它,那么您的整个应用程序就可以共享该连接。您是否需要为每个 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.
连接对象的作用域必须限于执行事务的方法。
在线程之间共享相同的连接对象会破坏连接池。
请参阅有关 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.