一: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. 将自己数据转化为cifar10支持的lmdb

    大家都知道,在caffe里面,要运行cifar10的例子就得先由cifar10的数据库.由于caffe为了提高运行效率,减少磁盘寻道时间等,统一了数据接口(lmdb,leveldb). 首先,看一下c ...

  2. 批处理运行Python脚本

    创建 在某个目录下创建script.bat文件 比如在D:\Code\VimCode\Python_auto\Script中 编辑script.bat文件 写入 @python.exe D:\Code ...

  3. Service通信的两篇博文

    普通Service http://blog.csdn.net/liuhe688/article/details/6874378 AIDL通信 http://blog.csdn.net/liuhe688 ...

  4. Vue之cookie操作(原生)

    Vue之cookie操作(原生) 再vue组件中加入以下几个方法,然后调用即可. methods:{ //读取cookie,需要注意的是cookie是不能存中文的,如果需要存中文,解决方法是后端先进行 ...

  5. Linux内核系统调用列表

    一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtablesize 进程所能打开的最 ...

  6. C/C++快速入门

    sscanf与sprint 均在stdio.h头文件下 sscanf用法 sscanf(str, "%d", &n); // 将str中内容以"%d"的 ...

  7. 用django框架开发一个B2C购物网站用户注册知识点总结2

    一:用户部分: 用户注册: 用户注册序列化器: import re from django_redis import get_redis_connection from rest_framework ...

  8. input 隐藏边框

    style='border-left:0px;border-top:0px;border-right:0px;border-bottom:1px; border-bottom-color:Black'

  9. lock free queues

    无锁队列,下面链接是源码,包含4种队列:单生产者单消费者/多生产者多消费者,队列定长/不定长.元素建议为简单数据类型,复杂类型都采用指针形式. queues-master.zip 源码来源:https ...

  10. 525. Contiguous Array两位求和为1的对数

    [抄题]: Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 ...