Do TransactionScope work with closed database connections?

using (var transaction = new TransactionScope(TransactionScopeOption.Required))
{
// creates a new connection, does stuff, commit trans and close
repos1.DoSomething(); // creates a new connection, does stuff, commit trans and close
repos2.DoSomething(); transaction.Complete();
}


Yes, that should work fine. Internally, the connections should be kept open until the transaction completes. Keep in mind that DTC may be required if multiple connections are used though, even if they are to the same database.

意思就是说在TransactionScope范围中的repos1.DoSomething中创建的DbConnection,并不会在repos1.DoSomething中因为调用了DbConnection.Close而真正关闭,ADO.NET会在repos2.DoSomething里创建并开启另一个DbConnection时,重用repos1.DoSomething创建的DbConnection,在整个TransactionScope范围结束(即using代码块结束)后,ADO.NET才会从底层真正关闭repos1.DoSomething中的DbConnection,这一切都是由ADO.NET 内部管理的。

using (var transaction = new TransactionScope(TransactionScopeOption.Required))
{
// creates a new connection, does stuff, commit trans and close
repos1.DoSomething(); // creates a new connection, does stuff, commit trans and close
repos2.DoSomething(); transaction.Complete();
}

原文链接

If TransactionScope will close database connections的更多相关文章

  1. ERROR 000732:Output Geodatabase:Dataset Database Connections\Connection to localhost.sde\SDE.Dataset does not exist or is not supported

    ArcCatalog 10中向SDE 数据集导入要素类时,出错:ERROR 000732:Output Geodatabase:Dataset Database Connections\Connect ...

  2. eclipse中创建DataBase Connections

    1.window --> show view --> other --> Data Management --> Data Exploerer --> ok: 2.右键单 ...

  3. handle exceptions, opening and closing database connections

    https://www.tutorialspoint.com/spring/spring_jdbc_framework.htm Spring - JDBC Framework Overview Whi ...

  4. 回滚原理 Since database connections are thread-local, this is thread-safe.

    mysql django 实践: django @transaction.atomic 机制分析  1.数据库清空表Tab 2.请求django-view        @transaction.at ...

  5. 解决:eclipse配置Database Connections报错Communications link failure Last packet sent to the server was 0 ms ago

    网上各式各样的问题,不过我的问题在于我开了Proxifier,导致链接localhost的时候被拦截...把Proxifier关了就好了 以后遇到这种问题.连不上数据库啊,连不上本地的服务器啊,先检查 ...

  6. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  7. Database API

    Database API Introduction Basic Usage Selects Joins Aggregates Raw Expressions Inserts Updates Delet ...

  8. How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络

    SQL Server database administrators may frequently need in especially development and test environmen ...

  9. [Windows Azure] Managing SQL Database using SQL Server Management Studio

    Managing Windows Azure SQL Database using SQL Server Management Studio You can use Windows Azure SQL ...

随机推荐

  1. hadoop家族成员

    1.概述 使用hadoop已经有一段时间了,从最开始懵懂到迷茫,再到各种阅读与写作,再到如今各种组合应用,逐渐已经离不开hadoop了,hadoop在大数据行业的成功,加速了它本身的发展,各大社区都能 ...

  2. Doxygen自动文档生成工具在Eclipse中的集成及使用举例

    你有为软件编写说明文档的苦恼吗?当别人甩给你一个庞大的系统,让你根据里面的代码注释理解后写出一份完整的开发文档,你会怎么办?一个个的看代码 然后耗时N天来写吗?这既是一份苦差事也极其耗时,有没有更好的 ...

  3. Java的HashCode,Equal和==

    ----------------------------------------以下是分割线---------------------------------------- 我们都知道Java语言是完 ...

  4. docker-compose部署elk+apm

    1.安装docker 参考我的另外的一篇博客:https://www.cnblogs.com/cuishuai/p/9485939.html 2.安装docker-compose # yum -y i ...

  5. 数组filter()参数详解,巧用filter()数组去重

    数组方法挺多,但是用来用去可能也就foreach,splice以及slice接触较多,filter()说实话之前也没过多了解.其实filter()为数组提供过滤功能,它会遍历数组所有元素,并返回满足条 ...

  6. niftynet Demo分析 -- brain_parcellation

    brain_parcellation 论文详细介绍 通过从脑部MR图像中分割155个神经结构来验证该网络学习3D表示的效率 目标:设计一个高分辨率和紧凑的网络架构来分割体积图像中的精细结构 特点:大多 ...

  7. JVM学习记录-类加载器

    前言 JVM设计团队把类加载阶段中的“通过一个类的全限定名来获取描述此类的二进制字节流”这个动作放到Java虚拟机外面去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码模块称为“类 ...

  8. 版本管理(二)之Git和GitHub的连接和使用

    首先需要注册登录GitHub:https://github.com 然后 ①:下载Git 先从Git官网,由于我的系统是64位的所以选择64-bit Git for Windows Setup htt ...

  9. Docker环境下搭建DNS LVS(keepAlived) OpenResty服务器简易集群

    现在上网已经成为每个人必备的技能,打开浏览器,输入网址,回车,简单的几步就能浏览到漂亮的网页,那从请求发出到返回漂亮的页面是怎么做到的呢,我将从公司中一般的分层架构角度考虑搭建一个简易集群来实现.目标 ...

  10. [转]SQL Server 2008 如何配置报表管理器

    本文转自:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/cc281384%28v%3dsql.100%2 ...