This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Seata configuration has several configuration items to note:
- Seata. Support. Spring. The datasource – autoproxy: true attribute data source proxy automatically switch, in order to service, the account – service, in the repo – service is set to true, the rest – we Set to false in b because the project does not access the data source and does not require a proxy
- If the registry file, seata. Service. Grouplist need seata server connection address, by default, the registry for the file, if need to service discovery from the registry, can increase the configuration is as follows:
seata:
registry:
type: nacos
nacos:
cluster: default
server-addr: 192.168216.128.: 8848
Copy the code
- Tx-service-group specifies the transaction group to which the service belongs. If this parameter is not executed, spring.application.name plus -seata-service-group is used by default. Either of the two parameters must be configured; otherwise, an error occurs.
Add a rollback log table
Add a rollback log table to the three databases seata-Account, Seata-repo, and Seata-Order to record the rollback log of each database table operation. If a transaction of a service is abnormal, the rollback will be performed based on the log.
CREATE TABLE undo_log(
id bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' ' ,
branch_id bigint(20) COMMENT ' ' ,
xid VARCHAR(200) COMMENT ' ' ,
context VARCHAR(250) COMMENT ' ' ,
rollback_info longblob COMMENT ' ' ,
log_status int(11) COMMENT ' ' ,
log_created DATETIME COMMENT ' ' ,
log_modified DATETIME COMMENT ' ' ,
PRIMARY KEY (id)
UNIQUE KEY 'ux_undo_log'('xid,'branch_id') )ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT = 'Rollback log tables';
Copy the code
Rest-web adds global transaction control
To modify RestOrderServiceImpl for a rest-web project, do the following:
- Added the @GlobalTransactional global transaction annotation
- Simulates an exception handling that throws an exception when the item number is equal to a specified value, triggering a rollback of the entire transaction.
@Slf4j
@Service
public class RestOrderServiceImpl implements IRestOrderService{
@Reference
private IRepoService repoService;
@Reference
private IOrderService orderService;
@Override
@GlobalTransactional(timeoutMills=300000,name="rest-web")
public AjaxResult handleBusiness(OrderReq orderReq) throw Exception{
log.info("Start global transaction :xid={}",RootContext.getIp());
log.info("Start order :"+orderReq);
ProductDto productDto = new ProductDto();
productDto.setProductCode(orderReq.getProductCode());
productDto.setCount(orderReq.getCount());
AjaxResult ajaxResult = repoService.decreaseRepo(productDto);
// Create an order
OrderDto orderDto = new OrderDto();
orderDto.setUserId(orderReq.getUserId());
orderDto.setOrderAmount(orderReq.getAmount());
orderDto.setOrderCount(orderReq.getCount());
orderDto.setProductCode(orderReq.getProductCode());
AjaxResult orderResult = orderServce.createOrder(orderDto);
if(orderReq.getProductCode().equals("20211113")) {throw new Exception("Trigger exception");
}
AjaxResult result = new AjaxResult();
result.put("data",orderResult.getData());
returnresult; }}Copy the code
In the location of the exception, without the introduction of a distributed transaction, even if the exception, due to inventory deductions, such as capital deductions order creation, account operation has effect, so the data cannot be rolled back, and after the introduction of seata will trigger each transaction branch after the rise of abnormal data rollback, guarantee the correctness of the data, if the configuration is normal, The transaction rollback can be completed.