禁用 sp_reset_connection
由于我遇到的一些问题,我正在 .NET Framework 4 中尝试连接池。使用 SQL Profiler,我可以看到每次从连接池中获取连接时,都会执行存储过程 sp_reset_connection。
要摆脱此重置(我实际上不需要 sp_reset_connection)。我尝试将连接字符串中的 Connection Reset 参数设置为 false,但这似乎没有任何效果。我想也许我误解了连接重置参数的目的。
我注意到连接重置参数没有记录在 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx。但在许多其他地方都提到了它,例如 http://www.techrepublic.com/article/educate-yourself-about-net-sql-server-connection-string-syntax/6084879。
如果我将连接重置标志设置为无效值(例如“hello”),则在打开连接时会出现异常,表明实际使用了连接重置标志。
ADO.NET 真的关心连接重置标志吗?
我的代码如下:
static void Main(string[] args)
{
const string connectionString =
"Data Source=(local);Initial Catalog=MyDatabse;User ID=sa;Password=<removed>;Connection Timeout=5;Pooling=true;Min Pool Size=1; Max Pool Size=1; Enlist=false; Connection Reset=false;";
var connections = new List<SqlConnection>();
for (int i = 0; i < 1000000; i++)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand command = new SqlCommand("SELECT * FROM DatabaseVersion", conn);
command.ExecuteNonQuery();
connections.Add(conn);
}
}
}
Due to some problems I'm experiencing, I am experimenting with connection pooling in .NET Framework 4. Using SQL Profiler, I can see that every time a connection is fetched from the connection pool, the stored procedure sp_reset_connection is executed.
To get rid of this reset (I don't actually need the sp_reset_connection). I've tried setting the Connection Reset parameter in the connection string to false, but this does not seem to have any effect. I'm thinking that maybe I'm misunderstanding the purpose of the Connection reset parameter.
I have noticed that the Connection reset parameter is not documented at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx. But it is mentioned on many other places, such as http://www.techrepublic.com/article/educate-yourself-about-net-sql-server-connection-string-syntax/6084879.
If I set the Conection Reset flag to an invalid value (such as 'hello'), I get an exception when opening the connection which indicatest hat the Connection Reset flag is actually used.
Does ADO.NET actually care about the Connection reset flag?
My code below:
static void Main(string[] args)
{
const string connectionString =
"Data Source=(local);Initial Catalog=MyDatabse;User ID=sa;Password=<removed>;Connection Timeout=5;Pooling=true;Min Pool Size=1; Max Pool Size=1; Enlist=false; Connection Reset=false;";
var connections = new List<SqlConnection>();
for (int i = 0; i < 1000000; i++)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand command = new SqlCommand("SELECT * FROM DatabaseVersion", conn);
command.ExecuteNonQuery();
connections.Add(conn);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SqlClient 确实验证
Connection Reset
标志(它必须可转换为布尔值)。但从 .NET 3.5 SP1 开始,该值被忽略。此外,SqlConnectionStringBuilder.ConnectionReset 属性在 .NET 3.5 SP1 中已过时。
原因是设置 Connection Reset = false 是一个巨大的安全漏洞 - 它可能允许重用具有与其关联的敏感元素(例如打开的对称密钥、临时密钥或模拟上下文)的会话。此外,它可能已将
ChangeDatabase
执行到另一个数据库,因此当您重新打开它时,它将连接到“错误”的数据库。设置
Connection Reset = false
的唯一好处是由于避免了往返而提高了 SQL Server 7.0 的性能。连接重置
记录在.NET 3.0 文档。我认为它从 3.5 SP1 文档及更高版本中删除的原因是因为它现在什么也不做。SqlClient does validate the
Connection Reset
flag (it must be convertable to boolean). But since .NET 3.5 SP1, the value is ignored.Additionally, the SqlConnectionStringBuilder.ConnectionReset property was made obsolete in .NET 3.5 SP1.
The reason is that setting
Connection Reset = false
is a huge security hole - it can allow for reusing a session that has sensitive elements associated with it such as opened symmetric keys, temporary keys, or impersonation context. Additionally it could have executedChangeDatabase
to another database, so when you reopen it then it would be connected to the 'wrong' database.The only benefit to setting
Connection Reset = false
is improved performance on SQL Server 7.0 due to avoiding a round trip.Connection Reset
is documented in the .NET 3.0 docs. I assume that the reason it was removed from the 3.5 SP1 docs and later is because it now does nothing.