ADO.NET连接池怎么关闭
这篇文章主要讲解了“ADO.NET连接池怎么关闭”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ADO.NET连接池怎么关闭”吧!
创新互联专注于麦积企业网站建设,成都响应式网站建设,商城网站建设。麦积网站建设公司,为麦积等地区提供建站服务。全流程按需定制设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
对于ADO.NET连接池,大家不会陌生。不过多次用过ADO.NET连接池的各位.NET程序员,却不一定深入了解它,具体原因是:
ADO.NET中提供了连接池的功能,多数开发人员很少设置它,因为它是默认的。
界面设置如下图:
关闭连接池也很简单,在连接字符串如下:
Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Pooling=False;
但连接池的本质是什么样的呢?
用Reflector,打开System.Data.SqlClient.SqlConnection的ConnectionString属性的设置值的方法,如下:
private void ConnectionString_Set(string value) { DbConnectionOptions userConnectionOptions = null; DbConnectionPoolGroup group = this.ConnectionFactory.GetConnectionPoolGroup(value, null, ref userConnectionOptions); DbConnectionInternal innerConnection = this.InnerConnection; bool allowSetConnectionString = innerConnection.AllowSetConnectionString; if (allowSetConnectionString) { allowSetConnectionString= this.SetInnerConnectionFrom(DbConnectionClosedBusy.SingletonInstance, innerConnection); if (allowSetConnectionString) { this._userConnectionOptions = userConnectionOptions; this._poolGroup = group; this._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance; } } if (!allowSetConnectionString) { throw ADP.OpenConnectionPropertySet("ConnectionString", innerConnection.State); } if (Bid.TraceOn) { string str = (userConnectionOptions != null) ? userConnectionOptions.UsersConnectionStringForTrace() : ""; Bid.Trace("%d#, '%ls'\n", this.ObjectID, str); } }
再连接 到红色的GetConnectionPoolGroup方法,如下代码
internal DbConnectionPoolGroup GetConnectionPoolGroup(string connectionString, DbConnectionPoolGroupOptions poolOptions, ref DbConnectionOptions userConnectionOptions) { DbConnectionPoolGroup group; if (ADP.IsEmpty(connectionString)) { return null; } if (!this._connectionPoolGroups.TryGetValue(connectionString, out group) || (group.IsDisabled && (group.PoolGroupOptions != null))) { DbConnectionOptions options = this.CreateConnectionOptions(connectionString, userConnectionOptions); if (options == null) { throw ADP.InternalConnectionError(ADP.ConnectionError.ConnectionOptionsMissing); } string str = connectionString; if (userConnectionOptions == null) { userConnectionOptions = options; str = options.Expand(); if (str != connectionString) { return this.GetConnectionPoolGroup(str, null, ref userConnectionOptions); } } if ((poolOptions == null) && ADP.IsWindowsNT) { if (group != null) { poolOptions = group.PoolGroupOptions; } else { poolOptions = this.CreateConnectionPoolGroupOptions(options); } } DbConnectionPoolGroup group2 = new DbConnectionPoolGroup(options, poolOptions) { ProviderInfo = this.CreateConnectionPoolGroupProviderInfo(options) }; lock (this) { Dictionarydictionary = this._connectionPoolGroups; if (!dictionary.TryGetValue(str, out group)) { Dictionary dictionary2 = new Dictionary (1 + dictionary.Count); foreach (KeyValuePair pair in dictionary) { dictionary2.Add(pair.Key, pair.Value); } dictionary2.Add(str, group2); this.PerformanceCounters.NumberOfActiveConnectionPoolGroups.Increment(); group = group2; this._connectionPoolGroups = dictionary2; } return group; } } if (userConnectionOptions == null) { userConnectionOptions = group.ConnectionOptions; } return group; }
TryGetValue是判断是否存在连接字符串为connectionString的连接,存在返回到group,不存在就调用CreateConnectionOptions创建一个DbConnectionOptions,***用
lock (this) { Dictionarydictionary = this._connectionPoolGroups; if (!dictionary.TryGetValue(str, out group)) { Dictionary dictionary2 = new Dictionary (1 + dictionary.Count); foreach (KeyValuePair pair in dictionary) { dictionary2.Add(pair.Key, pair.Value); } dictionary2.Add(str, group2); this.PerformanceCounters.NumberOfActiveConnectionPoolGroups.Increment(); group = group2; this._connectionPoolGroups = dictionary2; } return group; }
这段代码放到连接池中,在这里,可能显示的看到,ado.NET的连接池实质上是一个Dictionary
所谓的连接池,就是一个与连接对象Connection相关的集合,这不只是简单的集合,而是有一定的机制在内部。我们做开发时,可能建立Connection连接对象,关闭连接对象,有时候还调用Dispose来释放连接。下次再用时,便重新实例化一个连接。但在池中的连接不随连接对象的Close或Dispose而释放。如果下次重新建立连接,连接字符串与前一次完全一模一样,则连接池就会把上次可用的连接对象赋给连接去用。如果两个连接字符串有一点不一样,即使在某一个地方多一个空格,连接池也不会以为是相同的连接,这点微软可能在内部只直接去比较两个字符串了,而不是比较连接数据库字符串的键值互相匹配。
连接池的好处就是保留连接对象,防止下次重头再来实例化一个连接对象。
string constr1 = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; string constr2 = "Data Source=(local);Initial Catalog=Pubs;Integrated Security=SSPI;"; string AssMark = "System.Data,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; Assembly ass = Assembly.Load(AssMark); Type SqlConType = null; foreach (Type conType in ass.GetExportedTypes()) { Console.WriteLine(conType .ToString ()); if ("System.Data.SqlClient.SqlConnection" == conType.ToString()) { SqlConType = conType; } } if (SqlConType != null) { Type[] types1 = new Type[0]; ConstructorInfo constructorInfoObj1 = SqlConType.GetConstructor( BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, types1, null); SqlConnection con1 = (SqlConnection)constructorInfoObj1.Invoke(null); con1.ConnectionString = constr1; SqlConnection con2 = (SqlConnection)constructorInfoObj1.Invoke(null); con2.ConnectionString = constr2; PropertyInfo PI = SqlConType.GetProperty("PoolGroup", BindingFlags.Instance | BindingFlags.NonPublic); object poolGroup1 = PI.GetValue(con1, null); object poolGroup2 = PI.GetValue(con2, null); }
(说明:可能找到结果后觉得非常简单,但怎么找到结果的,却是费了很大劲,几乎是5个小时,所以相把找到结果的过程简单说一下:
一开始用Reflector发现SqlConnection中有一个PoolGroup的属性,于是就想在运行时候比较两个SqlConnection对象的这个属性,但由于这个属性是的访问修饰符是internal的,不能直接访问,只有用反射,代码(是经过优化的)如下:
然后在倒数***行设置断点,为比较poolGroup1和poolGroup2的不同,结果发现,当连接字符串一样时,这两个对象的_objectID相同,字符串有一点不同就会不同,这点说明连接池中是用字符串本身比较的,而不是字符串中键值对进行比较。同还发现当con1和con2的ConnectionString不赋值时这两个对象都是null,由此说明关键是ConnectionString赋值上,所以才开始用Reflector查看这个属性的赋值方法,才有上面的代码。
感谢各位的阅读,以上就是“ADO.NET连接池怎么关闭”的内容了,经过本文的学习后,相信大家对ADO.NET连接池怎么关闭这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
当前名称:ADO.NET连接池怎么关闭
分享网址:http://hbruida.cn/article/pgpocj.html