我通过搜索堆栈溢出和其他网站了解到单例是一种糟糕的编程实践。
我严重依赖 OleDbConnection 来连接到 SqlServer,我的页面可以实例化 20 多次连接。我想用单例替换它,以避免在已经打开连接时打开连接。
我的问题是:
我的情况是 Singleton 不好吗?
在 sql server 2008 上实现多个连接的最佳方法是什么?
i learned by searching stack overflow and other sites that singleton is a bad programming practice.
i'am relying heavily on OleDbConnection to connect to an SqlServer , i have pages that can instantiate like 20+ times a connection . i wanted to replace this by a singleton to avoid opening connections when there's already a connection opened.
My questions are :
Is Singleton in my case bad ?
What is the best way to achieve multiple connections on an sql server 2008 ?
发布评论
评论(2)
你所描述的与单例无关......单例本身既不好也不好 - 只有当出于错误的原因使用时它才是坏的(就像一切一样)。
至于您的连接问题:
这种情况通常是通过连接池解决/处理的...
您可以自己实现(不推荐)...取决于您使用的 ADO.NET 提供程序,它可能已经附带了一个良好实现的和经过良好测试的连接池(例如来自 Devart 的连接池 - 没有附属关系,只是一个满意的客户)...
连接池为数据库连接提供了某种缓存...当您需要一个连接池时,您可以从池中获取它,当你完成了,将其返回到池中...池保持连接(对于自上次使用以来的某个可配置的时间段或类似的)...由于池中的连接已经创建并打开,您将获得显着的速度优势...
OTOH,如果您使用的连接使用,您将无法真正从连接池中获利不同的登录和/或连接设置...
更新:
OleDB 附带内置连接池机制 - 请参阅 MSDN 了解详细信息。根据 MSDN,它默认启用...在关闭/处置连接时,它将自动返回到池...这反过来意味着您可能已经在使用池机制(假设您正在使用OleDB 的默认设置)。
what you describe has nothing to do with Singleton... singleton itself is neither bad nor good - only when used for the wrong reason it is bad (as everything).
as for your connection issue:
This situation is usually solved/handled via a connection pool...
You could implement that yourself (not recommended)... depending on the ADO.NET provider you use it might already come with a well-implemented and well-tested connection pool (for example the one from Devart - not affiliated, just a happy customer)...
Connection pools provide sort of a cache for DB connections... when you need one you acquire it from the pool, when you are finished you return it to the pool... the pool keeps connections around (for some configurable time period since last use or similar)... since the connections in the pool are already created and open you gain a significant speed advantage...
OTOH you can't really profit from a connection pool if the connections you use use different logins and/or connection settings...
UPDATE:
OleDB comes with a built-in connection pooling mechanism - see MSDN for details. According to MSDN it is enabled by default... on closing/disposing the connection it will automatically be returned to the pool... which in turn means for your situation that you might already be using the pooling mechanism (assuming you are using the default settings for OleDB).
单例在您的情况下不是正确的选择,因为建议的做法是在完成数据库事务后立即关闭连接。即使您经常需要数据库连接,您也应该在需要时打开连接并尽快关闭它尽可能。
除此之外,Singleton 根本不是一个糟糕的编程实践。 Singleton 是编程中广泛使用的模式。没有模式是不好的编程实践。只是您应该在它们适合并且完全有意义的场景中使用它们。如果您将在不适当的场景中使用模式,那将是一种糟糕的编程实践。
编辑
需要明确的是,在我看来,经验法则应该是您不应该为将来的操作保持开放的连接。如果有一些待处理的事务,显然最好使用相同的连接来执行所有事务。但同样,您不应该为将来可能需要执行的操作保持连接打开状态
Singleton is not the right choice in your case because it is a recommended practise to close the connection as soon as you are done with your database transaction.Even if you need database connection frequently, you should open connection whenever you need and close it as soon as possible.
Other than that Singleton is not a bad programming practise at all. Singleton is a widely used pattern in programming. No pattern is bad programming practise. It is just that you should be using them in a scenario where they fit in and make perfect sense. If you will use a pattern in a inappropriate scenario that would be a bad programming practise.
Edit
Just to be clear, in my opinion, rule of thumb should be that you should not keep a connection open for operations in future. If there are some pending transactions, obviously it would be best to use the same connection to execute all the transactions. But again you should not keep the connection open for something that you might need to do in future