Principle:

Create a transaction management group Tm project. LCN registers transactions with Tm. And then commit the transaction together. TCC commits the transaction first. And then after the error into the CL method to modify the data. It's just a simple use of collation. For more information, see the official documentation.Copy the code

Documents:

Source address: https://github.com/codingapi/tx-lcn Chinese document: http://www.txlcn.org/zh-cn/docs/preface.htmlCopy the code

Steps:

Create tX-Manage database and table. 2. Create Tm project. Modify the configuration. 3. Start the TM project and check whether it is successful. 4. 6. Use the @tCCTransaction modeCopy the code

1. Create tX-manage databases and tables

CREATE TABLE 't_TX_exception' (' id 'BIGint (20) NOT NULL AUTO_INCREMENT, `group_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `transaction_state` tinyint(4) NULL DEFAULT NULL, `registrar` tinyint(4) NULL DEFAULT NULL, `remark` varchar(4096) NULL DEFAULT NULL, `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT'0 unresolved 1 solved ', `create_time` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci  ROW_FORMAT = Dynamic;Copy the code

2. Create a Tm project

1. Add dependencies
<! --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> The < version > 5.1.45 < / version > < / dependency > <! TXLCN --> <dependency> <groupId> <artifactId> <version>${codingapi.txlcn.version}</version>
</dependency>
Copy the code
2. Start the class add @ EnableTransactionManagerServer annotation

3. Modify the Tm configuration file
server:
    port: 7970
spring:
    application:
        name: tx-manager
    datasource:
        Mysql > alter databaseDriver - class - name: com. Mysql. JDBC. Driver password: 123456 url: JDBC: mysql: / / 127.0.0.1:3306 / tx - manager? characterEncoding=UTF-8 username: root redis:# redis change the data source to own.
        database: 28
        host: 
        password: 
        port:
tx-lcn:
    manager:
        # login password
        admin-key: 123456

# to see http://www.txlcn.org/zh-cn/docs/setting/manager.html for details
Copy the code
4. Overall Tm project

3. Start the project and view it

1. Enter http://localhost:7970/admin/index.html#/login

2. Based on the value of admin-key in the configuration file. Log in to

4. Use TC and register with Tm

1. Add dependencies (microservices related and Mybatis related are not mentioned)
<! --tc--> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tc</artifactId> . < version > 5.0.2 RELEASE < / version > < / dependency > <! --> <dependency> <groupId>com.codingapi.txlcn</ artifactId> . < version > 5.0.2 RELEASE < / version > < / dependency >Copy the code
2. Start the class add @ EnableDistributedTransaction annotation
3. Write Tm project address in configuration file.
#Tm project address. The default IP address is 127.0.0.1:8070. If the IP address is on the server, change it to the corresponding IP address.
Port =8070 tx-lcn.manager.port=8070 tx-lcn.manager.port=8070 The default is Tm boot port +100. Even though the documentation says -100, it's actually +100Tx - LCN: client: manager - address: 127.0.0.1:8070Copy the code
4. Start the project. Check to see if you are registered with Tm.

5. Repeat steps 1-3 to start the other service.

5. Use the @lCNTransaction mode

1. Annotate @lCNTransaction on both the consumer and provider methods.
Procedure: A: Data is inserted. A -> B: data is inserted. B -> A: Whether to throw an exception. No exception thrown with no data: transaction commit. There are two pieces of dataCopy the code

2. Then test. Do not take the EX parameter. The project was successfully run. Two pieces of data are inserted

3. Add the EX parameter. The project throws an exception. Then see if the data is committed (rolled back). It was found that the data had not changed. The distributed transaction Lcn mode is successfully used.

6. Use @lCNTransaction mode

1. The process and logic remain unchanged. Replace @lCNTransaction with @tCCTransaction annotation in project B

2. The point can be broken after data is inserted into project B. Then look at the database. Whether data is inserted.

3. Finish the program. Check whether the CF and CL methods are entered.