【问题标题】:New DbConnection performance新的 DbConnection 性能
【发布时间】:2016-05-02 01:18:28
【问题描述】:

这是我从数据库中选择数据的函数:

    public DataTable SelectDataTable(string selectStatement, string connectionString)
    {
        using (var oracleConnection = new OracleConnection(connectionString))
        {
            using (var command = new OracleCommand(selectStatement, oracleConnection))
            {
                return SelectDataTable(command);
            }
        }
    }
    public DataTable SelectDataTable(OracleCommand command)
    {
        var result = new DataTable();

        try
        {
            var adapter = new OracleDataAdapter(command);
            adapter.Fill(result);
        } catch (OracleException ex)
        {
            throw NewDatabaseException("Error on fill data table.", command.CommandText, ex);
        }

        return result;
    }

我经常调用这个函数,我有点担心会出现性能问题,因为我仍在创建新的 DbConnection。只创建一个 DbConnection,将其存储在现场并多次使用它会更好吗?

【问题讨论】:

  • 根据 MSDN 当请求一个 OracleConnection 对象时,如果可用的连接可用,则从池中获取。
  • 在需要的时候创建新的连接并在使用后尽快关闭连接是正常的。您的代码示例符合这一点,所以不,我不会担心性能问题,除非您有证据证明存在性能问题。

标签: c# database dbconnection


【解决方案1】:

在连接字符串中将 MinPoolSize 设置为大于 0 的值,因此不会创建新连接,而是从现有连接池中获取。还将 MaxPoolSize 设置为高于 MinPoolSize 的数字。

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多