Multi-step database transaction split across multiple HTTP requests

荒凉一梦 提交于 2019-12-04 12:19:55

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7035259/multi-step-database-transaction-split-across-multiple-http-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!