Help Center > > Developer Guide> Distributed Transaction Management Development Guide> Connecting to DTM in Non-Intrusive Mode> Connecting Spring Cloud to DTM> Defining the Initiator of a Global Transaction

Defining the Initiator of a Global Transaction

Updated at: Sep 17, 2021 GMT+08:00

Use annotations on methods that require transaction consistency:

@DTMTxBegin(appName = "xx")

In the preceding information, appName is the unique ID of the global transaction.

The value of appName must be globally unique.

The initiator of the global transaction is the transfer method. This method calls the transfer-in service of Bank A and the transfer-out service of Bank B, and occasionally throws an exception. If an exception occurs in this method, transfer-out of Bank B fails. DTM helps Bank A roll back the transfer-in operation to ensure data consistency.
// com.huawei.bankcenter.controller.BankCenterController.java
@GetMapping(value = "transfer")
@DTMTxBegin(appName = "noninvasive-transfer-SpringCloud")
public String transfer(@RequestParam(value = "id") int id, @RequestParam(value = "money") int money,
    @RequestParam(value = "errRate") int errRate) {
    LOGGER.info("Bank-center start invoke bankA and bankB");
    bankOperator.transfer(id, money, errRate);
    return "ok";
}
// bankOperator has two implementation classes. The following uses FeignOpImpl as an example.
// com.huawei.bankcenter.impl.FeignOpImpl.java
public String transfer(int userId, int money, int errRate) {
    LOGGER.info("Start transfer---feign");
    bankAIntf.transfer(userId, money, errRate);
    bankBIntf.transfer(userId, money, errRate);
    return "ok";
}

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel