【问题标题】:Performance tips/hints for .NET webservice providing info from sql server.NET webservice 提供来自 sql server 的信息的性能提示/提示
【发布时间】:2009-03-04 12:30:39
【问题描述】:

我有一个相当简单的 web 服务,它从 sql server 公开数据。它将用于在 2 个不同的数据库(SQL Server 和 Lotus Notes)之间同步数据。我们正处于测试 web 服务的阶段,并以 20 req./min. 进行轮询,前 2 分钟一切正常,但在第二分钟之后,我们遇到了异常,显然是连接(到数据库) 无法打开(超时)。

您对做什么或去哪里有什么建议/建议? Web 服务已使用 C#/.NET 进行编程,与 db 的连接在(Web 服务)对象的构造期间打开,并在对象被释放时关闭。

我曾考虑使用 global.asax 来“共享”连接,但经过一番谷歌搜索后,我发现大多数人认为这是一个坏主意,我正在寻找不同的解决方案。

ps。服务是同步池化的,没有2个请求同时存在

-编辑- (在关于池的前 2 个回答者之后) 这是当前代码:

public class DataService : System.Web.Services.WebService
{
    private SqlConnection conn = new SqlConnection("Data Source=ip;database=database;uid=user;pwd=secret;");
    public DataService () 
    {
            try
            {
                conn.Open();

            }
            catch (Exception dbconn)
            {
                throw new SoapException("Couldn't open connection to database:" + dbconn.Message + " More info at: " + dbconn.HelpLink, errorCode);
            }
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
    }
    ~DataService()
    {
         conn.Close();
    }
    [WebMethod(Description="Gets all Person changes(from last week)")]
    public Person[] GetPerson()
    { 
            Person[] Personen = null;
            SqlCommand sqlcmd = conn.CreateCommand();

            sqlcmd.CommandText = "SELECT * FROM Person";
            SqlDataReader Rows = sqlcmd.ExecuteReader();
            while (Rows.Read())
            {
                    //doSomething
            }

            Rows.Close();
            return Personen;
    }

}

【问题讨论】:

    标签: .net sql-server performance web-services debugging


    【解决方案1】:

    听起来你已经用尽了连接池——最好的选择是用 using 块来包装你的 SQL 调用,这样松散地:

    using( SqlConnection con = new SqlConnection( "MyConnectionString" ) )
    {
        con.Open();
    
        using( SqlCommand cmd = new SqlCommand( "MyStoredProcedure", con ) )
        {
            // Do stuff with the Command
        }
    }
    

    这将允许您同时处理与连接池大小相同数量的请求。

    所以,修改后的代码变成:

    public class DataService : System.Web.Services.WebService
    {
        [WebMethod(Description="Gets all Person changes(from last week)")]
        public Person[] GetPerson()
        { 
            Person[] Personen = null;
    
            using( SqlConnection conn = new SqlConnection("Data Source=ip;database=database;uid=user;pwd=secret;") )
            {
                using( SqlCommand sqlcmd = conn.CreateCommand() )
                {
                    sqlcmd.CommandText = "SELECT * FROM Person";
                    SqlDataReader Rows = sqlcmd.ExecuteReader( CommandBehavior.CloseConnection ); // This will close the DB connection ASAP
                    while (Rows.Read())
                    {
                        //doSomething
                    }
    
                    Rows.Close();
                }
            }
    
            return Personen;
        }
    }
    

    【讨论】:

    • 比在对象构造时设置连接更好吗? (我添加了一些代码来详细说明)。有什么方法可以在连接之前检查连接池或以某种方式对其进行监控?
    • 这是使用数据库连接的推荐方式。打开它,做任何你需要做的事情,然后关闭/处理它。 using 子句将自动处理连接。下次您需要连接时,将从连接池返回相同的连接。
    • 我采纳了你的建议,经过一些小测试后,它现在可以毫无问题地处理 200 多个顺序请求。谢谢!
    【解决方案2】:

    查看此链接Best Practices for Using ADO.NET,您将在其中找到指向SQL Server Connection Pooling (ADO.NET) 的链接 - 确保您确实正确使用了连接池。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多