dtm-examples icon indicating copy to clipboard operation
dtm-examples copied to clipboard

example for mongodb transaction with session context

Open ja7ad opened this issue 3 years ago • 4 comments

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.

ja7ad avatar Jul 26 '22 11:07 ja7ad

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.

yedf2 avatar Jul 26 '22 14:07 yedf2

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

I will try add saga pattern for test

ja7ad avatar Jul 26 '22 19:07 ja7ad

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

ja7ad avatar Jul 27 '22 04:07 ja7ad

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

yedf2 avatar Jul 27 '22 09:07 yedf2