一:transaction

CUD: 在一个transaction中。。。

transaction: start

add....

delete...

update...

transaction: commit /rollback

R: nontransction 【无事务操作】

《1》验证:SaveChanges方法中做到的。。。

检测是是否开启事务的最好方式: Log接口 。。。 => Database下面

int num = this.ExecuteInTransaction<int>(() => this._adapter.Update(), executionStrategy, startLocalTransaction, true);

internal virtual T ExecuteInTransaction<T>(Func<T> func, IDbExecutionStrategy executionStrategy, bool startLocalTransaction, bool releaseConnectionOnSuccess)
{
T local2;
this.EnsureConnection(startLocalTransaction);
bool flag = false;
EntityConnection connection = (EntityConnection) this.Connection;
if (((connection.CurrentTransaction == null) && !connection.EnlistedInUserTransaction) && (this._lastTransaction == null))
{
flag = startLocalTransaction;
}
else if ((executionStrategy != null) && executionStrategy.RetriesOnFailure)
{
throw new InvalidOperationException(System.Data.Entity.Resources.Strings.ExecutionStrategy_ExistingTransaction(executionStrategy.GetType().Name));
}
DbTransaction transaction = null;
try
{
if (flag)
{
transaction = connection.BeginTransaction();
}
T local = func();
if (transaction != null)
{
transaction.Commit();
}
if (releaseConnectionOnSuccess)
{
this.ReleaseConnection();
}
local2 = local;
}
catch (Exception)
{
this.ReleaseConnection();
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
}
return local2;
}

《2》 Query操作的业务逻辑。。

flag=false 所以没有执行。。。

try
{
if (flag)
{
dbTransaction = entityConnection.BeginTransaction();
}
T t = func();
if (dbTransaction != null)
{
try
{
dbTransaction.Commit();
}
catch (Exception innerException)
{
throw new CommitFailedException(Strings.CommitFailed, innerException);
}
}
if (releaseConnectionOnSuccess)
{
this.ReleaseConnection();
}
result = t;
}
catch (Exception)
{
this.ReleaseConnection();
throw;
}
finally
{
if (dbTransaction != null)
{
dbTransaction.Dispose();
}
}

三:接管

自己接管有什么好处????

1. 高度的自定义化, 某些操作一个事务,某些操作另一个事务。。。。

2. 改变事务的隔离级别: Commit Read /UnCommit Read (未提交读)

select * from student with (nolock) where .....

效率不是第一位,死锁在第一位。。。

set transaction isolation level read committed //S【Share】锁 IS【锁】

如果一个记录上有一个S锁,那么Insert是进不去了。。。IX

如果加上nolock,我们的insert,update将不会受到影响。。。。 高并发的情况下采取的一个措施。

3.

