Skip to content

wzl-bxg/NTcc-TransactionCore

Repository files navigation

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.