dtm-labs / dtm-examples

various examples for dtm
MIT License
79 stars 22 forks source link

example for mongodb transaction with session context #3

Open Ja7ad opened 2 years ago

Ja7ad commented 2 years ago

hi It is my first time working with DTM for transactions in a microservice architecture. Using mongodb in my services, I need more examples of distributed transactions. MongoDB has transaction logic, but I do not know how to use DTM to roll back a session transaction in MongoDB. I want add dtm to my example project: https://github.com/Ja7ad/Library Please help me for this.

yedf2 commented 2 years ago

I have take a look at your repo, and there is only a picture describing the logic.

Because MongoDB does not support XA transaction, so you should implement to rollback service manually and submit the update and rollback urls to DTM like this:

    saga := dtmcli.NewSaga(DtmServer, shortuuid.New()).
        // add a TransOut sub-transaction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom"
        Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req).
        // add a TransIn sub-transaction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom"
        Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req)
    // submit the created saga transaction,dtm ensures all sub-transactions either complete or get revoked
    err := saga.Submit()

If you want me the help, you should firstly add some docs for a new developer to run your example project easily.

Ja7ad commented 2 years ago

Ok MonogoDB having oplog on replica and logging any trigger on documents.

I will try add saga pattern for test

Ja7ad commented 2 years ago

https://github.com/Ja7ad/Library/blob/main/README.md#how-to-run-server

yedf2 commented 2 years ago

Maybe you should finish your project without distributed transactions, and then consider how to add dtm to it.

I take a look at your project, client logics, rollback functions are not completed