using (SchoolDBEntities db = new SchoolDBEntities())
{
using (var transaction=db.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
{
try
{
// add.update...
db.Database.Log = Console.WriteLine;

var query = db.Students.FirstOrDefault();

db.SaveChanges();

transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}
}

4. TransactionScope

using (SchoolDBEntities db = new SchoolDBEntities())
{
using (var transaction=new TransactionScope())
{
try
{
// add.update...
db.Database.Log = Console.WriteLine;

var query = db.Students.FirstOrDefault();

db.SaveChanges();

transaction.Complete();

}
catch (Exception ex)
{
Transaction.Current.Rollback();
}
}
}

从源码角度看Transaction以及如何手工接管Transaction实现高度的自定义化的更多相关文章

  1. 从JDK源码角度看Short

    概况 Java的Short类主要的作用就是对基本类型short进行封装,提供了一些处理short类型的方法,比如short到String类型的转换方法或String类型到short类型的转换方法,当然 ...

  2. 从JDK源码角度看Byte

    Java的Byte类主要的作用就是对基本类型byte进行封装,提供了一些处理byte类型的方法,比如byte到String类型的转换方法或String类型到byte类型的转换方法,当然也包含与其他类型 ...

  3. 从JDK源码角度看Object

    Java的Object是所有其他类的父类,从继承的层次来看它就是最顶层根,所以它也是唯一一个没有父类的类.它包含了对象常用的一些方法,比如getClass.hashCode.equals.clone. ...

  4. 从JDK源码角度看Boolean

    Java的Boolean类主要作用就是对基本类型boolean进行封装,提供了一些处理boolean类型的方法,比如String类型和boolean类型的转换. 主要实现源码如下: public fi ...

  5. 从template到DOM(Vue.js源码角度看内部运行机制)

    写在前面 这篇文章算是对最近写的一系列Vue.js源码的文章(https://github.com/answershuto/learnVue)的总结吧,在阅读源码的过程中也确实受益匪浅,希望自己的这些 ...

  6. Android布局性能优化—从源码角度看ViewStub延迟加载技术

    在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码 ...

  7. 从源码角度看JedisPoolConfig参数配置

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 你好,JedisPoolConfig Java中使用Jedis作为连接Redis的工具.在使用Jedis的也可以配置Jed ...

  8. 从源码角度看 PHP 字符串类型转换

    PHP 的类型转换是比较方便的,但是越是容易使用的东西,底层的实现越是复杂,而且在使用中像我这样的新手也往往不清楚转换后的结果到底是什么.有时候,对于 Java 这种强类型的语言,使用的时候需要强制进 ...

  9. 从源码角度看finish()方法的执行流程

    1. finish()方法概览 首先我们来看一下finish方法的无参版本的定义: /** * Call this when your activity is done and should be c ...

随机推荐

  1. springboot-shiro chapter01——创建springboot webmvc环境

    简介:本章提供了springboot简单例子,主要包含以下内容 1.pom.xml依赖资源 2.springboot配置 3.web应用spring mvc 环境: IDEA15+ JDK1.8+ M ...

  2. 来谈谈 WebAssembly 是个啥?为何说它会影响每一个 Web 开发者?

    作者:link 原文:What is WebAssembly and why it affects web developers! 你听说过WebAssembly吗?这是由Google, Micros ...

  3. 10.Redis 性能测试

    转自:http://www.runoob.com/redis/redis-tutorial.html Redis 性能测试是通过同时执行多个命令实现的. 语法 redis 性能测试的基本命令如下: r ...

  4. django -- url 的 默认值

    在urls.py里可以直接向函数传递默认值,看代码: urls.py from django.conf.urls import url from mytest import views urlpatt ...

  5. Linux 下VI文件乱码解决

    linux在vi 模式查看文件会有乱码问题 如图: 怎么解决呢? 在vi中输入冒号 然后执行下面的命令 如果系统编码不是utf8,vi看uft8编码文件时这样用:set termencoding=ut ...

  6. Qt(自适应窗口)

    关于窗口布局: 默认控件的大小为最小尺寸: 如果此时右键设置布局时,窗口大小会自动缩放对应大小,不利于调整. 建议窗口控件设置好最小尺寸,便于窗口布局时,控件不会变形,例如: 改变minimumsiz ...

  7. SQL 语句中的in、find_in_set、like的区别

    1.in查询相当于多个or条件的叠加,例如: select * from user where user_id in (1,2,3);等效于select * from user where user_ ...

  8. 用Redis解决互联网项目的数据读取难点

    Redis在很多方面与其他数据库解决方案不同:它使用内存提供主存储支持,而仅使用硬盘做持久性的存储:它的数据模型非常独特,用的是单线程.另一个大区别在于,你可以在开发环境中使用Redis的功能,但却不 ...

  9. 一致性协议之Paxos算法

    一.算法提出背景 Paxos算法需要解决的问题就是如何在一个可能发生诸如宕机或网络异常情况的分布式气筒中,快速且正确地在集群内部对某个数据的值达成一致,并且保证不论发生以上任何异常,都不会破坏整个系统 ...

  10. Openssl rand命令

    一.简介 rand命令用来产生伪随机字节,随机数字产生器需要一个seed,在没有/dev/srandom系统下的解决方法是自己做一个~/.rnd文件 二.语法 openssl rand [-out f ...