A SqlConnection consists of two parts: the public instance that your code interacts with (the outer connection) and a hidden connection that represents an actual server connection (the inner connection).

When you call the Open method on the outer connection, the connection manager looks for a free inner connection from the pool that is associated with the transaction. It will create a new inner connection if one does not exist. When the Close method is called on the outer connection, it returns the inner connection to the pool, where the connection State is reset to ensure a clean slate when it is reused from the pool. SQL Server does not distinguish between the outer and inner connections, so only one outer connection can be used with one inner connection at a time.

When a pooled connection is reset, cursors are closed, options are set back to defaults, the database context is switched back to the one specified in the connection string, and all outstanding local transactions are rolled back. This behavior provides a known starting state every time that you open a connection from the pool. It also prevents you from reusing the inner connection until the local transaction finishes. When you close the transaction's outer connection, the inner connection is set aside pending a commit or rollback request from the transaction manager. When you open a new outer connection while the inner connection is still active, the pool will be empty, and a new inner connection is created. The local transaction manager will then detect that two different resources are trying to enlist in the same transaction. Because SQL Server connections cannot share in local transactions, the transaction manager must promote the transaction to a full distributed transaction.

以上来自于MSDN: http://msdn.microsoft.com/en-us/library/ms172070(v=vs.90).aspx

简言之,"SqlConnection " 由两部份组成:外部可操作实例和内部隐藏的连接。

当调用外部连接的open方法的时候,连接管理类寻找一个内部连接去关联。当找不到对应的内部连接时,将会创建一个内部连接实例。

当调用外部连接的close方法的时候,连接管理类将内部连接状态标记为等待提交或者回滚。一直等待提交或者回滚以后,将这个内部连接扔会到连接池,然后重置内部连接以备用。

这就是为什么命名SqlConnection都已经被close,相关的Transactions仍然能够被回滚,因为真正的连接并没有断开。

[杂]SQL Server 之 Understanding Connection Pooling and Transactions的更多相关文章

  1. [杂]SQL Server 之 Service Broker

    由于某些原因,我们的缓存依赖于数据库,而数据库反向通知需要依赖和使用ServiceBroker, 由于Deploy的人往往不是很清楚这个SB需要如何部署,特此记录. 判断数据库是否启用了Service ...

  2. [杂]SQL Server 之命名管道连接

    命名管道是通过进程间通信(IPC)机制实现通信.具体来说,命名管道建立在服务器的IPC$共享基础上,通过IPC$共享来进行通信. SQL Server命名管道 SQL Server 首先在服务器上创建 ...

  3. 微软Microsoft SQL server 之 MDS connection问题

    微软的MDS和DQS使用面大概还不太广的两个新产品,之前有说道DQS的system.web的register的问题,MDS的问题就经常会碰到链接的问题,当你的用户在Excel中经常都会发生以下问题的时 ...

  4. .net core 连接sql server 时提示Connection Timeout Expired

    .net core开发环境是ubuntu LINUX, 在ubuntu 上 telnet 数据库IP 端口是通的. SQL SERVER 是2008 ,未打SP补丁. 打完 SQL SERVER  2 ...

  5. Understanding how SQL Server executes a query

    https://www.codeproject.com/Articles/630346/Understanding-how-SQL-Server-executes-a-query https://ww ...

  6. ADO.NET Connection Pooling at a Glance

    ADO.NET Connection Pooling at a Glance Establishing a connection with a database server is a hefty a ...

  7. Migrating Oracle on UNIX to SQL Server on Windows

    Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Ap ...

  8. JDBC连接SQL Server代码模板

    *                  JDBC连接SQL Server数据库 代码模板* Connection: 连接数据库并担任传送数据的任务:* Statement :  执行SQL语句:* Re ...

  9. ORACLE透明网关访问SQL Server配置总结

      透明网关概念 ORACLE透明网关(Oracle Transparent Gateway)可以解决ORACLE数据库和非ORACLE数据库交互数据的需求.在一个异构的分布式环境中,通过ORACLE ...

随机推荐

  1. Linux下不同机器之间的文件拷贝

    通过 scp 命令实现不同机器之间的文件拷贝. (1)本机考到目标机器:scp 本机文件 目的地: 如:scp /home/odp-web.war   root@192.168.6.137:/usr/ ...

  2. jsp页面显示数据库乱码

    如何页面是utf-8,数据库也是的话,页面显示数据库乱码的话,就是数据库的格式有问题

  3. linux基本命令(2)-备份压缩命令

    一.tar命令 .解压文件 .tar.gz -C /opt (解压到/opt下) / /opt/tomcat (建立链接文件) 二.zip命令 // 1.把/home目录下面的mydata目录压缩为m ...

  4. Source Insight 基本使用(1)-使用Source Insight查看Android Framework 源码

    一.下载framework源码: google已经把framework源码托管在了gitHub上: https://github.com/android/platform_frameworks_bas ...

  5. 如何使用vim的帮助功能

    set guioptions+-=m/T 更换vim的默认color schema: 将下载的color.vim文件如sonofob'sidian.vim放到/usr/share/vim/vimfil ...

  6. tar 的一个记录

    今天看到群里有人在问.用tar命令备份/etc/passwd 文件提示删除/的提示 这里我要说明下,tar的打包压缩 命令: tar -zcv -f /backup/passwd.tar.gz /et ...

  7. ASP.NET MVC学习笔记-----使用自定义的View Engine

    我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...

  8. 模式串匹配之KMP算法

    模式串匹配之KMP算法 KMP算法 模式值计算(next[j]) (1) next[0]=-1,  第一个字符模式值为-1 (2) next[j]=-1, T中下标为j的字符与首字符相同,且j前面的1 ...

  9. Spring常用的接口和类(二)

    七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...

  10. 第11章 使用Vsftpd服务传输文件

    章节简述: 本章节先通过介绍文件传输协议来帮助读者理解FTP协议的用处,安装vsftpd服务程序并逐条分析服务文件的配置参数. 完整演示vsftpd服务匿名访问模式.本地用户模式及虚拟用户模式的配置方 ...