GithubHelp home page GithubHelp logo

wzl-bxg / ntcc-transactioncore Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 5.0 171 KB

TCC for .NET Core

License: MIT License

C# 99.70% TSQL 0.30%
distributed-transactions tcc transaction netcore csharp compensating-transaction compensation

ntcc-transactioncore's Introduction

NTcc-TransactionCore - The .NET Core port based on tcc-transaction

Introduction

This is the README file for NTcc-TransactionCore, .NET Core port of Java tcc-transaction. It supports .NET Core/netstandard 2.0 and later, not supported for .NET Framework.

NTcc-TransactionCore is an opensource project aimed at creating a free-for-commercial use TCC transaction, with enterprise features.

Architecture overview

Architecture.png

Getting Started

NuGet

NTcc-TransactionCore can be installed in your project with the following command.

 PM> Install-Package NTccTransactionCore
 PM> Install-Package NTccTransactionCore.Aop

NTcc-TransactionCore Currently supports Oracle, SqlServer as transaction log storage, following packages are available to install:

 PM> Install-Package NTccTransactionCore.Oracle
 PM> Install-Package NTccTransactionCore.SqlServer

Configuration

First, you need to configure NTcc-TransactionCore in your Startup.cs

public ILifetimeScope AutofacContainer { get; private set; }

public IServiceCollection Services { get; private set; }

public void ConfigureServices(IServiceCollection services)
{
    //......

    services.AddNTccTransaction((option) =>
    {
        option.UseOracle((oracleOption) =>
        {
            oracleOption.ConnectionString = Configuration.GetConnectionString("Your ConnectionStrings");// 						configure db connectiong
        });

        option.UseCastleInterceptor(); // use Castle Interceptor
    });

    Services = services;
}

public void ConfigureContainer(ContainerBuilder containerBuilder)
{
    containerBuilder.Register(Services); // register Castle Interceptor
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
}

DB Script

Currently supports Oracle, SqlServer as transaction log storage, execute the following database script to create transaction table:

Oracle

CREATE TABLE NTCC_TRANSACTION 
(
  TRANSACTION_ID VARCHAR2(128) NOT NULL 
  , GLOBAL_TRANSACTION_ID VARCHAR2(128)
  , BRANCH_QUALIFIER VARCHAR2(128) 
  , STATUS NUMBER(9, 0) NOT NULL 
  , TRANSACTION_TYPE NUMBER(9, 0) NOT NULL 
  , RETRIED_COUNT NUMBER(9, 0) NOT NULL 
  , CREATE_UTC_TIME DATE NOT NULL 
  , LAST_UPDATE_UTC_TIME DATE NOT NULL 
  , VERSION NUMBER(9, 0) NOT NULL 
  , CONTENT CLOB 
  , CONSTRAINT PK_NTCC_TRANSACTION PRIMARY KEY 
  (
     TRANSACTION_ID 
  )
);

Sql Server

CREATE TABLE [dbo].[NTCC_TRANSACTION] 
(
  [TRANSACTION_ID] varchar(128) NOT NULL 
  ,[GLOBAL_TRANSACTION_ID] varchar(128) NULL 
  ,[BRANCH_QUALIFIER] varchar(128) NULL 
  ,[STATUS] int NOT NULL 
  ,[TRANSACTION_TYPE] int NOT NULL 
  ,[RETRIED_COUNT] int NOT NULL 
  ,[CREATE_UTC_TIME] datetime NOT NULL 
  ,[LAST_UPDATE_UTC_TIME] datetime NOT NULL 
  ,[VERSION] int NOT NULL 
  ,[CONTENT] nvarchar(MAX) NULL 
  ,PRIMARY KEY 
  (
  	[TRANSACTION_ID]
  )
)

In Business Logic Service

In your business service, you need implement INTccTransactionService:

public class OrderService : IOrderService, INTccTransactionService
{
    private readonly ILogger<OrderService> _logger;
    private readonly ICapitalProxy _capitalProxy;

    public OrderService(ILogger<OrderService> logger, ICapitalProxy capitalProxy)
    {
        _logger = logger;
        _capitalProxy = capitalProxy;
    }

    [Compensable(CancelMethod = "CancelOrder", ConfirmMethod = "ConfirmOrder")]
    public async Task<string> TryPostOrder(string input, TransactionContext transactionContext = null)
    {
        return await Task.FromResult("");
    }

    public async Task ConfirmOrder(string input, TransactionContext transactionContext = null)
    {
        await Task.CompletedTask;
    }

    public async Task CancelOrder(string input, TransactionContext transactionContext = null)
    {
        await Task.CompletedTask;
    }
}

And add the attribute [Compensable(CancelMethod = "xxx", ConfirmMethod = "xxx")] on the Try method:

[Compensable(CancelMethod = "CancelOrder", ConfirmMethod = "ConfirmOrder")]
public async Task<string> TryPostOrder(string input, TransactionContext transactionContext = null)
{
   return await Task.FromResult("");
}

The type of the last parameter of the Try method must be TransactionContext, it's used to propagate transactions, and you need add Confirm method and Cancel method in the business logic service,the parameters of the two methods must be same as Try method.

public async Task ConfirmOrder(string input, TransactionContext transactionContext = null)
{
    await Task.CompletedTask;
}

public async Task CancelOrder(string input, TransactionContext transactionContext = null)
{
    await Task.CompletedTask;
}

Then register your class that implement INTccTransactionService in Startup.cs :

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IOrderService, OrderService>();
    services.AddTransient<OrderService>();
}

Contribute

One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.

ntcc-transactioncore's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.