使用一个单独的连接在ASP.NET网站是个好主意 [英] Is using a singleton for the connection a good idea in ASP.NET website

查看:216
本文介绍了使用一个单独的连接在ASP.NET网站是个好主意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用我的web应用程序单,以便有永远只有一个连接到数据库。

I'm currently using a singleton on my web application so that there is always only one connection to the database.

我想知道,如果它是一个好主意,因为现在我有与该错误的麻烦:

I want to know if it's a good idea because right now I'm having trouble with that error:

超时过期。之前从池中获取连接超时时间已过。出现这种情况可能是因为所有池连接均在使用,达到最大池大小。

另外重要的一点是,我的网站目前正处于开发,而不是很多人在上面走,所以我不明白为什么我得到这个错误!

Another important point is that my website is currently in dev and not a lot of people go on it so I don't understand why I get this error!

下面是我单身的code:

Here is the code of my singleton:

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只能在一个时间服务于一个用户,这将严重限制了应用程序的增长能力。

Using a single connection is an extremely bad idea - if access to the connection is properly locked, it means that ASP.NET can only serve one user at a time, which will seriously limit your application's ability to grow.

如果连接的的正确锁定,事情可能变得很怪异。例如,而另一个线程试图反对执行一个命令一个线程可以处理的连接。

If the connection is not properly locked, things can get really weird. For example, one thread might dispose the connection while another thread is trying to execute a command against it.

而不是使用一个单独的连接,当你需要他们,把连接池的优势,你应该创建新的连接对象。

Instead of using a single connection, you should just create new connection objects when you need them, to take advantage of connection pooling.

连接池是为的SqlClient类(也可能是其他数据提供者)的默认行为。当您使用连接池,你任何时候创造一个连接,该连接实际上会从现有的池中取出,这样你就不会招致构建每次从头开始的费用。当你释放它(将其关闭或进行处置),就可以返回到连接池,保持连接的总数相对较低。

Connection pooling is the default behavior for the SqlClient classes (and probably other data providers). When you use connection pooling, any time you 'create' a connection, the connection will actually be pulled from a pool of existing ones so that you don't incur the costs of building one from scratch each time. When you release it (close it or dispose of it) you return it to the connection pool, keeping your total count of connections relatively low.


编辑:你会看到你所提到的错误(的超时时间之前从池中获取一个连接过去的),如果你不关闭(或处置)的连接。确保你做,只要你使用的每个连接完成的。

You'll see the error you mention (The timeout period elapsed prior to obtaining a connection from the pool) if you're not closing (or disposing) your connections. Make sure you do that as soon as you're done using each connection.

有几个很好的堆栈溢出问题,讨论这个,我怀疑可能是有益的!

There are several good stack overflow questions that discuss this, which I suspect might be helpful!

这篇关于使用一个单独的连接在ASP.NET网站是个好主意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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