基于 EntityFramework 的数据库主从读写分离架构(1) - 原理概述和基本功能实现
public class DbMasterSlaveCommandInterceptor : DbCommandInterceptor
{
private string masterConnectionString = "server=192.168.0.99;port=3306;user id=root;password=123456;persistsecurityinfo=True;database=testdb";
private string slaveConnectionString = "server=192.168.0.101;port=3306;user id=root;password=123456;persistsecurityinfo=True;database=testdb";
public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
this.UpdateConnectionString(interceptionContext, this.slaveConnectionString);
}
public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
this.UpdateConnectionString(interceptionContext, this.slaveConnectionString);
}
public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
this.UpdateConnectionString(interceptionContext, this.masterConnectionString);
}
private void UpdateConnectionString(DbInterceptionContext interceptionContext, string connectionString)
{
foreach (var context in interceptionContext.DbContexts)
{
this.UpdateConnectionString(context.Database.Connection, connectionString);
}
}
private void UpdateConnectionString(DbConnection conn, string connectionString)
{
ConnectionState state = conn.State;
if (state == ConnectionState.Open)
conn.Close();
conn.ConnectionString = connectionString;
if (state == ConnectionState.Open)
conn.Open();
}
}
接着,在 Global.asax 的启动代码中将该 类型的实体注入 EF 全局执行上下文中。
public class MyHttpApplication : HttpApplication
{
protected void Application_Start()
{
DbInterception.Add(new DbMasterSlaveCommandInterceptor());
}
}
怎么样,原理是不是很简单?当然,如果想要实现一些丰富的配置和扩展功能,就还需要很多其他的代码了,关于这些本人将会在后续文章中逐步介绍!
基于 EntityFramework 的数据库主从读写分离架构(1) - 原理概述和基本功能实现的更多相关文章
- 基于 EntityFramework 的数据库主从读写分离架构 - 目录
基于 EntityFramework 的数据库主从读写分离架构 回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目 ...
- 基于 EntityFramework 的数据库主从读写分离架构(2)- 改进配置和添加事务支持
回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目录: src\ NDF.Data.EntityFramew ...
- 基于 EntityFramework 的数据库主从读写分离服务插件
基于 EntityFramework 的数据库主从读写分离服务插件 1. 版本信息和源码 1.1 版本信息 v1.01 beta(2015-04-07),基于 EF 6.1 开发,支持 EF 6.1 ...
- 基于 EntityFramework 的数据库主从读写分离
现在刚开始来研究EntityFramwork,起初是在vs2012中通过工具来创建EF ,但是对我这种不熟悉菜鸟来说 有很多业务用EF做出来还是有点难度的,今天来手动搭建一个EF框架,大神勿喷
- 基于Amoba实现mysql主从读写分离
一.Amoeba简介 Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特 ...
- MySQL搭建主从数据库 实现读写分离
首先声明,实际生产中,网站为了提高用户体验,性能等,将数据库实现读写分离是有必要的,我们让主数据库去写入数据,然后当用户查询的时候,然后在从数据库读取数据,故能减轻数据库的压力,实现良好的用户体验! ...
- Amoeba搞定mysql主从读写分离
前言:一直想找一个工具,能很好的实现mysql主从的读写分离架构,曾经试用过mysql-proxy发现lua用起来很不爽,尤其是不懂lua脚本,突然发现了Amoeba这个项目,试用了下,感觉还不错,写 ...
- Amoeba实现mysql主从读写分离
Amoeba实现mysql主从读写分离 这段在网上看了下关于amoeba的文章,总体感觉好像要比mysql-proxy好的多,也参考了不少的资料,此文章可能与其他文章作者会有雷同的地方,请谅解,但是此 ...
- Mycat - 实现数据库的读写分离与高可用
前言 开心一刻 上语文课,不小心睡着了,坐在边上的同桌突然叫醒了我,并小声说道:“读课文第三段”.我立马起身大声读了起来.正在黑板写字的老师吓了一跳,老师郁闷的看着我,问道:“同学有什么问题吗?”,我 ...
随机推荐
- JQuery 提示用户名密码不为空
$(document).ready(function(){ //HTML()替换 HTML函数 //append()追加 :input = :t ...
- WCF Restful 服务 Get/Post请求
Restful Get方式请求: Restful服务 Get请求方式:http://localhost:10718/Service1.svc/Get/A/B/C http://localhost:1 ...
- LeetCode Optimal Division
原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...
- PADS Logic 脚本的 Fields 一个对象记录
PADS Logic 脚本的 Fields 一个对象记录 PADS Laogic 有一个非常棒的脚本功能,可以导出所以元件. 我目前是把脚本绑定到 Ctrl+S 上,在保存时自动导出 txt 文件,方 ...
- 关于android api 23 +的权限问题
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, n ...
- 使用Log4j将程序日志实时写入Kafka
第一部分 搭建Kafka环境 安装Kafka 下载:http://kafka.apache.org/downloads.html tar zxf kafka-<VERSION>.tgz c ...
- Regex正则表达式
正则表达式 热身 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 例如 g ...
- RecyclerView(滚动控件)的用法
1.首先在build.gradle中添加依赖库 compile 'com.android.support:recyclerview-v7:24.2.1' 2.修改activity_main.xml & ...
- HBuilder使用技巧
ctrl+ Tab 切换 ctrl + shift + D 查找所写的函数
- thinkphp中的dump方法
感受一下,调试. 1.print_r() 2.var_dump() 3.再看看thinkphp中的dump方法 清晰多了!真实够傻的,今天才发现有这么好的调试方法.