gpt4 book ai didi

asp.net - 在 ASP.NET 网站中使用单例进行连接是一个好主意吗

转载 作者:行者123 更新时间:2023-12-02 14:12:33 25 4
gpt4 key购买 nike

我目前在 Web 应用程序上使用单例,以便始终只有一个与数据库的连接。

我想知道这是否是一个好主意,因为现在我遇到了这个错误的问题:

超时已过。从池中获取连接之前超时时间已过。发生这种情况的原因可能是所有池连接都在使用中并且已达到最大池大小。

另一个重要的一点是,我的网站目前处于开发阶段,没有很多人继续使用它,所以我不明白为什么会出现此错误!

这是我的单例的代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// This class take care of all the interaction with the database
/// </summary>
public class DatabaseFacade
{
SqlConnection m_conn = null;

string m_csLanguageColumn;

//Variables that implement the Singleton pattern
//Singleton pattern create only one instance of the class
static DatabaseFacade instance = null;
static readonly object padlock = new object();

/// <summary>
/// Private constructor. We must use Instance to use this class
/// </summary>
private DatabaseFacade()
{
}

/// <summary>
/// Static method to implement the Singleton
/// </summary>
public static DatabaseFacade Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new DatabaseFacade();
}
return instance;
}
}
}

/// <summary>
/// Do the connection to the database
/// </summary>
public void InitConnection(int nLanguage)
{
m_conn = new SqlConnection(GetGoodConnectionString());

try
{
//We check if the connection is not already open
if (m_conn.State != ConnectionState.Open)
{
m_conn.Open();
}

m_csLanguageColumn = Tools.GetTranslationColumn(nLanguage);

}
catch (Exception err)
{
throw err;
}
}
}

感谢您的帮助!

最佳答案

使用单个连接是一个极其糟糕的主意 - 如果对连接的访问​​被正确锁定,则意味着 ASP.NET 一次只能为一个用户提供服务,这将严重限制应用程序的增长能力。

如果连接正确锁定,事情会变得非常奇怪。例如,一个线程可能会释放该连接,而另一个线程则尝试对其执行命令。

您应该在需要时创建新的连接对象,而不是使用单个连接,以利用连接池。

连接池是 the default behavior for the SqlClient classes (可能还有其他数据提供商)。当您使用连接池时,每当您“创建”连接时,该连接实际上都会从现有连接池中拉出,这样您就不会每次都从头开始构建一个连接。当您释放它(关闭它或处置它)时,您将其返回到连接池,从而使连接总数保持相对较低。

<小时/>

编辑:如果您不关闭(或处置)连接,您将看到您提到的错误(从池中获取连接之前超时时间已过)。确保在使用完每个连接后立即执行此操作。

有几个很好的堆栈溢出问题讨论了这个问题,我怀疑它们可能会有所帮助!

关于asp.net - 在 ASP.NET 网站中使用单例进行连接是一个好主意吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1557592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com