简体   繁体   中英

How to handle data inconsistency

My applications is on Spring MVC 4.2 with postgres database. In my application, we are consuming an API which is written using spring boot having its own database (mysql).

@Transaction(rollbackfor = Exception.class)
updateOrder(Order order) {

    // This insert is part of my application
    update(order); //**STEP - 1**

   //This is not part of our application &
   // happening in api written in spring boot.
   Integer transactionId = updateOrderWorkflow(order);// **STEP - 2**

   //Below updateOrderWithTransactionId is part of my application
   //Updates the order with the transaction Id
   updateOrderWithTransactionId(order, transactionId); //**STEP - 3**
}

If STEP-3 fails, then I have to rollback the changes made in the consuming api. For rolling back I have written compensation/rollback method, which rolls back to the old workflow status.

Now the problem scenario:

If one process (PROCESS_1) is working on the above updateOrder() method and reaches to STEP-3, but before this process FAILS in STEP-3, another process (PROCESS_2) tries to access the updateOrder() method and updates STEP-2. Now PROCESS_1 fails in STEP-3 and it calls compensation/rollback method, but PROCESS_2 completes STEP-3 successfully.

This creates data inconsistency. How to handle this situation?

It sounds like the problem is that updateOrderWorkflow in step 2 exposes the changes made by the transaction of PROCESS_1 before it has been committed.

What I would do is:

  • Change updateOrderWorkflow in step 2 so that it doesn't show uncommitted changes. Any changes it makes have to be made in a temporary space of some kind, associated with the transaction ID.
  • Add an API endpoint to this same API, to commit or roll back the changes of a transaction. If committed, the changes in the temporary space are made visible globally. If rolled back, the changes are discarded.
  • Use the new commit API endpoint in your updateOrder method, and the rollback API in your rollback handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM