c# – 为什么强制.Close()和使用(…)后SqlConnection仍然打开?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 为什么强制.Close()和使用(…)后SqlConnection仍然打开?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3120字,纯文字阅读大概需要5分钟。
内容图文
![c# – 为什么强制.Close()和使用(…)后SqlConnection仍然打开?](/upload/InfoBanner/zyjiaocheng/911/370cc8145f914c6a9a9fa30402bf0668.jpg)
我正在对ADO.Net和EF进行一些实验,以便更好地理解它如何处理SQL Server连接.
我发现ADO.Net非常有趣.我正在创建多个任务,调用一个简单的插入SQL脚本,并正确等待处理SqlConnection和SqlCommand.这里没什么特别的,但是当10k任务完成处理时,所有SQL连接仍然挂起(我通过运行sp_who确认).清除这些连接的唯一方法是关闭应用程序实例.
这怎么可能呢?我尝试了很多东西来强迫它关闭,= null数据访问实例强制GC但没有…
我试图从这种行为中弄清楚,但我失败了.有线索吗?
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
for (int i = 0; i < 10000; i++)
{
Task.Run(() =>
{
var dbLegacy = new DataAccessLegacy();
dbLegacy.TableBInsert();
dbLegacy = null;
});
}
Console.ReadKey();
}
public void TableBInsert()
{
using (SqlConnection connection = new SqlConnection(@"Password=qpqp;Persist Security Info=True;User ID=sqlUser2;Initial Catalog=DatabaseA;Data Source=VM2HOSTNAME\VM2INSTANCEA"))
{
using (SqlCommand command = new SqlCommand("DatabaseBInsert", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("ColAInt", SqlDbType.Int);
command.Parameters[0].Value = (new Random()).Next(0, 5000);
command.Parameters.Add("ColBTinyInt", SqlDbType.TinyInt);
command.Parameters[1].Value = (new Random()).Next(1, 255);
command.Parameters.Add("ColCVarchar", SqlDbType.VarChar);
command.Parameters[2].Value = Convert.ToChar((new Random()).Next(1, 255)).ToString();
command.Parameters.Add("ColDVarcharMax", SqlDbType.VarChar);
command.Parameters[3].Value = Convert.ToChar((new Random()).Next(1, 255)).ToString();
command.Parameters.Add("ColEDecimal", SqlDbType.Decimal);
command.Parameters[4].Value = (new Random()).Next(0, 5000) + 0.5;
command.Parameters.Add("ColFSmallInt", SqlDbType.SmallInt);
command.Parameters[5].Value = (new Random()).Next(0, 5000);
command.Parameters.Add("ColGDateTime", SqlDbType.DateTime);
command.Parameters[6].Value = DateTime.Now;
command.Parameters.Add("ColHChar", SqlDbType.Char);
command.Parameters[7].Value = Convert.ToChar((new Random()).Next(1, 255)).ToString();
command.Parameters.Add("ColINVarchar", SqlDbType.NVarChar);
command.Parameters[8].Value = Convert.ToChar((new Random()).Next(1, 255)).ToString();
command.Parameters.Add("ColJNChar", SqlDbType.NChar);
command.Parameters[9].Value = Convert.ToChar((new Random()).Next(1, 255)).ToString();
connection.Open();
command.ExecuteScalar();
connection.Close();
command.Dispose();
}
connection.Dispose();
}
}
解决方法:
默认情况下,ADO.Net使用connection pooling.从文档(强调我的):
Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration. Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call.
内容总结
以上是互联网集市为您收集整理的c# – 为什么强制.Close()和使用(…)后SqlConnection仍然打开?全部内容,希望文章能够帮你解决c# – 为什么强制.Close()和使用(…)后SqlConnection仍然打开?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。