日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
深入分析EFCore事務(wù)提交,分布式事務(wù)

深入分析EF Core事務(wù)提交,分布式事務(wù)

作者:conan 2021-03-17 00:05:50

開發(fā)

前端

分布式 雖然所有關(guān)系數(shù)據(jù)庫提供程序都支持事務(wù),但在調(diào)用事務(wù) API 時(shí),可能會(huì)引發(fā)其他提供程序類型或不執(zhí)行任何操作。

本文轉(zhuǎn)載自微信公眾號「后端Q」,作者conan。轉(zhuǎn)載本文請聯(lián)系后端Q公眾號。

控制事務(wù)

可以使用 DbContext.Database API 開始、提交和回滾事務(wù)。 以下示例顯示了在單個(gè)事務(wù)中執(zhí)行的兩個(gè) SaveChanges 操作以及一個(gè) LINQ 查詢:

  
 
 
  1. using var context = new BloggingContext();
  2. using var transaction = context.Database.BeginTransaction();
  3. try
  4. {
  5.     context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
  6.     context.SaveChanges();
  7.     context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/visualstudio" });
  8.     context.SaveChanges();
  9.     var blogs = context.Blogs
  10.         .OrderBy(b => b.Url)
  11.         .ToList();
  12.     // Commit transaction if all commands succeed, transaction will auto-rollback
  13.     // when disposed if either commands fails
  14.     transaction.Commit();
  15. }
  16. catch (Exception)
  17. {
  18.     // TODO: Handle failure
  19. }

雖然所有關(guān)系數(shù)據(jù)庫提供程序都支持事務(wù),但在調(diào)用事務(wù) API 時(shí),可能會(huì)引發(fā)其他提供程序類型或不執(zhí)行任何操作。

使用 System.Transactions

如果需要跨較大作用域進(jìn)行協(xié)調(diào),則可以使用環(huán)境事務(wù)。

  
 
 
  1. using (var scope = new TransactionScope(
  2.     TransactionScopeOption.Required,
  3.     new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
  4. {
  5.     using var connection = new SqlConnection(connectionString);
  6.     connection.Open();
  7.     try
  8.     {
  9.         // Run raw ADO.NET command in the transaction
  10.         var command = connection.CreateCommand();
  11.         command.CommandText = "DELETE FROM dbo.Blogs";
  12.         command.ExecuteNonQuery();
  13.         // Run an EF Core command in the transaction
  14.         var options = new DbContextOptionsBuilder()
  15.             .UseSqlServer(connection)
  16.             .Options;
  17.         using (var context = new BloggingContext(options))
  18.         {
  19.             context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
  20.             context.SaveChanges();
  21.         }
  22.         // Commit transaction if all commands succeed, transaction will auto-rollback
  23.         // when disposed if either commands fails
  24.         scope.Complete();
  25.     }
  26.     catch (Exception)
  27.     {
  28.         // TODO: Handle failure
  29.     }
  30. }

還可以在顯式事務(wù)中登記。

  
 
 
  1. using (var transaction = new CommittableTransaction(
  2.     new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
  3. {
  4.     var connection = new SqlConnection(connectionString);
  5.     try
  6.     {
  7.         var options = new DbContextOptionsBuilder()
  8.             .UseSqlServer(connection)
  9.             .Options;
  10.         using (var context = new BloggingContext(options))
  11.         {
  12.             context.Database.OpenConnection();
  13.             context.Database.EnlistTransaction(transaction);
  14.             // Run raw ADO.NET command in the transaction
  15.             var command = connection.CreateCommand();
  16.             command.CommandText = "DELETE FROM dbo.Blogs";
  17.             command.ExecuteNonQuery();
  18.             // Run an EF Core command in the transaction
  19.             context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
  20.             context.SaveChanges();
  21.             context.Database.CloseConnection();
  22.         }
  23.         // Commit transaction if all commands succeed, transaction will auto-rollback
  24.         // when disposed if either commands fails
  25.         transaction.Commit();
  26.     }
  27.     catch (Exception)
  28.     {
  29.         // TODO: Handle failure
  30.     }
  31. }

System.Transactions 的限制

  1. EF Core 依賴數(shù)據(jù)庫提供程序以實(shí)現(xiàn)對 System.Transactions 的支持。 如果提供程序未實(shí)現(xiàn)對 System.Transactions 的支持,則可能會(huì)完全忽略對這些 API 的調(diào)用。 SqlClient 支持它。
  2. 自 .NET Core 2.1 起,System.Transactions 實(shí)現(xiàn)不包括對分布式事務(wù)的支持,因此不能使用 TransactionScope 或 CommittableTransaction 來跨多個(gè)資源管理器協(xié)調(diào)事務(wù)。

當(dāng)前題目:深入分析EFCore事務(wù)提交,分布式事務(wù)
URL鏈接:http://www.5511xx.com/article/cddsgeo.html