Multi-stage database transaction, divided into several HTTP requests

I used TransactionScope in the past with client applications to work with the rollback of an incomplete multistage transaction. This approach is unlikely to work in a web application.

Can someone suggest ways that you can follow several steps on multiple pages to roll back if the whole process is not completed? (their browser crashes or they close the browser in the middle process, for example)

Of course, I could write some temporary table, and then transfer the final record to the real table in one transaction, but this risks the conditions of the race. I would like to start a transaction, serve several pages, each page writes part of the transaction to the table (s), then completes the transaction with a commit, and if the transaction is not completed, then it is rolled back when the session ends.

Or am I not thinking of the right path? Suggestions?

Since I use MVC 3, EF 4.1, and Ninject, I'm not sure how this will affect the solution, but I thought I would include this information.

+6
source share
2 answers

No database transaction / TransactionScope on multiple pages. Even trying to do something like this is terribly wrong.

You have two options for solving the problem:

  • Use Session

    Store the data in the session and save it in the database only if the user completes all the steps and confirms the save. This is definitely what you need.]

  • Use a workflow foundation and long-running transactions.

    Long-term transactions are not database transactions - this is a fully customizable solution where you must manually perform the compensation (rollback of long-term transactions). You should still somehow find that your workflow needs to be compensated, but this is not necessary for your decision. This is the solution where you need a "transaction" for multiple sessions.

+8
source

You can look at nservicebus or masstransit settings and use their sagas.

+1
source

Source: https://habr.com/ru/post/894896/


All Articles