从连接池的角度来看我做错了什么吗?

发布于 2024-10-15 23:13:03 字数 2192 浏览 0 评论 0原文

我有一个类,它使用 ADO.NET 和 LINQ 来访问一台服务器上的一对数据库。数据库中的表并不广泛,因此实体对象相当轻。我已经尽我所能编写了代码,当然是利用经验和网络文章。例如...

http://dotnetperls.com/sqldataadapter http://www.velocityreviews.com/forums/t71781-set-maximum-pool-size-in-web-config.html http://msdn.microsoft.com/en-us/library/ms971481#adonetbest_topic5

运行我的代码的服务器仅运行我的代码,与数据库主机不是同一台服务器。通过查看从我的 .NET 应用程序服务器到数据库服务器的连接(它是一个 Windows 服务,但我不想详细讨论这一点,因为它现在看起来并不重要),连接数约为 200 个,但它当然应该小于那个;它应该在 10 左右,因为我已在 appSettings.config 中将最大池大小设置为 10。

有人可以看一下我的连接代码并告诉我我是否正在做一些会导致连接突然上升的事情吗?

这是我的 LINQ DB 上下文创建:

private const string ConnectionKey = "SQL2";
protected static string ConnectionString
{
    get
    {
        return _connectionString = (_connectionString == null) ? ConfigurationManager.ConnectionStrings[ConnectionKey].ConnectionString : _connectionString;
    }
}
private static string _connectionString = null;

protected static PricingDBDataContext ContextDB
{
    get
    {
        if (_context == null)
        {
            _context = new PricingDBDataContext(ConnectionString);
        }

        return _context;
    }
}
private static PricingDBDataContext _context = null;

这是 ADO.NET 方面的内容:

protected DataSet GetDataSet(bool isSproc, string cmdStr, params object[] args)
{
    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
        {
            cmd.CommandType = isSproc ? CommandType.StoredProcedure : CommandType.Text;
            for (int index = 0; index < args.Length; index += 2)
                cmd.Parameters.AddWithValue(args[index].ToString(), args[index + 1]);
            conn.Open();
            DataSet set = FillSet(cmd);
            conn.Close();
            return set;
        }
    }
}

private DataSet FillSet(SqlCommand cmd)
{
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet set = new DataSet();
    adapter.Fill(set);
    return set;
}

谢谢,

Matt。

I have a class which uses both ADO.NET and LINQ to access a pair of databases on one server. The tables in the DBs are not extensive and so the entity objects are fairly light. I have written the code as best as I, using experience and net articles of course. For example...

http://dotnetperls.com/sqldataadapter
http://www.velocityreviews.com/forums/t71781-set-maximum-pool-size-in-web-config.html
http://msdn.microsoft.com/en-us/library/ms971481#adonetbest_topic5

The server which is running my code is only running my code and is not the same server as the db host. From looking at the connections going to the DB server from the my .NET app server (it's a Windows Service, but I don't want to dwell on that as it seem immaterial right now) the number of connections is around 200, but it should certainly be less than that; It should be around 10 as I have set the Max Pool Size to 10 in the appSettings.config.

Could anyone take a look at my connection code and tell me if I'm doing something which would cause the connections to shoot up, please?

Here is my LINQ DB context creation:

private const string ConnectionKey = "SQL2";
protected static string ConnectionString
{
    get
    {
        return _connectionString = (_connectionString == null) ? ConfigurationManager.ConnectionStrings[ConnectionKey].ConnectionString : _connectionString;
    }
}
private static string _connectionString = null;

protected static PricingDBDataContext ContextDB
{
    get
    {
        if (_context == null)
        {
            _context = new PricingDBDataContext(ConnectionString);
        }

        return _context;
    }
}
private static PricingDBDataContext _context = null;

Here is the ADO.NET side of things:

protected DataSet GetDataSet(bool isSproc, string cmdStr, params object[] args)
{
    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
        {
            cmd.CommandType = isSproc ? CommandType.StoredProcedure : CommandType.Text;
            for (int index = 0; index < args.Length; index += 2)
                cmd.Parameters.AddWithValue(args[index].ToString(), args[index + 1]);
            conn.Open();
            DataSet set = FillSet(cmd);
            conn.Close();
            return set;
        }
    }
}

private DataSet FillSet(SqlCommand cmd)
{
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet set = new DataSet();
    adapter.Fill(set);
    return set;
}

Thanks,

Matt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-10-22 23:13:03

作为一般规则,如果您对连接池执行任何明确的操作,那么您可能做错了。自 .Net 早期以来,默认设置就对我很有用。

以下是一些需要记住的事项:

  • ADO 将池化具有完全相同连接字符串的连接
  • 大多数 ado.net 对象都实现 IDisposable 并且应该可以显式地或在 using 块中进行处理。这包括连接、命令、数据集和数据读取器
  • Linq 2 SQL 和 EF 数据上下文也实现了 IDisposable 并且应该被处置

As a general rule, if you are doing anything explicit with the connection pool, you are probably doing it wrong. The default settings have served me well since the early days of .Net

Here are some things to keep in mind:

  • ADO will pool connections that have exactly the same connection string
  • Most ado.net objects implement IDisposable and should be disposed of either explicitly or in a using block. This includes connections, commands, datasets and data readers
  • Linq 2 SQL and EF data contexts also implement IDisposable and should be disposed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文