数据库连接池的疑惑?

来源:互联网 发布:上海铭悦软件 编辑:程序博客网 时间:2024/06/03 20:15

做了个试验:

WinForm窗体上放三个按钮,代码分别为:

string strConn1 = @"Provider=SQLOLEDB.1;Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI";

string strConn2 = @"Data Source=(local);Initial Catalog=Northwind;Provider=SQLOLEDB.1;Integrated Security=SSPI";

//注意:连接串strConn1 、strConn2除了属性顺序有所不同,其他都一样。

OleDbConnection conn1, conn2;

private void button1_Click(object sender, EventArgs e)
{
conn1 = new OleDbConnection(strConn1);
conn1.Open();
}

private void button2_Click(object sender, EventArgs e)
{
conn2 = new OleDbConnection(strConn2);
conn2.Open();
}

private void button3_Click(object sender, EventArgs e)
{
conn1.Close();
}

  然后打开“控制面板”/“管理工具”/“性能”,在性能查看器中添加计数器,在“添加计数器”对话框中,选择“性能对象”为“SQLServer:General Statistics”,再在下面的列表框中选择“User Connections”,最后“添加”。然后点击工具栏中“查看报表”,可以看到:
-------------------------------------------------
SQLServer:General Statistics
User Connections 0
-------------------------------------------------
表明当前SQLServer连接数为0。

接下来点击上述WinForm窗体上的Button1,看到:
User Connections ------1 //表明建立了一个连接

再点击Button3,看到:
User Connections ------1 //表明连接并没有被关闭,而是放回到了连接池中

最后点击Button2,仍然看到:
User Connections------1 //疑问之处!

按理最后点击Button2,应该看到:User Connections ------2 ,因为两个连接串并不完全相同。但事实上连接数却仍为1!


为什么呢? 我的分析是:点击Button3,关闭了第一个连接,连接被放回到连接池;然后点击Button2时,系统使用了连接池中的那个连接,并没有创建新的连接,所以看到连接数仍为1。


但是,MSDN上看到,两个连接串必须完全相同(包括其中属性的顺序)时,第二个连接才会使用池中的连接。这不跟上述试验矛盾了吗?


难道 是ADO.NET 2.0 的新特性 ?

原创粉丝点击