日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

ef mysql 读写分离_EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一...

發布時間:2025/3/11 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ef mysql 读写分离_EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

///

///SQL命令攔截器///主要實現EF的讀寫分離///

public classCommandInterceptor : DbCommandInterceptor

{staticCommandInterceptor()

{

readConnList=DistributedReadWriteManager.Instance;

sysTimer.Enabled= true;

sysTimer.Elapsed+=sysTimer_Elapsed;

sysTimer.Start();

}///

///是否在一個事務中,如果是select,insert,update,delete都走主庫///ThreadStatic標識它只在當前線程有效///

[ThreadStatic]public static bool IsTransactionScope = false;///

///鎖住它///

private static object lockObj = new object();///

///定期找沒有在線的數據庫服務器///

private static Timer sysTimer = new Timer(5000);///

///讀庫,從庫集群,寫庫不用設置走默認的EF框架///

private static IListreadConnList;#region Private Methods

private static void sysTimer_Elapsed(objectsender, ElapsedEventArgs e)

{if (readConnList != null &&readConnList.Any())

{foreach (var item inreadConnList)

{//心跳測試,將死掉的服務器IP從列表中移除

var client = newTcpClient();try{

client.Connect(newIPEndPoint(IPAddress.Parse(item.Ip), item.Port));

}catch(SocketException)

{//異常,沒有連接上

readConnList.Remove(item);

}if (!client.Connected)

{

readConnList.Remove(item);

}

}

}

}///

///處理讀庫字符串///

///

private stringGetReadConn()

{if (readConnList != null &&readConnList.Any())

{var resultConn = readConnList[Convert.ToInt32(Math.Floor((double)new Random().Next(0, readConnList.Count)))];return string.Format(System.Configuration.ConfigurationManager.AppSettings["readDbConnection"]

, resultConn.Ip

, resultConn.DbName

, resultConn.UserId

, resultConn.Password);

}return string.Empty;

}///

///只讀庫的選擇,加工command對象///說明:事務中,所有語句都走主庫,事務外select走讀庫,insert,update,delete走主庫///希望:一個WEB請求中,讀與寫的倉儲使用一個,不需要在程序中去重新定義///

///

private voidReadDbSelect(DbCommand command)

{if (!string.IsNullOrWhiteSpace(GetReadConn()))//如果配置了讀寫分離,就去實現

{

command.Connection.Close();if (!command.CommandText.StartsWith("insert", StringComparison.InvariantCultureIgnoreCase) && !IsTransactionScope)

command.Connection.ConnectionString=GetReadConn();

command.Connection.Open();

}

}#endregion

#region Override Methods

///

///Linq to Entity生成的update,delete///

///

///

public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContextinterceptionContext)

{base.NonQueryExecuting(command, interceptionContext);//update,delete等寫操作直接走主庫

}///

///執行sql語句,并返回第一行第一列,沒有找到返回null,如果數據庫中值為null,則返回 DBNull.Value///

///

///

public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContextinterceptionContext)

{

ReadDbSelect(command);base.ScalarExecuting(command, interceptionContext);

}///

///Linq to Entity生成的select,insert///發送到sqlserver之前觸發///warning:在select語句中DbCommand.Transaction為null,而ef會為每個insert添加一個DbCommand.Transaction進行包裹///

///

///

public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContextinterceptionContext)

{

ReadDbSelect(command);base.ReaderExecuted(command, interceptionContext);

}///

///發送到sqlserver之后觸發///

///

///

public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContextinterceptionContext)

{base.ReaderExecuted(command, interceptionContext);

}#endregion}

總結

以上是生活随笔為你收集整理的ef mysql 读写分离_EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。