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. 第k大的数

    问题描述:输入一组数,指定一个k,输出这组数里第k大的数. 一般这种题目,第一想法是把整个数组先排序后,再选取第k位的数.但是这样做实际上浪费了大量的时间在排序上,我们只是要求第k大的数,并非要把整个 ...

  2. INSERT INTO IF EXISTS

    INSERT INTO books (name) SELECT 'MySQL Manual' FROM dual WHERE NOT EXISTS (SELECT id FROM booksWHERE ...

  3. 从Java进程里dump出类的字节码文件

    想要查看一些被增强过的类的字节码,或者一些AOP框架的生成类,就需要dump出运行时的Java进程里的字节码. 从运行的java进程里dump出运行中的类的class文件的方法: 用agent att ...

  4. log4jdbc 与 logback 集合打印日志过多的解决

    在项目中使用了log4jdbc,可以很方便的把sql的参数也打印出来,便于问题调试.比如原始sql: select * from t_order where order_id = ? : 经过log4 ...

  5. 面试题----gcc的编译流程

    gcc编译流程 一.    编译与处理指令: gcc -E hello.c -o a.c 如果不使用-o指定输出的文件,会默认输出到终端.所以建议使用同时使用-o选项. 还要注意:编译时会保留#pra ...

  6. MySQL、Mariadb 复制原理

    复制的作用 l  水平扩展 l  数据备份 l  数据分析 l  数据分布 l  高可用性 复制的工作原理 Mariadb的复制功能是基于binlog进行的.复制的工作主要是由主库上Master du ...

  7. MVC实现加载更多

    MVC中实现加载更多 作者 欢醉 关注 2016.01.25 08:48 字数 945 阅读 136评论 0喜欢 2 需要实现的功能: 数据太多想初次加载部分数据,在底部加上“加载更多”按钮 点击后加 ...

  8. 基于Spring Security2与 Ext 的权限管理设计与兑现

    基于Spring Security2与 Ext 的权限管理设计与实现 一.Spring Security介绍 Spring Security的前身Acegi,其配置及使用相对来说复杂一些,因为要配置的 ...

  9. 快速导出云服务器mysql的表数据

    1.许多互联网应用的数据库都布署在远程的Linux云服务器上,我们经常要编辑表数据,导出表数据. 通常的做法是ssh连接到服务器,然后命令登录,命令查询导出数据,费时费力,效率低下. 安装TreeSo ...

  10. 【JavaFx教程】第五部分:将数据用 XML 格式存储

    第5部分的主题 持久化数据为XML 使用JavaFX的FileChooser 使用JavaFX的菜单 在用户设置中保存最后打开的文件路径. 现在我们的地址应用程序的数据只保存在内存中.每次我们关闭应用 ...