Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-step database transaction split across multiple HTTP requests

I have used TransactionScope in the past with desktop client applications to roll back an incomplete multi-step transaction. That approach would seem unlikely to work in an Web application.

Can anyone suggest ways in which multiple steps, across several pages, can be ensured to roll back if the entire process is not completed? (their browser crashes or they close the browser in mid process for example)

Certainly, i could write to a temp table of some sort, then transfer the final record to the real table in a single transaction, but that runs the risk of race conditions. I'd like to begin a transaction, serve several pages, each page writing a piece of the transaction to the table(s), then completing 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 the right way? Suggestions?

Since i'm using MVC 3, EF 4.1 and Ninject, i'm not sure how that will affect the solution, but i thought i'd include that information.

like image 948
Erik Funkenbusch Avatar asked Aug 12 '11 03:08

Erik Funkenbusch


2 Answers

There is no database transaction / TransactionScope across several pages. Even trying to do something like that is terribly wrong.

You have two options to solve the problem:

  • Use Session

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

  • Use workflow foundation and long running transactions.

    Long running transactions are not database transactions - they are a completely custom solution where you must implement compensation (rollback of long running transactions) manually. You still have to detect somehow that your workflow should be compensated, but this is not necessary for your solution. It is for a solution where you need a "transaction" for multiple sessions.

like image 165
Ladislav Mrnka Avatar answered Oct 24 '22 10:10

Ladislav Mrnka


You might look at setting up nservicebus or masstransit and use their saga facilies.

like image 36
Aaron Fischer Avatar answered Oct 24 '22 09:10

Aaron Fischer