EF Core下利用Mysql进行数据存储在并发访问下的数据同步问题
小故事
在開始講這篇文章之前,我們來說一個小故事,純素虛構(真實的存錢邏輯并非如此)
小劉發工資后,趕忙拿著現金去銀行,準備把錢存起來,而與此同時,小劉的老婆劉嫂知道小劉的品性,知道他發工資的日子,也知道他喜歡一發工資就去銀行存起來,擔心小劉卡里存的錢太多拿去“大寶劍”,于是,也去了銀行,想趁著小劉把錢存進去后就把錢給取出來,省的夜長夢多。
?
小劉與劉嫂取得是兩家不同的銀行的ATM,所以兩人沒有碰面。
小劉插入銀行卡存錢之前查詢了自己的余額,ATM這樣顯示的:
?
與次同時,劉嫂也通過卡號和密碼查詢該卡內的余額,也是這么顯示的:
?
劉嫂,很生氣,沒想到小劉偷偷藏了5000塊錢的私房錢,就把5000塊錢全部取出來了。所以把賬戶6217****888888的金額更新成0.(查詢結果5000基礎上減5000)
在這之后,小劉把自己發的3000塊錢也存到了銀行卡里,所以這邊的這臺ATM把賬戶6217****888888的金額更新成了8000.(在查詢的5000基礎上加3000)
最終的結果是,小劉的銀行卡金額8000塊錢,劉嫂也拿到了5000塊錢。
反思?
故事結束了,很多同學肯定會說,要真有這樣的銀行不早就倒閉了?確實,真是的銀行不可能是這樣來計算的,可是我們的同學在設計程序的時候,卻經常是這樣的一個思路,先從數據庫中取值,然后在取到的值的基礎上對該值進行修改。可是,卻有可能在取到值之后,另外一個客戶也取了值,并在你保存之前對數據進行了更新。那么如何解決?
解決辦法—樂觀鎖
常用的辦法是,使用客觀鎖,那么什么是樂觀鎖?
下面是來自百度百科關于樂觀鎖的解釋:
樂觀鎖,大多是基于數據版本( Version )記錄機制實現。何謂數據版本?即為數據增加一個版本標識,在基于數據庫表的版本解決方案中,一般是通過為數據庫表增加一個 “version” 字段來實現。讀取出數據時,將此版本號一同讀出,之后更新時,對此版本號加一。此時,將提交數據的版本數據與數據庫表對應記錄的當前版本信息進行比對,如果提交的數據版本號大于數據庫表當前版本號,則予以更新,否則認為是過期數據。
?
通俗地講,就是在我們設計數據庫的時候,給實體添加一個Version的屬性,對實體進行修改前,比較該實體現在的Version和自己當年取出來的Version是否一致,如果一致,對該實體修改,同時,對Version屬性+1;如果不一致,則不修改并觸發異常。
?
作為強大的EF(Entiry FrameWork)當然對這種操作進行了封裝,不用我們自己獨立地去實現,但是在查詢微軟官方文檔時,我們發現,官方文檔是利用給Sql Server數據庫添加timestamp標簽實現的,Sql Server在數據發生更改時,能自動地對timestamp進行更新,但是Mysql沒有這樣的功能的,我是通過并發令牌(ConcurrencyToken)實現的。
?
什么是并發令牌(ConcurrencyToken)?
所謂的并發令牌,就是在實體的屬性中添加一塊令牌,當對數據執行修改操作時,系統會在Sql語句后加一個Where條件,篩選被標記成令牌的字段是否與取出來一致,如果不一致了,返回的肯定是影響0行,那么此時,就會對拋出異常。
具體怎么用?
首先,新建一個WebApi項目,然后在該項目的Model目錄(如果沒有就手動創建)新建一個student實體。其代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Bingfa.Model
{
? ? public class Student
? ? {
? ? ? ? public int id { get; set; }
? ? ? ? public string Name { get; set; }
? ? ? ? public string Pwd { get; set; }
? ? ? ? public int Age { get; set; }
? ? ? ? public DateTime LastChanged { get; set; }
? ? }
}
然后創建一個數據庫上下文,其代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Bingfa.Model
{
? ? public class SchoolContext : DbContext
? ? {
? ? ? ? public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
? ? ? ? {
? ? ? ? }
? ? ? ? public DbSet<Student> students { get; set; }
? ? ? ? protected override void OnModelCreating(ModelBuilder modelBuilder)
? ? ? ? {
? ? ? ? ? ? modelBuilder.Entity<Student>().Property(p => p.LastChanged).IsConcurrencyToken() ;
? ? ? ? }
? ? }
}
紅色部分,我們把Student的LastChange屬性標記成并發令牌。
然后在依賴項中選擇Nuget包管理器,安裝??Pomelo.EntityFrameworkCore.MySql 改引用,該引用可以理解為Mysql的EF Core驅動。
安裝成功后,在appsettings.json文件中寫入Mysql數據庫的連接字符串。寫入后,該文件如下:其中紅色部分為連接字符串
{
? "Logging": {
? ? "IncludeScopes": false,
? ? "Debug": {
? ? ? "LogLevel": {
? ? ? ? "Default": "Warning"
? ? ? }
? ? },
? ? "Console": {
? ? ? "LogLevel": {
? ? ? ? "Default": "Warning"
? ? ? }
? ? }
? },
? "ConnectionStrings": { "Connection": "Data Source=127.0.0.1;Database=school;User ID=root;Password=123456;pooling=true;CharSet=utf8;port=3306;" }
}
然后,在Stutup.cs中對Mysql進行依賴注入:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bingfa.Model;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Bingfa
{
? ? public class Startup
? ? {
? ? ? ? public Startup(IConfiguration configuration)
? ? ? ? {
? ? ? ? ? ? Configuration = configuration;
? ? ? ? }
? ? ? ? public IConfiguration Configuration { get; }
? ? ? ? // This method gets called by the runtime. Use this method to add services to the container.
? ? ? ? public void ConfigureServices(IServiceCollection services)
? ? ? ? {
? ? ? ? ? ? var connection = Configuration.GetConnectionString("Connection");
? ? ? ? ? ? services.AddDbContext<SchoolContext>(options =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? options.UseMySql(connection);
? ? ? ? ? ? ? ? options.UseLoggerFactory(new LoggerFactory().AddConsole());
? ? ? ? ? ? });
? ? ? ? ? ? services.AddMvc();
? ? ? ? }
? ? ? ? // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
? ? ? ? public void Configure(IApplicationBuilder app, IHostingEnvironment env)
? ? ? ? {
? ? ? ? ? ? if (env.IsDevelopment())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? app.UseDeveloperExceptionPage();
? ? ? ? ? ? }
? ? ? ? ? ? app.UseMvc();
? ? ? ? }
? ? }
}
其中,紅色字體部分即為對Mysql數據庫上下文進行注入,藍色背景部分,為將sql語句在控制臺中輸出,便于我們查看運行過程中的sql語句。
以上操作完成后,即可在數據庫中生成表了。打開程序包管理控制臺,打開方式如下:
打開后分別輸入以下兩條命令:、
add-migration init
update-database
是分別輸入哦,不是一次輸入兩條,語句執行效果如圖:
執行完成后即可在Mysql數據庫中看到生成的數據表了,如圖。
最后,我們就要進行實際的業務處理過程的編碼了。打開ValuesController.cs的代碼,我修改后代碼如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bingfa.Model;
using Microsoft.AspNetCore.Mvc;
namespace Bingfa.Controllers
{
? ? [Route("api/[controller]")]
? ? public class ValuesController : Controller
? ? {
? ? ? ? private SchoolContext schoolContext;
? ? ? ? public ValuesController(SchoolContext _schoolContext)//控制反轉,依賴注入
? ? ? ? {
? ? ? ? ? ? schoolContext = _schoolContext;
? ? ? ? }
? ? ? ??
? ? ? ? // GET api/values/5
? ? ? ? [HttpGet("{id}")]
? ? ? ? public Student Get(int id)
? ? ? ? {
? ? ? ? ? ? return schoolContext.students.Where(p => p.id == id).FirstOrDefault();? //通過Id獲取學生數據
? ? ? ? }
? ? ? ? [HttpGet]
? ? ? ? public List<Student> Get()
? ? ? ? {
? ? ? ? ? ? return schoolContext.students.ToList();? ?//獲取所有的學生數據
? ? ? ? }
? ? ? ? // POST api/values
? ? ? ? [HttpPost]
? ? ? ? public string Post(Student student)? ?//更新學生數據
? ? ? ? {
? ? ? ? ? ? if (student.id != 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Student studentDataBase = schoolContext.students.Where(p => p.id == student.id).FirstOrDefault();? ?//首先通過Id找到該學生
? ? ? ? ? ? ? ? ? ? //如果查找到的學生的LastChanged與Post過來的數據的LastChanged的時間相同,則表示數據沒有修改過
? ? ? ? ? ? ? ? ? ? //為了控制時間精度,對時間進行秒后取三位小數
? ? ? ? ? ? ? ? ? ? if (studentDataBase.LastChanged.ToString("yyyy-MM-dd HH:mm:ss.fff").Equals(student.LastChanged.ToString("yyyy-MM-dd HH:mm:ss.fff")))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? studentDataBase.LastChanged=DateTime.Now;//把數據的LastChanged更改成現在的時間
? ? ? ? ? ? ? ? ? ? ? ? studentDataBase.Age = student.Age;
? ? ? ? ? ? ? ? ? ? ? ? studentDataBase.Name = student.Name;
? ? ? ? ? ? ? ? ? ? ? ? studentDataBase.Pwd = student.Pwd;
? ? ? ? ? ? ? ? ? ? ? ? schoolContext.SaveChanges();? //保存數據
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? throw new Exception("數據已經修改,請刷新查看");
? ? ? ? ? ? ? ? ? ? ? ? //return "";
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return e.Message;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return "success";
? ? ? ? ? ? }
? ? ? ? ? ? return "沒有找到該Student";
? ? ? ? }
? ? ? ? // PUT api/values/5
? ? ? ? [HttpPut("{id}")]
? ? ? ? public void Put(int id, [FromBody]string value)
? ? ? ? {
? ? ? ? }
? ? ? ? // DELETE api/values/5
? ? ? ? [HttpDelete("{id}")]
? ? ? ? public void Delete(int id)
? ? ? ? {
? ? ? ? }
? ? }
}
主要代碼在Post方法中。
為了方便看到運行的Sql語句,我們需要把啟動程序更改成項目本身而不是IIS。如圖
啟動后效果如圖:
我們先往數據庫中插入一條數據
然后,通過訪問http://localhost:56295/api/values/1即可獲取該條數據,如圖:
我們把該數據修改age成2之后,利用postMan把數據post到控制器,進行數據修改,如圖,修改成功
那么,我們把age修改成3,LastChange的數據依然用第一次獲取到的時間進行Post,那么返回的結果如圖:
可以看到,執行了catch內的代碼,觸發了異常,沒有接受新的提交。
最后,我們看看加了并發鎖之后的sql語句:
從控制臺中輸出的sql語句可以看到? 對LastChanged屬性進行了篩選,只有當LastChanged與取出該實體時一致,該更新才會執行。
這就是樂觀鎖的實現過程。
并發訪問測試程序
為了對該程序進行測試,我特意編寫了一個程序,多線程地對數據庫的數據進行get和post,模擬一個并發訪問的過程,代碼如下:
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using Newtonsoft.Json;
namespace Test
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("輸入回車開始測試...");
? ? ? ? ? ? Console.ReadKey();
? ? ? ? ? ? ServicePointManager.DefaultConnectionLimit = 1000;
? ? ? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Thread td = new Thread(new ParameterizedThreadStart(PostTest));
? ? ? ? ? ? ? ? td.Start(i);
? ? ? ? ? ? ? ? Thread.Sleep(new Random().Next(1,100));//隨機休眠時長
? ? ? ? ? ? }
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? ? ? public static void PostTest(object i)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string url = "http://localhost:56295/api/values/1";//獲取ID為1的student的信息
? ? ? ? ? ? ? ? Student student = JsonConvert.DeserializeObject<Student>(RequestHandler.HttpGet(url));
? ? ? ? ? ? ? ? student.Age++;//對年齡進行修改
? ? ? ? ? ? ? ? string postData = $"Id={ student.id}&age={student.Age}&Name={student.Name}&Pwd={student.Pwd}&LastChanged={student.LastChanged.ToString("yyyy-MM-dd HH:mm:ss.fff")}";
? ? ? ? ? ? ? ? Console.WriteLine($"線程{i.ToString()}Post數據{postData}");
? ? ? ? ? ? ? ? string r = RequestHandler.HttpPost("http://localhost:56295/api/values", postData);
? ? ? ? ? ? ? ? Console.WriteLine($"線程{i.ToString()}Post結果{r}");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(ex.Message);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
測試效果:
可以看到,部分修改成功了,部分沒有修改成功,這就是樂觀鎖的效果。
項目的完整代碼我已經提交到github,有興趣的可以訪問以下地址查看:
https://github.com/liuzhenyulive/Bingfa
第一次這么認真地寫一篇文章,如果喜歡,請推薦支持,謝謝!
原文:https://www.cnblogs.com/CoderAyu/p/8530798.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的EF Core下利用Mysql进行数据存储在并发访问下的数据同步问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建基于MailKit和MimeKit的
- 下一篇: EF Core:一统SQL和NoSQL数