Other URL
Integration of distributed transaction Seata and sub-database and sub-table sharding-sphere_-CSDN博客
Corresponding github address: https://github.com/dean4lee/distributed-transaction-demo/tree/master/springcloud-shardingjdbc-seata
springcloud-shardingjdbc-seata distributed transaction_dean.lee's blog -CSDN blog
Corresponding github address: https://github.com/dean4lee/distributed-transaction-demo
Introduction
Description
This article uses examples to integrate sharding-jdbc-spring-boot-starter: 4.1.1, spring-cloud-starter-alibaba-seata: 2.2.3.RELEASE, seata-spring-boot-starter: 1.4.1, mybatis-plus- boot-starter: 3.4.1 dynamic data source
Project address: https://gitee.com/shapeless/demo_SpringCloud/tree/sharding-jdbc+seata-at
Precautions
I have referenced no less than 50 blogs, and only succeeded according to the two blogs above "Other URLs".
The key points of the code:
Add the following two notes before the method of importing business:
@GlobalTransactional
@TransactionalWrite this code in the first line of this method: TransactionTypeHolder.set(TransactionType.BASE); //It is useless to add this annotation before the method: @ShardingTransactionType(value = TransactionType.BASE)
Why add @Transactional?
Because the operation on a table in my method is: it is updated after inserting. If @Transactional is not added, if an exception is reported after the update, the rollback will fail, and it will continue to retry. For details, please see: https://github.com/seata/seata/issues/3036
test
Sub-database sub-table and no exceptions are reported
http://localhost:6001/order/order/createShardingNormal?userId=1&productId=1&count=10&money=100
result:
An order data is inserted into the t_order_x table of the sharding-jdbcx database of the sub-database and sub-table
The amount of t_acount and the inventory of t_storage of the seata database, regardless of database and table, are changed.
Sub-database and sub-table and report exception
http://localhost:6001/order/order/createShardingFault?userId=1&productId=1&count=10&money=100
Order data is not inserted in the t_order_x table of the sharding-jdbcx database of the sub-database and sub-table
The amount of t_acount and the inventory of t_storage of the seata database, regardless of database and table, have not changed.
No database or table and no exceptions are reported
http://localhost:6001/order/order/createNoShardingNormal?userId=1&productId=1&count=10&money=100
A piece of order data is inserted into the t_order table of the seata database without database and table
The amount of t_acount and the inventory of t_storage of the seata database, regardless of database and table, are changed.
Regardless of database and table and report exceptions
http://localhost:6001/order/order/createNoShardingFault?userId=1&productId=1&count=10&money=100
A piece of order data is inserted into the t_order table of the seata database without database and table
The amount of t_acount and the inventory of t_storage of the seata database, regardless of database and table, are changed.