简体   繁体   English

每个查询的新连接还是所有查询的一个连接? (CommandBehavior.CloseConnection 与 CommandBehavior.Default)

[英]New connection for each query or one connection for all queries? (CommandBehavior.CloseConnection vs CommandBehavior.Default)

I'm developing a text analysis desktop application, that intensively queries local database (MSSQLCE 3.5).我正在开发一个文本分析桌面应用程序,它集中查询本地数据库(MSSQLCE 3.5)。 As the user throws text in, it should react in real-time, so I'm using ADO.NET with pure SQL and trying to get the best performance results.当用户输入文本时,它应该实时反应,所以我使用 ADO.NET 和纯 SQL 并试图获得最佳性能结果。

The question is: based on my task, should I keep ONE cached connection (as static variable or singleton) and make queries with CommandBehavior.Default or should I build a new connection for each query and specify CommandBehavior.CloseConnection ?问题是:根据我的任务,我应该保留一个缓存连接(作为 static 变量或单例)并使用CommandBehavior.Default进行查询,还是应该为每个查询建立一个新连接并指定CommandBehavior.CloseConnection

I know that usually it's recommended to close connection ASAP, but should I really do it, if there can be thousands of queries per minute, for example when user pastes a huge text?我知道通常建议尽快关闭连接,但如果每分钟可能有数千个查询,例如当用户粘贴大量文本时,我真的应该这样做吗?

Until now, application worked on CloseConnection.到目前为止,应用程序在 CloseConnection 上工作。 Now I tried to turn it to CommandBehavior.Default with single connection.现在我尝试将其转换为 CommandBehavior.Default 与单个连接。 I can see some small performance speed-up and can't see any problems at this time, but I want to know if there are any strings attached, before I put this to deployment.我可以看到一些小的性能加速,此时看不到任何问题,但我想知道在部署之前是否有任何附加条件。

// one cached connection for all queries
private static DbConnection _connection = null;
public static String MakeQuery()
{            
    if (_connection == null)
    {
        _connection = new SqlCeConnection(...);
        _connection.Open();
    }
    var cmd = new SqlCeCommand("...", _connection);
    using (var reader = cmd.ExecuteReader(CommandBehavior.Default))
    { 

    }
}

vs.对比

// new connection for each query
public static String MakeQuery()
{            
    using (var connection = new SqlCeConnection(...))
    {
       connection.Open();            
       var cmd = new SqlCeCommand("...", connection);
       using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
       { 
           ...
       }
    }
}

Personally, when I need many queries in a rapid succession from a non-web application, I prefer to use one shared connection.就个人而言,当我需要来自非 Web 应用程序的快速连续查询时,我更喜欢使用一个共享连接。

The only problem is that you have to keep an eye on it because it may go down unexpectedly due to a network problem or because SQL server thought it better be closed .唯一的问题是您必须密切注意它,因为它可能由于网络问题或 SQL 服务器认为最好关闭它而意外关闭 That is, you must handle StateChanged events.也就是说,您必须处理 StateChanged 事件。

But connection pooling is on by default anyway.但无论如何,连接池默认情况下是打开的。 It should be handling all these issues for you.它应该为您处理所有这些问题。 Yes, pooling does add an overhead, but it's not big.是的,池化确实增加了开销,但并不大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ExecuteReader(CommandBehavior.CloseConnection) 是否总是关闭连接? - Will ExecuteReader(CommandBehavior.CloseConnection) always close connection? 将DataReader从Method传递到.cs页-使用CommandBehavior.CloseConnection但连接未关闭 - Passing DataReader from Method to .cs page - use CommandBehavior.CloseConnection but connection not closing 在ExecuteReader()中使用CommandBehavior.CloseConnection的用途/优点是什么 - What is the use/advantage of using CommandBehavior.CloseConnection in ExecuteReader() 数据库的良好编码做法:一个连接/查询与一个连接/所有查询 - Good Coding Practice with Databases: One connection / query vs one connection / all queries SQLDataLReader和CommandBehavior.SequentialAccess - SQLDataLReader and CommandBehavior.SequentialAccess 每个查询的新sql连接? - A new sql connection for each query? 为每个查询创建新的SQL连接是否有优势? - Is there an advantage to creating a new SQL connection for each query? CommandBehavior.SequentialAccess是否有任何性能提升? - Is there any performance gain from CommandBehavior.SequentialAccess? 设置CommandBehavior以在Entity Framework中执行命令 - Setting CommandBehavior for executing commands in Entity Framework 自动生成的默认连接字符串与手动添加的字符串 - Autogenerated default connection string vs. manually added one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM