当前位置:网站首页>Talk about the SQL server version of DTM sub transaction barrier function
Talk about the SQL server version of DTM sub transaction barrier function
2022-06-30 18:08:00 【Dotnet cross platform】
background
I wrote two articles before ( Talk about how to use it C# Easily complete a SAGA Distributed transactions and Talk about how to use it C# Easily complete a TCC Distributed transactions ) How to use C# be based on DTM Make it easy SAGA and TCC Of distributed transactions , There is a sub transaction barrier function , Good handling of null compensation 、 Hang 、 Repeated requests and other abnormal problems .
https://dtm.pub/practice/barrier.html
But the previous examples are based on mysql Of , It was asked if other databases were supported .
Obviously this is supported , Relational databases , except mysql, And support pgsql and sql server .
Some existing systems , There must be a lot more based on sql server Developed , If this batch of systems want to access DTM, There is no need to move the database first and then access .
So next , Lao Huang will take you through the experience based on sql server Version of DTM Sub transaction barrier function .
The example will be based on the previous SAGA Examples of transformation .
Sub transaction barrier table
Use the sub transaction barrier first , It is essential to prepare a sub transaction barrier table , So this sub transaction barrier table should be placed under the database of that instance ?
All you need to do here is make sure , The business data to be operated and the sub transaction barrier can be in the same local transaction .
The following is for SQL Server Table of SQL.
IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N’[dbo].[barrier]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
BEGIN
DROP TABLE [dbo].[barrier]
END
GO
CREATE TABLE [dbo].[barrier]
(
[id] bigint NOT NULL IDENTITY(1,1) PRIMARY KEY,
[trans_type] varchar(45) NOT NULL DEFAULT(''),
[gid] varchar(128) NOT NULL DEFAULT(''),
[branch_id] varchar(128) NOT NULL DEFAULT(''),
[op] varchar(45) NOT NULL DEFAULT(''),
[barrier_id] varchar(45) NOT NULL DEFAULT(''),
[reason] varchar(45) NOT NULL DEFAULT(''),
[create_time] datetime NOT NULL DEFAULT(getdate()) ,
[update_time] datetime NOT NULL DEFAULT(getdate())
)
GO
CREATE UNIQUE INDEX[ix_uniq_barrier] ON[dbo].[barrier]
([gid] ASC, [branch_id] ASC, [op] ASC, [barrier_id] ASC)
WITH(IGNORE_DUP_KEY = ON)
GOThe key here is the unique index , There is one IGNORE_DUP_KEY = ON. This is actually for equivalence mysql Of insert ignore.
In fact, the preparatory work has already been completed here .
Here is an example after the transformation , Not much to do , Just switch some configurations .
Switch configuration
To be in InApi Which specifies the sub transaction barrier Database type and Custom table name .
Perform the following configuration before startup
// Appoint Sub transaction barrier The database type is sql server
Dtmcli.DtmImp.DbSpecialDelegate.Instance.SetCurrentDBType("sqlserver");
// Appoint Table name of the sub transaction barrier table
Dtmcli.BranchBarrier.SetBarrierTableName("[test].[dbo].[barrier]");For the table name , Lao Huang suggested adding the name of the library .
PS: This setting should be put into the configuration file later , At this stage, it is basically to maintain and go The version is written in a similar way .
Last but not least is to change the connection string and SqlConnection 了 .
public class Db
{
private static readonly string _conn = "Data Source=127.0.0.1, 1433;Initial Catalog=test;user id=dev;password=123456;TrustServerCertificate=True;";
public static DbConnection GeConn() => new Microsoft.Data.SqlClient.SqlConnection(_conn);
}Come here , All the transformation has been completed . Let's take a look at the specific example demonstration .
Example demonstration
Here's a demonstration of Talk about how to use it C# Easily complete a SAGA Distributed transactions There are two problems: null compensation and idempotence .
Let's look at the short compensation

Let's look at idempotent

The log output and behavior effect are basically consistent .
Finally, look at the data in the database

At the end
In this article , Describes how to use in the sub transaction barrier SQL Server As underlying storage . I hope it will be helpful for you to study distributed transactions .
Sample code of this article :
https://github.com/catcherwong-archive/2022/tree/main/DtmMsSqlSample
Reference material
https://dtm.pub/practice/barrier.html# principle
https://github.com/dtm-labs/dtmcli-csharp
边栏推荐
- MSF后渗透总结
- Grep output with multiple colors- Grep output with multiple Colors?
- ASP. Net authentication code login
- Simulation of campus network design based on ENSP
- 构建基本buildroot文件系统
- splitting. JS password display hidden JS effect
- Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases
- 5g has been in business for three years. Where will innovation go in the future?
- Redis (V) - advanced data types
- Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.
猜你喜欢

Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution

墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路

K-line diagram interpretation and practical application skills (see position entry)

Compile and generate busybox file system

Post penetration file system + uploading and downloading files

Redis (I) - data type

New skill: accelerate node through code cache JS startup

Deep understanding of JVM (II) - memory structure (II)
![[Architecture] 1366- how to draw an excellent architecture diagram](/img/98/5dc29e08e91e751f67d910fadc6430.jpg)
[Architecture] 1366- how to draw an excellent architecture diagram

Six pictures show you why TCP has three handshakes?
随机推荐
Develop those things: how to add text watermarks to videos?
ASP. Net generate verification code
Deep understanding of JVM (VI) -- garbage collection (III)
腾讯持久化框架MMKV原理探究
Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
构建基本buildroot文件系统
MSF后渗透总结
Redis (IV) - delete policy
Rainbow Brackets 插件的快捷键
TCP session hijacking based on hunt1.5
Design of online shopping mall based on SSH
Flutter custom component
ABAP publish restful service
Only black-and-white box test is required for test opening post? No, but also learn performance test
[machine learning] K-means clustering analysis
Dropout: immediate deactivation
[sword finger offer] 52 The first common node of two linked lists
Nft: unlimited possibilities to open the era of encryption Art
ABAP-发布Restful服务
Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!