从源码角度看Transaction以及如何手工接管Transaction实现高度的自定义化
一: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实现高度的自定义化的更多相关文章
- 从JDK源码角度看Short
概况 Java的Short类主要的作用就是对基本类型short进行封装,提供了一些处理short类型的方法,比如short到String类型的转换方法或String类型到short类型的转换方法,当然 ...
- 从JDK源码角度看Byte
Java的Byte类主要的作用就是对基本类型byte进行封装,提供了一些处理byte类型的方法,比如byte到String类型的转换方法或String类型到byte类型的转换方法,当然也包含与其他类型 ...
- 从JDK源码角度看Object
Java的Object是所有其他类的父类,从继承的层次来看它就是最顶层根,所以它也是唯一一个没有父类的类.它包含了对象常用的一些方法,比如getClass.hashCode.equals.clone. ...
- 从JDK源码角度看Boolean
Java的Boolean类主要作用就是对基本类型boolean进行封装,提供了一些处理boolean类型的方法,比如String类型和boolean类型的转换. 主要实现源码如下: public fi ...
- 从template到DOM(Vue.js源码角度看内部运行机制)
写在前面 这篇文章算是对最近写的一系列Vue.js源码的文章(https://github.com/answershuto/learnVue)的总结吧,在阅读源码的过程中也确实受益匪浅,希望自己的这些 ...
- Android布局性能优化—从源码角度看ViewStub延迟加载技术
在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码 ...
- 从源码角度看JedisPoolConfig参数配置
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 你好,JedisPoolConfig Java中使用Jedis作为连接Redis的工具.在使用Jedis的也可以配置Jed ...
- 从源码角度看 PHP 字符串类型转换
PHP 的类型转换是比较方便的,但是越是容易使用的东西,底层的实现越是复杂,而且在使用中像我这样的新手也往往不清楚转换后的结果到底是什么.有时候,对于 Java 这种强类型的语言,使用的时候需要强制进 ...
- 从源码角度看finish()方法的执行流程
1. finish()方法概览 首先我们来看一下finish方法的无参版本的定义: /** * Call this when your activity is done and should be c ...
随机推荐
- 推荐一篇mysql优化干货
淘宝的技术一直比较前沿,特别是LVS的作者加入淘宝后,淘宝和阿里的开源做的有声有色,君不见淘宝出了tengine,tairs,tddl,hsf(soa框架,未开源),tfs(小文件存储系统)等等,阿里 ...
- OpenLayers 3 之 切换图层控件
OpenLayers 3 之 切换图层控件 openlayers 3中并没有默认的图层切换控件,GitHub中有一个项目实现了我们需要的控件-------- ol3-layerswitcher . 可 ...
- leetcode357
public class Solution { public int CountNumbersWithUniqueDigits(int n) { ) { ; } ; ; ; && av ...
- vertex shader(3)
之前我们学习了如何声明顶点着色器.如何设置常量寄存器中的常量.接下来我们学习如何写和编译一个顶点着色器程序. 在我们编译一个顶点着色器之前,首先需要写一个. 有17种不同的指令(instruction ...
- Linux任务前后台的切换(转)
Linux任务前后台的切换 Shell支持作用控制,有以下命令实现前后台切换: 1. command& 让进程在后台运行 2. jobs 查看后台运行的进程 3. fg %n 让后台运行的 ...
- plsql程序中循环语句的使用
- 配置siebel捕捉SQL语句
C:\Siebel\15.0.0.0.0\Client\BIN\siebel.exe /c c:\Siebel\15.0.0.0.0\Client\bin\chs\siebel.cfg /B &quo ...
- ORA-00604: 递归 SQL 级别 1 出现错误 ORA-01000: 超出打开游标的最大数
有程序没关闭游标, --打开了哪些游标 select * from v$open_cursor 在open cursor之后一定要注意要close cursor(在store procedure里更应 ...
- 高性能Web服务器Nginx的配置与部署研究(7)核心模块之主模块的非测试常用指令
1. error_log 含义:指定存储错误日志的文件 语法:error_log <file> [debug|info|notice|warn|error|crit] 缺省:${prefi ...
- 141. Linked List Cycle (List; Two-Pointers)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...