EF6.0新特性-DbCommandInterceptor实现非SQL端读写分离
前几天看了一个基于sqlserver的负载均衡与读写分离的软件Moebius,实现的方式还是不错的,这使得用sqlserver数据库的同学时有机会对数据库进行更有效的优化了
看着人有做的东西,自己也想用EF来实现一个读写分离,所以就有了本篇文章,仓储大叔读写分离的思路是:
1 用sqlserver自带的发布、订阅实现主,从数据库的结构,同步这事由sql帮我们完成
2 配置文件建立几个供只读的数据库连接串
3 建立SQL命令拦截器
4 修改大叔的DbContextRepository基数,添加拦截行为
5 测试,搞定
有了上面的想法,咱就可以干事了,第一步不用说了,可以自己百度,从第2步说起
配置文件建立几个供只读的数据库连接串
<!-- 只写--><add name="backgroundEntities" connectionString="metadata=res://*/background.csdl|res://*/background.ssdl|res://*/background.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=background;persist security info=True;user id=sa;password=zzl123;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /><!-- 只读--><add name="backgroundEntitiesRead" connectionString="metadata=res://*/background.csdl|res://*/background.ssdl|res://*/background.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=background_Read1;persist security info=True;user id=sa;password=zzl123;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
建立SQL命令拦截器
/// <summary>/// SQL命令拦截器/// </summary>public class NoLockInterceptor : DbCommandInterceptor{private static readonly Regex _tableAliasRegex =new Regex(@"(?<tableAlias>AS \[Extent\d+\](?! WITH \(NOLOCK\)))",RegexOptions.Multiline | RegexOptions.IgnoreCase);[ThreadStatic]public static bool SuppressNoLock;public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext){string conn = command.Connection.ConnectionString;base.NonQueryExecuting(command, interceptionContext);}public override void ScalarExecuting(DbCommand command,DbCommandInterceptionContext<object> interceptionContext){command.Connection.Close();command.Connection.ConnectionString = "data source=.;initial catalog=background_Read1;persist security info=True;user id=sa;password=zzl123;multipleactiveresultsets=True;application name=EntityFramework";command.Connection.Open();if (!SuppressNoLock){command.CommandText =_tableAliasRegex.Replace(command.CommandText, "${tableAlias} WITH (NOLOCK)");}}public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext){command.Connection.Close();command.Connection.ConnectionString = "data source=.;initial catalog=background_Read1;persist security info=True;user id=sa;password=zzl123;multipleactiveresultsets=True;application name=EntityFramework";command.Connection.Open();if (!SuppressNoLock){command.CommandText =_tableAliasRegex.Replace(command.CommandText, "${tableAlias} WITH (NOLOCK)");}}}
修改大叔的DbContextRepository基数,添加拦截行为
public DbContextRepository(IUnitOfWork db, Action<string> logger){UnitWork = db;Db = (DbContext)db;Logger = logger;((IObjectContextAdapter)Db).ObjectContext.CommandTimeout = 0;//SQL语句拦截器System.Data.Entity.Infrastructure.Interception.DbInterception.Add(new EntityFrameworks.Data.Core.Common.NoLockInterceptor());EntityFrameworks.Data.Core.Common.NoLockInterceptor.SuppressNoLock = true;}
大功造成,感谢阅读!
原文链接1:
EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离
原文链接2:
基于 EntityFramework 的数据库主从读写分离服务插件
转载于:https://www.cnblogs.com/x-poior/p/5257015.html
EF6.0新特性-DbCommandInterceptor实现非SQL端读写分离相关推荐
- WCF4.0新特性体验(9):非破坏性队列消息接收(Non-destructive queue receive )
这次来介绍一下WCF4.0新特性体验(9):非破坏性队列接收(Non-destructive queue receive ).这个特性不是那么直观.确切来说是WCF4.0对于以前处理MSMQ消息队列机 ...
- TiDB 6.0 新特性
TiDB 6.0 新特性 Placement Rules in SQL 小表缓存 内存悲观锁 Top SQL TiDB Enterprise Manager(TiEM) Placement Rules ...
- mysql8.0默认引擎是什么_MySQL8.0新特性【转】
Server层,选项持久化 mysql> show variables like '%max_connections%';+------------------------+-------+ | ...
- mysql sdi_MySQL 8.0新特性: 数据字典
一.概述 数据字典(Data Dictionary)中存储了诸多数据库的元数据信息,包括基本Database, table, index, column, function, trigger, pro ...
- [转]C# 2.0新特性与C# 3.5新特性
C# 2.0新特性与C# 3.5新特性 一.C# 2.0 新特性: 1.泛型List<MyObject> obj_list=new List(); obj_list.Add(new MyO ...
- mysql8.0创建属性,MySQL 8.0新特性 — 管理端口的使用简介
前言 下面这个报错,相信大多数童鞋都遇见过:那么碰到这个问题,我们应该怎么办呢?在MySQL 5.7及之前版本,出现"too many connection"报错,超级用户root ...
- SpringBoot2.0新特性 - Quartz自动化配置集成
https://www.jianshu.com/p/056281e057b3 在新版本的SpringBoot2.0发布后,急迫尝鲜的我将相关的项目已经更换为最新版本,在SpringBoot源码GitH ...
- Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析
在Spring Boot 2.0中推出了Relaxed Binding 2.0,对原有的属性绑定功能做了非常多的改进以帮助我们更容易的在Spring应用中加载和读取配置信息.下面本文就来说说Sprin ...
- [翻译] C# 8.0 新特性
原文: Building C# 8.0 [译注:原文主标题如此,但内容大部分为新特性介绍,所以意译标题为 "C# 8.0 新特性"] C# 的下一个主要版本是 8.0.我们已经为它 ...
- 浅谈Tuple之C#4.0新特性
一.4.0新特性中的Tuple 大家可以回顾一下c#4.0中增加的一个新特性,元组Tuple,它是一种固定成员的泛型集合: 下面先看看官方的一个使用例子: 创建一个包含7个元素的Tuple数组 // ...
最新文章
- 接口里面的方法都是抽象方法吗_正确的敷面膜方法 你真的都掌握了吗
- C++动态数组再总结
- 基于移动位置服务器,移动定位服务器的设计与实现
- currentThread()方法的作用
- C++笔记——malloc基本用法
- #翻译#将像素绘制到屏幕上
- caffe:制作自己的数据集train.txt和val.txt,生成lmdb文件
- 使用Scipy进行函数优化
- MySql-Mysql技术内幕~SQL编程学习笔记(1)
- Redis五种数据结构及常用操作指令、Redis在JAVA中如何封装使用
- 八皇后-韩顺平java
- Android倒计时效果
- 【原创】Web前端性能优化思维导图
- 为真实硬件安装WDM驱动
- 利用java网络通信技术实现一个迷你QQ
- 单片机蜂鸣器发出叮咚c语言程序,单片机门铃程序设计 按键按下触发蜂鸣器门铃“叮咚”响...
- Outlook_Hotkey
- state_dict详解--存放训练过程中需要学习的权重和偏执系数
- C语言hannoi汉诺塔
- Mac下如何将Safari书签导入Chrome